001: package net.sourceforge.pmd;
002:
003: import java.text.MessageFormat;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import net.sourceforge.pmd.ast.*;
008:
009: // FUTURE Move all Java centric code into 'java' package instead of core PMD class. Makes sense when we add non-Java languages.
010: public abstract class AbstractJavaRule extends CommonAbstractRule
011: implements JavaParserVisitor {
012:
013: public AbstractJavaRule() {
014: // Enable Type Resolution on Java Rules by default
015: super .setUsesTypeResolution();
016: }
017:
018: /**
019: * Adds a violation to the report.
020: *
021: * @param data
022: * the RuleContext
023: * @param node
024: * the node that produces the violation
025: */
026: protected final void addViolation(Object data, SimpleNode node) {
027: RuleContext ctx = (RuleContext) data;
028: ctx.getReport().addRuleViolation(
029: new RuleViolation(this , ctx, node));
030: }
031:
032: /**
033: * Adds a violation to the report.
034: *
035: * @param data
036: * the RuleContext
037: * @param node
038: * the node that produces the violation
039: * @param msg
040: * specific message to put in the report
041: */
042: protected final void addViolationWithMessage(Object data,
043: SimpleNode node, String msg) {
044: RuleContext ctx = (RuleContext) data;
045: ctx.getReport().addRuleViolation(
046: new RuleViolation(this , ctx, node, msg));
047: }
048:
049: /**
050: * Adds a violation to the report.
051: *
052: * @param data
053: * the RuleContext
054: * @param node
055: * the node that produces the violation
056: * @param embed
057: * a variable to embed in the rule violation message
058: */
059: protected final void addViolation(Object data, SimpleNode node,
060: String embed) {
061: RuleContext ctx = (RuleContext) data;
062: ctx.getReport().addRuleViolation(
063: new RuleViolation(this , ctx, node, MessageFormat
064: .format(getMessage(), embed)));
065: }
066:
067: /**
068: * Adds a violation to the report.
069: *
070: * @param data
071: * the RuleContext
072: * @param node
073: * the node that produces the violation, may be null, in which
074: * case all line and column info will be set to zero
075: * @param args
076: * objects to embed in the rule violation message
077: */
078: protected final void addViolation(Object data, Node node,
079: Object[] args) {
080: RuleContext ctx = (RuleContext) data;
081: ctx.getReport().addRuleViolation(
082: new RuleViolation(this , ctx, (SimpleNode) node,
083: MessageFormat.format(getMessage(), args)));
084: }
085:
086: public void apply(List acus, RuleContext ctx) {
087: visitAll(acus, ctx);
088: }
089:
090: protected void visitAll(List acus, RuleContext ctx) {
091: for (Iterator i = acus.iterator(); i.hasNext();) {
092: ASTCompilationUnit node = (ASTCompilationUnit) i.next();
093: visit(node, ctx);
094: }
095: }
096:
097: /**
098: * Gets the Image of the first parent node of type
099: * 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,
172: Object data) {
173: return visit((SimpleJavaNode) node, data);
174: }
175:
176: public Object visit(ASTEnumBody node, Object data) {
177: return visit((SimpleJavaNode) node, data);
178: }
179:
180: public Object visit(ASTEnumConstant node, Object data) {
181: return visit((SimpleJavaNode) node, data);
182: }
183:
184: public Object visit(ASTReferenceType node, Object data) {
185: return visit((SimpleJavaNode) node, data);
186: }
187:
188: public Object visit(ASTClassOrInterfaceType node, Object data) {
189: return visit((SimpleJavaNode) node, data);
190: }
191:
192: public Object visit(ASTTypeArguments node, Object data) {
193: return visit((SimpleJavaNode) node, data);
194: }
195:
196: public Object visit(ASTTypeArgument node, Object data) {
197: return visit((SimpleJavaNode) node, data);
198: }
199:
200: public Object visit(ASTWildcardBounds node, Object data) {
201: return visit((SimpleJavaNode) node, data);
202: }
203:
204: public Object visit(ASTAnnotation node, Object data) {
205: return visit((SimpleJavaNode) node, data);
206: }
207:
208: public Object visit(ASTNormalAnnotation node, Object data) {
209: return visit((SimpleJavaNode) node, data);
210: }
211:
212: public Object visit(ASTMarkerAnnotation node, Object data) {
213: return visit((SimpleJavaNode) node, data);
214: }
215:
216: public Object visit(ASTSingleMemberAnnotation node, Object data) {
217: return visit((SimpleJavaNode) node, data);
218: }
219:
220: public Object visit(ASTMemberValuePairs node, Object data) {
221: return visit((SimpleJavaNode) node, data);
222: }
223:
224: public Object visit(ASTMemberValuePair node, Object data) {
225: return visit((SimpleJavaNode) node, data);
226: }
227:
228: public Object visit(ASTMemberValue node, Object data) {
229: return visit((SimpleJavaNode) node, data);
230: }
231:
232: public Object visit(ASTMemberValueArrayInitializer node, Object data) {
233: return visit((SimpleJavaNode) node, data);
234: }
235:
236: public Object visit(ASTAnnotationTypeDeclaration node, Object data) {
237: return visit((SimpleJavaNode) node, data);
238: }
239:
240: public Object visit(ASTAnnotationTypeBody node, Object data) {
241: return visit((SimpleJavaNode) node, data);
242: }
243:
244: public Object visit(ASTAnnotationTypeMemberDeclaration node,
245: Object data) {
246: return visit((SimpleJavaNode) node, data);
247: }
248:
249: public Object visit(ASTDefaultValue node, Object data) {
250: return visit((SimpleJavaNode) node, data);
251: }
252:
253: public Object visit(ASTRUNSIGNEDSHIFT node, Object data) {
254: return visit((SimpleJavaNode) node, data);
255: }
256:
257: public Object visit(ASTRSIGNEDSHIFT node, Object data) {
258: return visit((SimpleJavaNode) node, data);
259: }
260:
261: public Object visit(ASTCompilationUnit node, Object data) {
262: return visit((SimpleJavaNode) node, data);
263: }
264:
265: public Object visit(ASTEnumDeclaration node, Object data) {
266: return visit((SimpleJavaNode) node, data);
267: }
268:
269: public Object visit(ASTAssertStatement node, Object data) {
270: return visit((SimpleJavaNode) node, data);
271: }
272:
273: public Object visit(ASTPackageDeclaration node, Object data) {
274: return visit((SimpleJavaNode) node, data);
275: }
276:
277: public Object visit(ASTImportDeclaration node, Object data) {
278: return visit((SimpleJavaNode) node, data);
279: }
280:
281: public Object visit(ASTTypeDeclaration node, Object data) {
282: return visit((SimpleJavaNode) node, data);
283: }
284:
285: public Object visit(ASTFieldDeclaration node, Object data) {
286: return visit((SimpleJavaNode) node, data);
287: }
288:
289: public Object visit(ASTVariableDeclarator node, Object data) {
290: return visit((SimpleJavaNode) node, data);
291: }
292:
293: public Object visit(ASTVariableDeclaratorId node, Object data) {
294: return visit((SimpleJavaNode) node, data);
295: }
296:
297: public Object visit(ASTVariableInitializer node, Object data) {
298: return visit((SimpleJavaNode) node, data);
299: }
300:
301: public Object visit(ASTArrayInitializer node, Object data) {
302: return visit((SimpleJavaNode) node, data);
303: }
304:
305: public Object visit(ASTMethodDeclaration node, Object data) {
306: return visit((SimpleJavaNode) node, data);
307: }
308:
309: public Object visit(ASTMethodDeclarator node, Object data) {
310: return visit((SimpleJavaNode) node, data);
311: }
312:
313: public Object visit(ASTFormalParameters node, Object data) {
314: return visit((SimpleJavaNode) node, data);
315: }
316:
317: public Object visit(ASTFormalParameter node, Object data) {
318: return visit((SimpleJavaNode) node, data);
319: }
320:
321: public Object visit(ASTConstructorDeclaration node, Object data) {
322: return visit((SimpleJavaNode) node, data);
323: }
324:
325: public Object visit(ASTExplicitConstructorInvocation node,
326: Object data) {
327: return visit((SimpleJavaNode) node, data);
328: }
329:
330: public Object visit(ASTInitializer node, Object data) {
331: return visit((SimpleJavaNode) node, data);
332: }
333:
334: public Object visit(ASTType node, Object data) {
335: return visit((SimpleJavaNode) node, data);
336: }
337:
338: public Object visit(ASTPrimitiveType node, Object data) {
339: return visit((SimpleJavaNode) node, data);
340: }
341:
342: public Object visit(ASTResultType node, Object data) {
343: return visit((SimpleJavaNode) node, data);
344: }
345:
346: public Object visit(ASTName node, Object data) {
347: return visit((SimpleJavaNode) node, data);
348: }
349:
350: public Object visit(ASTNameList node, Object data) {
351: return visit((SimpleJavaNode) node, data);
352: }
353:
354: public Object visit(ASTExpression node, Object data) {
355: return visit((SimpleJavaNode) node, data);
356: }
357:
358: public Object visit(ASTAssignmentOperator node, Object data) {
359: return visit((SimpleJavaNode) node, data);
360: }
361:
362: public Object visit(ASTConditionalExpression node, Object data) {
363: return visit((SimpleJavaNode) node, data);
364: }
365:
366: public Object visit(ASTConditionalOrExpression node, Object data) {
367: return visit((SimpleJavaNode) node, data);
368: }
369:
370: public Object visit(ASTConditionalAndExpression node, Object data) {
371: return visit((SimpleJavaNode) node, data);
372: }
373:
374: public Object visit(ASTInclusiveOrExpression node, Object data) {
375: return visit((SimpleJavaNode) node, data);
376: }
377:
378: public Object visit(ASTExclusiveOrExpression node, Object data) {
379: return visit((SimpleJavaNode) node, data);
380: }
381:
382: public Object visit(ASTAndExpression node, Object data) {
383: return visit((SimpleJavaNode) node, data);
384: }
385:
386: public Object visit(ASTEqualityExpression node, Object data) {
387: return visit((SimpleJavaNode) node, data);
388: }
389:
390: public Object visit(ASTInstanceOfExpression node, Object data) {
391: return visit((SimpleJavaNode) node, data);
392: }
393:
394: public Object visit(ASTRelationalExpression node, Object data) {
395: return visit((SimpleJavaNode) node, data);
396: }
397:
398: public Object visit(ASTShiftExpression node, Object data) {
399: return visit((SimpleJavaNode) node, data);
400: }
401:
402: public Object visit(ASTAdditiveExpression node, Object data) {
403: return visit((SimpleJavaNode) node, data);
404: }
405:
406: public Object visit(ASTMultiplicativeExpression node, Object data) {
407: return visit((SimpleJavaNode) node, data);
408: }
409:
410: public Object visit(ASTUnaryExpression node, Object data) {
411: return visit((SimpleJavaNode) node, data);
412: }
413:
414: public Object visit(ASTPreIncrementExpression node, Object data) {
415: return visit((SimpleJavaNode) node, data);
416: }
417:
418: public Object visit(ASTPreDecrementExpression node, Object data) {
419: return visit((SimpleJavaNode) node, data);
420: }
421:
422: public Object visit(ASTUnaryExpressionNotPlusMinus node, Object data) {
423: return visit((SimpleJavaNode) node, data);
424: }
425:
426: public Object visit(ASTPostfixExpression node, Object data) {
427: return visit((SimpleJavaNode) node, data);
428: }
429:
430: public Object visit(ASTCastExpression node, Object data) {
431: return visit((SimpleJavaNode) node, data);
432: }
433:
434: public Object visit(ASTPrimaryExpression node, Object data) {
435: return visit((SimpleJavaNode) node, data);
436: }
437:
438: public Object visit(ASTPrimaryPrefix node, Object data) {
439: return visit((SimpleJavaNode) node, data);
440: }
441:
442: public Object visit(ASTPrimarySuffix node, Object data) {
443: return visit((SimpleJavaNode) node, data);
444: }
445:
446: public Object visit(ASTLiteral node, Object data) {
447: return visit((SimpleJavaNode) node, data);
448: }
449:
450: public Object visit(ASTBooleanLiteral node, Object data) {
451: return visit((SimpleJavaNode) node, data);
452: }
453:
454: public Object visit(ASTNullLiteral node, Object data) {
455: return visit((SimpleJavaNode) node, data);
456: }
457:
458: public Object visit(ASTArguments node, Object data) {
459: return visit((SimpleJavaNode) node, data);
460: }
461:
462: public Object visit(ASTArgumentList node, Object data) {
463: return visit((SimpleJavaNode) node, data);
464: }
465:
466: public Object visit(ASTAllocationExpression node, Object data) {
467: return visit((SimpleJavaNode) node, data);
468: }
469:
470: public Object visit(ASTArrayDimsAndInits node, Object data) {
471: return visit((SimpleJavaNode) node, data);
472: }
473:
474: public Object visit(ASTStatement node, Object data) {
475: return visit((SimpleJavaNode) node, data);
476: }
477:
478: public Object visit(ASTLabeledStatement node, Object data) {
479: return visit((SimpleJavaNode) node, data);
480: }
481:
482: public Object visit(ASTBlock node, Object data) {
483: return visit((SimpleJavaNode) node, data);
484: }
485:
486: public Object visit(ASTBlockStatement node, Object data) {
487: return visit((SimpleJavaNode) node, data);
488: }
489:
490: public Object visit(ASTLocalVariableDeclaration node, Object data) {
491: return visit((SimpleJavaNode) node, data);
492: }
493:
494: public Object visit(ASTEmptyStatement node, Object data) {
495: return visit((SimpleJavaNode) node, data);
496: }
497:
498: public Object visit(ASTStatementExpression node, Object data) {
499: return visit((SimpleJavaNode) node, data);
500: }
501:
502: public Object visit(ASTSwitchStatement node, Object data) {
503: return visit((SimpleJavaNode) node, data);
504: }
505:
506: public Object visit(ASTSwitchLabel node, Object data) {
507: return visit((SimpleJavaNode) node, data);
508: }
509:
510: public Object visit(ASTIfStatement node, Object data) {
511: return visit((SimpleJavaNode) node, data);
512: }
513:
514: public Object visit(ASTWhileStatement node, Object data) {
515: return visit((SimpleJavaNode) node, data);
516: }
517:
518: public Object visit(ASTDoStatement node, Object data) {
519: return visit((SimpleJavaNode) node, data);
520: }
521:
522: public Object visit(ASTForStatement node, Object data) {
523: return visit((SimpleJavaNode) node, data);
524: }
525:
526: public Object visit(ASTForInit node, Object data) {
527: return visit((SimpleJavaNode) node, data);
528: }
529:
530: public Object visit(ASTStatementExpressionList node, Object data) {
531: return visit((SimpleJavaNode) node, data);
532: }
533:
534: public Object visit(ASTForUpdate node, Object data) {
535: return visit((SimpleJavaNode) node, data);
536: }
537:
538: public Object visit(ASTBreakStatement node, Object data) {
539: return visit((SimpleJavaNode) node, data);
540: }
541:
542: public Object visit(ASTContinueStatement node, Object data) {
543: return visit((SimpleJavaNode) node, data);
544: }
545:
546: public Object visit(ASTReturnStatement node, Object data) {
547: return visit((SimpleJavaNode) node, data);
548: }
549:
550: public Object visit(ASTThrowStatement node, Object data) {
551: return visit((SimpleJavaNode) node, data);
552: }
553:
554: public Object visit(ASTSynchronizedStatement node, Object data) {
555: return visit((SimpleJavaNode) node, data);
556: }
557:
558: public Object visit(ASTTryStatement node, Object data) {
559: return visit((SimpleJavaNode) node, data);
560: }
561:
562: public Object visit(ASTFinallyStatement node, Object data) {
563: return visit((SimpleJavaNode) node, data);
564: }
565:
566: public Object visit(ASTCatchStatement node, Object data) {
567: return visit((SimpleJavaNode) node, data);
568: }
569: }
|