01: package org.acm.seguin.pmd.rules.design;
02:
03: import org.acm.seguin.pmd.AbstractRule;
04: import org.acm.seguin.pmd.RuleContext;
05: import net.sourceforge.jrefactory.ast.ASTClassDeclaration;
06: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
07: import net.sourceforge.jrefactory.ast.ASTConstructorDeclaration;
08: import net.sourceforge.jrefactory.ast.ASTMethodDeclaration;
09: import net.sourceforge.jrefactory.ast.ASTUnmodifiedClassDeclaration;
10:
11: public class UseSingletonRule extends AbstractRule {
12:
13: private boolean isOK;
14: private int methodCount;
15:
16: public Object visit(ASTConstructorDeclaration decl, Object data) {
17: if (decl.isPrivate()) {
18: isOK = true;
19: }
20: return data;
21: }
22:
23: public Object visit(ASTUnmodifiedClassDeclaration decl, Object data) {
24: if (decl.jjtGetParent() instanceof ASTClassDeclaration
25: && ((ASTClassDeclaration) decl.jjtGetParent())
26: .isAbstract()) {
27: isOK = true;
28: }
29: return super .visit(decl, data);
30: }
31:
32: public Object visit(ASTMethodDeclaration decl, Object data) {
33: methodCount++;
34:
35: if (!isOK && !decl.isStatic()) {
36: isOK = true;
37: }
38:
39: return data;
40: }
41:
42: public Object visit(ASTCompilationUnit cu, Object data) {
43: methodCount = 0;
44: isOK = false;
45: Object RC = cu.childrenAccept(this , data);
46:
47: if (!isOK && methodCount > 0) {
48: RuleContext ctx = (RuleContext) data;
49: ctx.getReport().addRuleViolation(
50: createRuleViolation(ctx, cu.getBeginLine()));
51: }
52:
53: return RC;
54: }
55: }
|