View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.RuleContext;
9   import net.sourceforge.pmd.lang.LanguageRegistry;
10  import net.sourceforge.pmd.lang.ast.Node;
11  import net.sourceforge.pmd.lang.java.JavaLanguageModule;
12  import net.sourceforge.pmd.lang.java.ast.*;
13  import net.sourceforge.pmd.lang.rule.AbstractRule;
14  import net.sourceforge.pmd.lang.rule.ImmutableLanguage;
15  
16  public abstract class AbstractJavaRule extends AbstractRule implements JavaParserVisitor, ImmutableLanguage {
17  
18      public AbstractJavaRule() {
19  	super.setLanguage(LanguageRegistry.getLanguage(JavaLanguageModule.NAME));
20  	// Enable Type Resolution on Java Rules by default
21  	super.setUsesTypeResolution();
22      }
23  
24      public void apply(List<? extends Node> nodes, RuleContext ctx) {
25  	visitAll(nodes, ctx);
26      }
27  
28      protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
29  	for (Object element : nodes) {
30  	    ASTCompilationUnit node = (ASTCompilationUnit) element;
31  	    visit(node, ctx);
32  	}
33      }
34  
35      /**
36       * Gets the Image of the first parent node of type
37       * ASTClassOrInterfaceDeclaration or <code>null</code>
38       *
39       * @param node
40       *            the node which will be searched
41       */
42      protected final String getDeclaringType(Node node) {
43  	ASTClassOrInterfaceDeclaration c = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
44  	if (c != null) {
45  	    return c.getImage();
46  	}
47  	return null;
48      }
49  
50      public static boolean isQualifiedName(Node node) {
51  	return node.getImage().indexOf('.') != -1;
52      }
53  
54      public static boolean importsPackage(ASTCompilationUnit node, String packageName) {
55  	List<ASTImportDeclaration> nodes = node.findChildrenOfType(ASTImportDeclaration.class);
56  	for (ASTImportDeclaration n : nodes) {
57  	    if (n.getPackageName().startsWith(packageName)) {
58  		return true;
59  	    }
60  	}
61  	return false;
62      }
63  
64      protected boolean isSuppressed(Node node) {
65          return JavaRuleViolation.isSupressed(node, this);
66      }
67  
68      //
69      // The following APIs are identical to those in JavaParserVisitorAdapter.
70      // Due to Java single inheritance, it preferred to extend from the more
71      // complex Rule base class instead of from relatively simple Visitor.
72      //
73      public Object visit(JavaNode node, Object data) {
74  	node.childrenAccept(this, data);
75  	return null;
76      }
77  
78      public Object visit(ASTExtendsList node, Object data) {
79  	return visit((JavaNode) node, data);
80      }
81  
82      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
83  	return visit((JavaNode) node, data);
84      }
85  
86      public Object visit(ASTImplementsList node, Object data) {
87  	return visit((JavaNode) node, data);
88      }
89  
90      public Object visit(ASTTypeParameters node, Object data) {
91  	return visit((JavaNode) node, data);
92      }
93  
94      public Object visit(ASTMemberSelector node, Object data) {
95  	return visit((JavaNode) node, data);
96      }
97  
98      public Object visit(ASTTypeParameter node, Object data) {
99  	return visit((JavaNode) node, data);
100     }
101 
102     public Object visit(ASTTypeBound node, Object data) {
103 	return visit((JavaNode) node, data);
104     }
105 
106     public Object visit(ASTClassOrInterfaceBody node, Object data) {
107 	return visit((JavaNode) node, data);
108     }
109 
110     public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
111 	return visit((JavaNode) node, data);
112     }
113 
114     public Object visit(ASTEnumBody node, Object data) {
115 	return visit((JavaNode) node, data);
116     }
117 
118     public Object visit(ASTEnumConstant node, Object data) {
119 	return visit((JavaNode) node, data);
120     }
121 
122     public Object visit(ASTReferenceType node, Object data) {
123 	return visit((JavaNode) node, data);
124     }
125 
126     public Object visit(ASTClassOrInterfaceType node, Object data) {
127 	return visit((JavaNode) node, data);
128     }
129 
130     public Object visit(ASTTypeArguments node, Object data) {
131 	return visit((JavaNode) node, data);
132     }
133 
134     public Object visit(ASTTypeArgument node, Object data) {
135 	return visit((JavaNode) node, data);
136     }
137 
138     public Object visit(ASTWildcardBounds node, Object data) {
139 	return visit((JavaNode) node, data);
140     }
141 
142     public Object visit(ASTAnnotation node, Object data) {
143 	return visit((JavaNode) node, data);
144     }
145 
146     public Object visit(ASTNormalAnnotation node, Object data) {
147 	return visit((JavaNode) node, data);
148     }
149 
150     public Object visit(ASTMarkerAnnotation node, Object data) {
151 	return visit((JavaNode) node, data);
152     }
153 
154     public Object visit(ASTSingleMemberAnnotation node, Object data) {
155 	return visit((JavaNode) node, data);
156     }
157 
158     public Object visit(ASTMemberValuePairs node, Object data) {
159 	return visit((JavaNode) node, data);
160     }
161 
162     public Object visit(ASTMemberValuePair node, Object data) {
163 	return visit((JavaNode) node, data);
164     }
165 
166     public Object visit(ASTMemberValue node, Object data) {
167 	return visit((JavaNode) node, data);
168     }
169 
170     public Object visit(ASTMemberValueArrayInitializer node, Object data) {
171 	return visit((JavaNode) node, data);
172     }
173 
174     public Object visit(ASTAnnotationTypeDeclaration node, Object data) {
175 	return visit((JavaNode) node, data);
176     }
177 
178     public Object visit(ASTAnnotationTypeBody node, Object data) {
179 	return visit((JavaNode) node, data);
180     }
181 
182     public Object visit(ASTAnnotationTypeMemberDeclaration node, Object data) {
183 	return visit((JavaNode) node, data);
184     }
185 
186     public Object visit(ASTAnnotationMethodDeclaration node, Object data) {
187 	return visit((JavaNode)node, data);
188     }
189 
190     public Object visit(ASTDefaultValue node, Object data) {
191 	return visit((JavaNode) node, data);
192     }
193 
194     public Object visit(ASTRUNSIGNEDSHIFT node, Object data) {
195 	return visit((JavaNode) node, data);
196     }
197 
198     public Object visit(ASTRSIGNEDSHIFT node, Object data) {
199 	return visit((JavaNode) node, data);
200     }
201 
202     public Object visit(ASTCompilationUnit node, Object data) {
203 	return visit((JavaNode) node, data);
204     }
205 
206     public Object visit(ASTEnumDeclaration node, Object data) {
207 	return visit((JavaNode) node, data);
208     }
209 
210     public Object visit(ASTAssertStatement node, Object data) {
211 	return visit((JavaNode) node, data);
212     }
213 
214     public Object visit(ASTPackageDeclaration node, Object data) {
215 	return visit((JavaNode) node, data);
216     }
217 
218     public Object visit(ASTImportDeclaration node, Object data) {
219 	return visit((JavaNode) node, data);
220     }
221 
222     public Object visit(ASTTypeDeclaration node, Object data) {
223 	return visit((JavaNode) node, data);
224     }
225 
226     public Object visit(ASTFieldDeclaration node, Object data) {
227 	return visit((JavaNode) node, data);
228     }
229 
230     public Object visit(ASTVariableDeclarator node, Object data) {
231 	return visit((JavaNode) node, data);
232     }
233 
234     public Object visit(ASTVariableDeclaratorId node, Object data) {
235 	return visit((JavaNode) node, data);
236     }
237 
238     public Object visit(ASTVariableInitializer node, Object data) {
239 	return visit((JavaNode) node, data);
240     }
241 
242     public Object visit(ASTArrayInitializer node, Object data) {
243 	return visit((JavaNode) node, data);
244     }
245 
246     public Object visit(ASTMethodDeclaration node, Object data) {
247 	return visit((JavaNode) node, data);
248     }
249 
250     public Object visit(ASTMethodDeclarator node, Object data) {
251 	return visit((JavaNode) node, data);
252     }
253 
254     public Object visit(ASTFormalParameters node, Object data) {
255 	return visit((JavaNode) node, data);
256     }
257 
258     public Object visit(ASTFormalParameter node, Object data) {
259 	return visit((JavaNode) node, data);
260     }
261 
262     public Object visit(ASTConstructorDeclaration node, Object data) {
263 	return visit((JavaNode) node, data);
264     }
265 
266     public Object visit(ASTExplicitConstructorInvocation node, Object data) {
267 	return visit((JavaNode) node, data);
268     }
269 
270     public Object visit(ASTInitializer node, Object data) {
271 	return visit((JavaNode) node, data);
272     }
273 
274     public Object visit(ASTType node, Object data) {
275 	return visit((JavaNode) node, data);
276     }
277 
278     public Object visit(ASTPrimitiveType node, Object data) {
279 	return visit((JavaNode) node, data);
280     }
281 
282     public Object visit(ASTResultType node, Object data) {
283 	return visit((JavaNode) node, data);
284     }
285 
286     public Object visit(ASTName node, Object data) {
287 	return visit((JavaNode) node, data);
288     }
289 
290     public Object visit(ASTNameList node, Object data) {
291 	return visit((JavaNode) node, data);
292     }
293 
294     public Object visit(ASTExpression node, Object data) {
295 	return visit((JavaNode) node, data);
296     }
297 
298     public Object visit(ASTAssignmentOperator node, Object data) {
299 	return visit((JavaNode) node, data);
300     }
301 
302     public Object visit(ASTConditionalExpression node, Object data) {
303 	return visit((JavaNode) node, data);
304     }
305 
306     public Object visit(ASTConditionalOrExpression node, Object data) {
307 	return visit((JavaNode) node, data);
308     }
309 
310     public Object visit(ASTConditionalAndExpression node, Object data) {
311 	return visit((JavaNode) node, data);
312     }
313 
314     public Object visit(ASTInclusiveOrExpression node, Object data) {
315 	return visit((JavaNode) node, data);
316     }
317 
318     public Object visit(ASTExclusiveOrExpression node, Object data) {
319 	return visit((JavaNode) node, data);
320     }
321 
322     public Object visit(ASTAndExpression node, Object data) {
323 	return visit((JavaNode) node, data);
324     }
325 
326     public Object visit(ASTEqualityExpression node, Object data) {
327 	return visit((JavaNode) node, data);
328     }
329 
330     public Object visit(ASTInstanceOfExpression node, Object data) {
331 	return visit((JavaNode) node, data);
332     }
333 
334     public Object visit(ASTRelationalExpression node, Object data) {
335 	return visit((JavaNode) node, data);
336     }
337 
338     public Object visit(ASTShiftExpression node, Object data) {
339 	return visit((JavaNode) node, data);
340     }
341 
342     public Object visit(ASTAdditiveExpression node, Object data) {
343 	return visit((JavaNode) node, data);
344     }
345 
346     public Object visit(ASTMultiplicativeExpression node, Object data) {
347 	return visit((JavaNode) node, data);
348     }
349 
350     public Object visit(ASTUnaryExpression node, Object data) {
351 	return visit((JavaNode) node, data);
352     }
353 
354     public Object visit(ASTPreIncrementExpression node, Object data) {
355 	return visit((JavaNode) node, data);
356     }
357 
358     public Object visit(ASTPreDecrementExpression node, Object data) {
359 	return visit((JavaNode) node, data);
360     }
361 
362     public Object visit(ASTUnaryExpressionNotPlusMinus node, Object data) {
363 	return visit((JavaNode) node, data);
364     }
365 
366     public Object visit(ASTPostfixExpression node, Object data) {
367 	return visit((JavaNode) node, data);
368     }
369 
370     public Object visit(ASTCastExpression node, Object data) {
371 	return visit((JavaNode) node, data);
372     }
373 
374     public Object visit(ASTPrimaryExpression node, Object data) {
375 	return visit((JavaNode) node, data);
376     }
377 
378     public Object visit(ASTPrimaryPrefix node, Object data) {
379 	return visit((JavaNode) node, data);
380     }
381 
382     public Object visit(ASTPrimarySuffix node, Object data) {
383 	return visit((JavaNode) node, data);
384     }
385 
386     public Object visit(ASTLiteral node, Object data) {
387 	return visit((JavaNode) node, data);
388     }
389 
390     public Object visit(ASTBooleanLiteral node, Object data) {
391 	return visit((JavaNode) node, data);
392     }
393 
394     public Object visit(ASTNullLiteral node, Object data) {
395 	return visit((JavaNode) node, data);
396     }
397 
398     public Object visit(ASTArguments node, Object data) {
399 	return visit((JavaNode) node, data);
400     }
401 
402     public Object visit(ASTArgumentList node, Object data) {
403 	return visit((JavaNode) node, data);
404     }
405 
406     public Object visit(ASTAllocationExpression node, Object data) {
407 	return visit((JavaNode) node, data);
408     }
409 
410     public Object visit(ASTArrayDimsAndInits node, Object data) {
411 	return visit((JavaNode) node, data);
412     }
413 
414     public Object visit(ASTStatement node, Object data) {
415 	return visit((JavaNode) node, data);
416     }
417 
418     public Object visit(ASTLabeledStatement node, Object data) {
419 	return visit((JavaNode) node, data);
420     }
421 
422     public Object visit(ASTBlock node, Object data) {
423 	return visit((JavaNode) node, data);
424     }
425 
426     public Object visit(ASTBlockStatement node, Object data) {
427 	return visit((JavaNode) node, data);
428     }
429 
430     public Object visit(ASTLocalVariableDeclaration node, Object data) {
431 	return visit((JavaNode) node, data);
432     }
433 
434     public Object visit(ASTEmptyStatement node, Object data) {
435 	return visit((JavaNode) node, data);
436     }
437 
438     public Object visit(ASTStatementExpression node, Object data) {
439 	return visit((JavaNode) node, data);
440     }
441 
442     public Object visit(ASTSwitchStatement node, Object data) {
443 	return visit((JavaNode) node, data);
444     }
445 
446     public Object visit(ASTSwitchLabel node, Object data) {
447 	return visit((JavaNode) node, data);
448     }
449 
450     public Object visit(ASTIfStatement node, Object data) {
451 	return visit((JavaNode) node, data);
452     }
453 
454     public Object visit(ASTWhileStatement node, Object data) {
455 	return visit((JavaNode) node, data);
456     }
457 
458     public Object visit(ASTDoStatement node, Object data) {
459 	return visit((JavaNode) node, data);
460     }
461 
462     public Object visit(ASTForStatement node, Object data) {
463 	return visit((JavaNode) node, data);
464     }
465 
466     public Object visit(ASTForInit node, Object data) {
467 	return visit((JavaNode) node, data);
468     }
469 
470     public Object visit(ASTStatementExpressionList node, Object data) {
471 	return visit((JavaNode) node, data);
472     }
473 
474     public Object visit(ASTForUpdate node, Object data) {
475 	return visit((JavaNode) node, data);
476     }
477 
478     public Object visit(ASTBreakStatement node, Object data) {
479 	return visit((JavaNode) node, data);
480     }
481 
482     public Object visit(ASTContinueStatement node, Object data) {
483 	return visit((JavaNode) node, data);
484     }
485 
486     public Object visit(ASTReturnStatement node, Object data) {
487 	return visit((JavaNode) node, data);
488     }
489 
490     public Object visit(ASTThrowStatement node, Object data) {
491 	return visit((JavaNode) node, data);
492     }
493 
494     public Object visit(ASTSynchronizedStatement node, Object data) {
495 	return visit((JavaNode) node, data);
496     }
497 
498     public Object visit(ASTTryStatement node, Object data) {
499 	return visit((JavaNode) node, data);
500     }
501 
502     public Object visit(ASTFinallyStatement node, Object data) {
503 	return visit((JavaNode) node, data);
504     }
505 
506     public Object visit(ASTCatchStatement node, Object data) {
507 	return visit((JavaNode) node, data);
508     }
509 
510     public Object visit(ASTResourceSpecification node, Object data) {
511          return visit((JavaNode)node, data);
512     }
513 
514     public Object visit(ASTResources node, Object data) {
515          return visit((JavaNode)node, data);
516     }
517 
518     public Object visit(ASTResource node, Object data) {
519          return visit((JavaNode)node, data);
520     }
521 
522     public Object visit(ASTLambdaExpression node, Object data) {
523         return visit((JavaNode)node, data);
524     }
525 
526     public Object visit(ASTMethodReference node, Object data) {
527         return visit((JavaNode)node, data);
528     }
529 }