|
1 |
| package net.sourceforge.pmd.rules; |
|
2 |
| |
|
3 |
| import net.sourceforge.pmd.AbstractRule; |
|
4 |
| import net.sourceforge.pmd.ast.ASTAdditiveExpression; |
|
5 |
| import net.sourceforge.pmd.ast.ASTLiteral; |
|
6 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
|
7 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
|
8 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
9 |
| import net.sourceforge.pmd.symboltable.NameOccurrence; |
|
10 |
| |
|
11 |
| import java.util.Iterator; |
|
12 |
| import java.util.List; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| public abstract class AbstractPoorMethodCall extends AbstractRule { |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| protected abstract String targetTypename(); |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| protected abstract String[] methodNames(); |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| protected abstract boolean isViolationArgument(int argIndex, String arg); |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
6
| private boolean isNotedMethod(NameOccurrence occurrence) {
|
|
62 |
| |
|
63 |
0
| if (occurrence == null) return false;
|
|
64 |
| |
|
65 |
6
| String methodCall = occurrence.getImage();
|
|
66 |
6
| String[] methodNames = methodNames();
|
|
67 |
| |
|
68 |
7
| for (int i=0; i<methodNames.length; i++) {
|
|
69 |
6
| if (methodCall.indexOf(methodNames[i]) != -1) return true;
|
|
70 |
| } |
|
71 |
0
| return false;
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
7
| public static boolean isSingleCharAsString(String value) {
|
|
81 |
7
| return value.length() == 3 && value.charAt(0) == '\"';
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
6
| public Object visit(ASTVariableDeclaratorId node, Object data) {
|
|
92 |
| |
|
93 |
6
| if (!node.getNameDeclaration().getTypeImage().equals(targetTypename())) {
|
|
94 |
0
| return data;
|
|
95 |
| } |
|
96 |
| |
|
97 |
6
| for (Iterator i = node.getUsages().iterator(); i.hasNext();) {
|
|
98 |
6
| NameOccurrence occ = (NameOccurrence) i.next();
|
|
99 |
6
| if (isNotedMethod(occ.getNameForWhichThisIsAQualifier())) {
|
|
100 |
6
| SimpleNode parent = (SimpleNode)occ.getLocation().jjtGetParent().jjtGetParent();
|
|
101 |
6
| if (parent instanceof ASTPrimaryExpression) {
|
|
102 |
| |
|
103 |
6
| List additives = parent.findChildrenOfType(ASTAdditiveExpression.class);
|
|
104 |
6
| if (!additives.isEmpty()) {
|
|
105 |
1
| return data;
|
|
106 |
| } |
|
107 |
5
| List literals = parent.findChildrenOfType(ASTLiteral.class);
|
|
108 |
5
| for (int l=0; l<literals.size(); l++) {
|
|
109 |
7
| ASTLiteral literal = (ASTLiteral)literals.get(l);
|
|
110 |
7
| if (isViolationArgument(l, literal.getImage())) {
|
|
111 |
3
| addViolation(data, occ.getLocation());
|
|
112 |
| } |
|
113 |
| } |
|
114 |
| } |
|
115 |
| } |
|
116 |
| } |
|
117 |
5
| return data;
|
|
118 |
| } |
|
119 |
| } |
|
120 |
| |