1
2
3
4 package net.sourceforge.pmd.lang.java.rule.imports;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
10 import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
11 import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
12 import net.sourceforge.pmd.lang.java.ast.ASTName;
13 import net.sourceforge.pmd.lang.java.ast.ASTPackageDeclaration;
14 import net.sourceforge.pmd.lang.java.ast.JavaNode;
15 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
16
17 public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule {
18
19 private List<ASTImportDeclaration> imports = new ArrayList<ASTImportDeclaration>();
20 private List<ASTImportDeclaration> matches = new ArrayList<ASTImportDeclaration>();
21
22 public UnnecessaryFullyQualifiedNameRule() {
23 super.addRuleChainVisit(ASTCompilationUnit.class);
24 super.addRuleChainVisit(ASTImportDeclaration.class);
25 super.addRuleChainVisit(ASTClassOrInterfaceType.class);
26 super.addRuleChainVisit(ASTName.class);
27 }
28
29 @Override
30 public Object visit(ASTCompilationUnit node, Object data) {
31 imports.clear();
32 return data;
33 }
34
35 @Override
36 public Object visit(ASTImportDeclaration node, Object data) {
37 imports.add(node);
38 return data;
39 }
40
41 @Override
42 public Object visit(ASTClassOrInterfaceType node, Object data) {
43 checkImports(node, data, false);
44 return data;
45 }
46
47 @Override
48 public Object visit(ASTName node, Object data) {
49 if (!(node.jjtGetParent() instanceof ASTImportDeclaration)
50 && !(node.jjtGetParent() instanceof ASTPackageDeclaration)) {
51 checkImports(node, data, true);
52 }
53 return data;
54 }
55
56 private void checkImports(JavaNode node, Object data, boolean checkStatic) {
57 String name = node.getImage();
58 matches.clear();
59
60
61 for (ASTImportDeclaration importDeclaration : imports) {
62 if (importDeclaration.isImportOnDemand()) {
63
64 if (name.startsWith(importDeclaration.getImportedName())) {
65 if (name.lastIndexOf('.') == importDeclaration.getImportedName().length()) {
66 matches.add(importDeclaration);
67 continue;
68 }
69 }
70 } else {
71
72 if (name.equals(importDeclaration.getImportedName())) {
73 matches.add(importDeclaration);
74 continue;
75 }
76
77 if (name.startsWith(importDeclaration.getImportedName())) {
78 if (name.lastIndexOf('.') == importDeclaration.getImportedName().length()) {
79 matches.add(importDeclaration);
80 continue;
81 }
82 }
83 }
84 }
85
86
87
88
89
90
91
92
93
94
95
96 if (matches.isEmpty() && name.indexOf('.') >= 0) {
97 for (ASTImportDeclaration importDeclaration : imports) {
98 if (importDeclaration.isStatic()) {
99 String[] importParts = importDeclaration.getImportedName().split("\\.");
100 String[] nameParts = name.split("\\.");
101 if (importDeclaration.isImportOnDemand()) {
102
103 if (nameParts[nameParts.length - 2].equals(importParts[importParts.length - 1])) {
104 matches.add(importDeclaration);
105 }
106 } else {
107
108 if (nameParts[nameParts.length - 1].equals(importParts[importParts.length - 1])
109 && nameParts[nameParts.length - 2].equals(importParts[importParts.length - 2])) {
110 matches.add(importDeclaration);
111 }
112 }
113 }
114 }
115 }
116
117 if (!matches.isEmpty()) {
118 ASTImportDeclaration firstMatch = matches.get(0);
119 String importStr = firstMatch.getImportedName() + (matches.get(0).isImportOnDemand() ? ".*" : "");
120 String type = firstMatch.isStatic() ? "static " : "";
121 addViolation(data, node, new Object[] { node.getImage(), importStr, type });
122 }
123
124 matches.clear();
125 }
126 }