01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.design;
04:
05: import net.sourceforge.pmd.AbstractRule;
06: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
07: import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
08: import net.sourceforge.pmd.ast.ASTCompilationUnit;
09: import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
10: import net.sourceforge.pmd.ast.ASTFieldDeclaration;
11: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
12: import net.sourceforge.pmd.ast.ASTResultType;
13:
14: public class UseSingleton extends AbstractRule {
15:
16: private boolean isOK;
17: private int methodCount;
18:
19: public Object visit(ASTCompilationUnit cu, Object data) {
20: methodCount = 0;
21: isOK = false;
22: Object result = cu.childrenAccept(this , data);
23: if (!isOK && methodCount > 0) {
24: addViolation(data, cu);
25: }
26:
27: return result;
28: }
29:
30: public Object visit(ASTFieldDeclaration decl, Object data) {
31: if (!decl.isStatic()) {
32: isOK = true;
33: }
34: return data;
35: }
36:
37: public Object visit(ASTConstructorDeclaration decl, Object data) {
38: if (decl.isPrivate()) {
39: isOK = true;
40: }
41: return data;
42: }
43:
44: public Object visit(ASTClassOrInterfaceDeclaration decl, Object data) {
45: if (decl.isAbstract()) {
46: isOK = true;
47: }
48: return super .visit(decl, data);
49: }
50:
51: public Object visit(ASTMethodDeclaration decl, Object data) {
52: methodCount++;
53:
54: if (!isOK && !decl.isStatic()) {
55: isOK = true;
56: }
57:
58: // TODO use symbol table
59: if (decl.getMethodName().equals("suite")) {
60: ASTResultType res = decl
61: .getFirstChildOfType(ASTResultType.class);
62: ASTClassOrInterfaceType c = res
63: .getFirstChildOfType(ASTClassOrInterfaceType.class);
64: if (c != null && c.hasImageEqualTo("Test")) {
65: isOK = true;
66: }
67: }
68:
69: return data;
70: }
71:
72: }
|