1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules.strings; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.ast.ASTAdditiveExpression; |
8 |
| import net.sourceforge.pmd.ast.ASTArgumentList; |
9 |
| import net.sourceforge.pmd.ast.ASTDoStatement; |
10 |
| import net.sourceforge.pmd.ast.ASTForStatement; |
11 |
| import net.sourceforge.pmd.ast.ASTIfStatement; |
12 |
| import net.sourceforge.pmd.ast.ASTLiteral; |
13 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
14 |
| import net.sourceforge.pmd.ast.ASTName; |
15 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
16 |
| import net.sourceforge.pmd.ast.ASTPrimarySuffix; |
17 |
| import net.sourceforge.pmd.ast.ASTSwitchLabel; |
18 |
| import net.sourceforge.pmd.ast.ASTSwitchStatement; |
19 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
20 |
| import net.sourceforge.pmd.ast.ASTWhileStatement; |
21 |
| import net.sourceforge.pmd.ast.Node; |
22 |
| import net.sourceforge.pmd.ast.SimpleNode; |
23 |
| import net.sourceforge.pmd.symboltable.NameOccurrence; |
24 |
| |
25 |
| import java.util.HashSet; |
26 |
| import java.util.Iterator; |
27 |
| import java.util.List; |
28 |
| import java.util.Map; |
29 |
| import java.util.Set; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| public class ConsecutiveLiteralAppends extends AbstractRule { |
55 |
| |
56 |
| private final static Set blockParents; |
57 |
| |
58 |
| static { |
59 |
12
| blockParents = new HashSet();
|
60 |
12
| blockParents.add(ASTForStatement.class);
|
61 |
12
| blockParents.add(ASTWhileStatement.class);
|
62 |
12
| blockParents.add(ASTDoStatement.class);
|
63 |
12
| blockParents.add(ASTIfStatement.class);
|
64 |
12
| blockParents.add(ASTSwitchStatement.class);
|
65 |
12
| blockParents.add(ASTMethodDeclaration.class);
|
66 |
| } |
67 |
| |
68 |
| private int threshold = 1; |
69 |
| |
70 |
78
| public Object visit(ASTVariableDeclaratorId node, Object data) {
|
71 |
| |
72 |
78
| if (!isStringBuffer(node)) {
|
73 |
35
| return data;
|
74 |
| } |
75 |
43
| threshold = getIntProperty("threshold");
|
76 |
| |
77 |
43
| int concurrentCount = checkConstructor(node, data);
|
78 |
43
| Node lastBlock = getFirstParentBlock(node);
|
79 |
43
| Node currentBlock = lastBlock;
|
80 |
43
| Map decls = node.getScope().getVariableDeclarations();
|
81 |
43
| SimpleNode rootNode = null;
|
82 |
| |
83 |
43
| if (concurrentCount == 1) {
|
84 |
2
| rootNode = node;
|
85 |
| } |
86 |
43
| for (Iterator iter = decls.entrySet().iterator(); iter.hasNext();) {
|
87 |
58
| Map.Entry entry = (Map.Entry) iter.next();
|
88 |
58
| List decl = (List) entry.getValue();
|
89 |
58
| for (int ix = 0; ix < decl.size(); ix++) {
|
90 |
141
| NameOccurrence no = (NameOccurrence) decl.get(ix);
|
91 |
141
| SimpleNode n = no.getLocation();
|
92 |
| |
93 |
141
| currentBlock = getFirstParentBlock(n);
|
94 |
| |
95 |
141
| if (!InefficientStringBuffering.isInStringBufferAppend(n, 3)) {
|
96 |
26
| if (!no.isPartOfQualifiedName()) {
|
97 |
14
| checkForViolation(rootNode, data, concurrentCount);
|
98 |
14
| concurrentCount = 0;
|
99 |
| } |
100 |
26
| continue;
|
101 |
| } |
102 |
115
| ASTPrimaryExpression s = (ASTPrimaryExpression) n
|
103 |
| .getFirstParentOfType(ASTPrimaryExpression.class); |
104 |
115
| int numChildren = s.jjtGetNumChildren();
|
105 |
115
| for (int jx = 0; jx < numChildren; jx++) {
|
106 |
246
| SimpleNode sn = (SimpleNode) s.jjtGetChild(jx);
|
107 |
246
| if (!(sn instanceof ASTPrimarySuffix)
|
108 |
| || sn.getImage() != null) { |
109 |
123
| continue;
|
110 |
| } |
111 |
| |
112 |
| |
113 |
123
| if ((currentBlock != null && lastBlock != null && !currentBlock
|
114 |
| .equals(lastBlock)) |
115 |
| || (currentBlock == null ^ lastBlock == null)) { |
116 |
46
| checkForViolation(rootNode, data, concurrentCount);
|
117 |
46
| concurrentCount = 0;
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
123
| if (concurrentCount == 0) {
|
123 |
93
| rootNode = sn;
|
124 |
| } |
125 |
123
| if (isAdditive(sn)) {
|
126 |
7
| concurrentCount = processAdditive(data,
|
127 |
| concurrentCount, sn, rootNode); |
128 |
7
| if (concurrentCount != 0) {
|
129 |
4
| rootNode = sn;
|
130 |
| } |
131 |
116
| } else if (!isAppendingStringLiteral(sn)) {
|
132 |
13
| checkForViolation(rootNode, data, concurrentCount);
|
133 |
13
| concurrentCount = 0;
|
134 |
| } else { |
135 |
103
| concurrentCount++;
|
136 |
| } |
137 |
123
| lastBlock = currentBlock;
|
138 |
| } |
139 |
| } |
140 |
| } |
141 |
43
| checkForViolation(rootNode, data, concurrentCount);
|
142 |
43
| return data;
|
143 |
| } |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
43
| private int checkConstructor(ASTVariableDeclaratorId node, Object data) {
|
152 |
43
| Node parent = node.jjtGetParent();
|
153 |
43
| if (parent.jjtGetNumChildren() >= 2) {
|
154 |
43
| ASTArgumentList list = (ASTArgumentList) ((SimpleNode) parent
|
155 |
| .jjtGetChild(1)).getFirstChildOfType(ASTArgumentList.class); |
156 |
43
| if (list != null) {
|
157 |
4
| ASTLiteral literal = (ASTLiteral) list
|
158 |
| .getFirstChildOfType(ASTLiteral.class); |
159 |
4
| if (!isAdditive(list) && literal != null
|
160 |
| && literal.isStringLiteral()) { |
161 |
1
| return 1;
|
162 |
| } |
163 |
3
| return processAdditive(data, 0, list, node);
|
164 |
| } |
165 |
| } |
166 |
39
| return 0;
|
167 |
| } |
168 |
| |
169 |
10
| private int processAdditive(Object data, int concurrentCount,
|
170 |
| SimpleNode sn, SimpleNode rootNode) { |
171 |
10
| ASTAdditiveExpression additive = (ASTAdditiveExpression) sn
|
172 |
| .getFirstChildOfType(ASTAdditiveExpression.class); |
173 |
10
| if (additive == null) {
|
174 |
1
| return 0;
|
175 |
| } |
176 |
9
| int count = concurrentCount;
|
177 |
9
| boolean found = false;
|
178 |
9
| for (int ix = 0; ix < additive.jjtGetNumChildren(); ix++) {
|
179 |
20
| SimpleNode childNode = (SimpleNode) additive.jjtGetChild(ix);
|
180 |
20
| if (childNode.jjtGetNumChildren() != 1
|
181 |
| || childNode.findChildrenOfType(ASTName.class).size() != 0) { |
182 |
7
| if (!found) {
|
183 |
7
| checkForViolation(rootNode, data, count);
|
184 |
7
| found = true;
|
185 |
| } |
186 |
7
| count = 0;
|
187 |
| } else { |
188 |
13
| count++;
|
189 |
| } |
190 |
| } |
191 |
| |
192 |
| |
193 |
| |
194 |
9
| if (!found) {
|
195 |
2
| count = 1;
|
196 |
| } |
197 |
| |
198 |
9
| return count;
|
199 |
| } |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
127
| private boolean isAdditive(SimpleNode n) {
|
213 |
127
| List lstAdditive = n.findChildrenOfType(ASTAdditiveExpression.class);
|
214 |
127
| if (lstAdditive.size() == 0) {
|
215 |
116
| return false;
|
216 |
| } |
217 |
| |
218 |
| |
219 |
| |
220 |
11
| for (int ix = 0; ix < lstAdditive.size(); ix++) {
|
221 |
11
| ASTAdditiveExpression expr = (ASTAdditiveExpression) lstAdditive.get(ix);
|
222 |
11
| if (expr.getParentsOfType(ASTArgumentList.class).size() != 1) {
|
223 |
2
| return false;
|
224 |
| } |
225 |
| } |
226 |
9
| return true;
|
227 |
| } |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
184
| private Node getFirstParentBlock(Node node) {
|
238 |
184
| Node parentNode = node.jjtGetParent();
|
239 |
| |
240 |
184
| Node lastNode = node;
|
241 |
184
| while (parentNode != null
|
242 |
| && !blockParents.contains(parentNode.getClass())) { |
243 |
1139
| lastNode = parentNode;
|
244 |
1139
| parentNode = parentNode.jjtGetParent();
|
245 |
| } |
246 |
184
| if (parentNode != null
|
247 |
| && parentNode.getClass().equals(ASTIfStatement.class)) { |
248 |
17
| parentNode = lastNode;
|
249 |
167
| } else if (parentNode != null
|
250 |
| && parentNode.getClass().equals(ASTSwitchStatement.class)) { |
251 |
10
| parentNode = getSwitchParent(parentNode, lastNode);
|
252 |
| } |
253 |
184
| return parentNode;
|
254 |
| } |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
10
| private Node getSwitchParent(Node parentNode, Node lastNode) {
|
264 |
10
| int allChildren = parentNode.jjtGetNumChildren();
|
265 |
10
| ASTSwitchLabel label = null;
|
266 |
103
| for (int ix = 0; ix < allChildren; ix++) {
|
267 |
103
| Node n = parentNode.jjtGetChild(ix);
|
268 |
103
| if (n.getClass().equals(ASTSwitchLabel.class)) {
|
269 |
36
| label = (ASTSwitchLabel) n;
|
270 |
67
| } else if (n.equals(lastNode)) {
|
271 |
10
| parentNode = label;
|
272 |
10
| break;
|
273 |
| } |
274 |
| } |
275 |
10
| return parentNode;
|
276 |
| } |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
123
| private void checkForViolation(SimpleNode node, Object data,
|
283 |
| int concurrentCount) { |
284 |
123
| if (concurrentCount > threshold) {
|
285 |
20
| String[] param = {String.valueOf(concurrentCount)};
|
286 |
20
| addViolation(data, node, param);
|
287 |
| } |
288 |
| } |
289 |
| |
290 |
116
| private boolean isAppendingStringLiteral(SimpleNode node) {
|
291 |
116
| SimpleNode n = node;
|
292 |
116
| while (n.jjtGetNumChildren() != 0
|
293 |
| && !n.getClass().equals(ASTLiteral.class)) { |
294 |
696
| n = (SimpleNode) n.jjtGetChild(0);
|
295 |
| } |
296 |
116
| return n.getClass().equals(ASTLiteral.class);
|
297 |
| } |
298 |
| |
299 |
78
| private static boolean isStringBuffer(ASTVariableDeclaratorId node) {
|
300 |
78
| SimpleNode nn = (SimpleNode) node.getTypeNameNode();
|
301 |
78
| if (nn.jjtGetNumChildren() == 0) {
|
302 |
9
| return false;
|
303 |
| } |
304 |
69
| return "StringBuffer".equals(((SimpleNode) nn.jjtGetChild(0))
|
305 |
| .getImage()); |
306 |
| } |
307 |
| |
308 |
| } |