1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package net.sourceforge.pmd.rules.design;
5
6 import net.sourceforge.pmd.AbstractRule;
7 import net.sourceforge.pmd.RuleContext;
8 import net.sourceforge.pmd.ast.ASTClassDeclaration;
9 import net.sourceforge.pmd.ast.ASTCompilationUnit;
10 import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
11 import net.sourceforge.pmd.ast.ASTMethodDeclaration;
12 import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
13 import net.sourceforge.pmd.ast.ASTFieldDeclaration;
14
15 public class UseSingletonRule extends AbstractRule {
16
17 private boolean isOK;
18 private int methodCount;
19
20 public Object visit(ASTCompilationUnit cu, Object data) {
21 methodCount = 0;
22 isOK = false;
23 Object RC = cu.childrenAccept(this, data);
24
25 if (!isOK && methodCount > 0) {
26 RuleContext ctx = (RuleContext) data;
27 ctx.getReport().addRuleViolation(createRuleViolation(ctx, cu.getBeginLine()));
28 }
29
30 return RC;
31 }
32
33 public Object visit(ASTFieldDeclaration decl, Object data) {
34 isOK = true;
35 return data;
36 }
37
38 public Object visit(ASTConstructorDeclaration decl, Object data) {
39 if (decl.isPrivate()) {
40 isOK = true;
41 }
42 return data;
43 }
44
45 public Object visit(ASTUnmodifiedClassDeclaration decl, Object data) {
46 if (decl.jjtGetParent() instanceof ASTClassDeclaration && ((ASTClassDeclaration)decl.jjtGetParent()).isAbstract()) {
47 isOK = true;
48 }
49 return super.visit(decl, data);
50 }
51
52 public Object visit(ASTMethodDeclaration decl, Object data) {
53 methodCount++;
54
55 if (!isOK && !decl.isStatic()) {
56 isOK = true;
57 }
58
59 return data;
60 }
61
62 }
This page was automatically generated by Maven