View Javadoc

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