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