1 package net.sourceforge.pmd.rules.design;
2
3 import net.sourceforge.pmd.AbstractRule;
4 import net.sourceforge.pmd.RuleContext;
5 import net.sourceforge.pmd.ast.ASTClassDeclaration;
6 import net.sourceforge.pmd.ast.ASTCompilationUnit;
7 import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
8 import net.sourceforge.pmd.ast.ASTMethodDeclaration;
9 import net.sourceforge.pmd.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 && ((ASTClassDeclaration)decl.jjtGetParent()).isAbstract()) {
25 isOK = true;
26 }
27 return super.visit(decl, data);
28 }
29
30 public Object visit(ASTMethodDeclaration decl, Object data) {
31 methodCount++;
32
33 if (!isOK && !decl.isStatic()) {
34 isOK = true;
35 }
36
37 return data;
38 }
39
40 public Object visit(ASTCompilationUnit cu, Object data) {
41 methodCount = 0;
42 isOK = false;
43 Object RC = cu.childrenAccept(this, data);
44
45 if (!isOK && methodCount > 0) {
46 RuleContext ctx = (RuleContext) data;
47 ctx.getReport().addRuleViolation(createRuleViolation(ctx, cu.getBeginLine()));
48 }
49
50 return RC;
51 }
52 }
This page was automatically generated by Maven