01: package net.sourceforge.pmd.rules;
02:
03: import net.sourceforge.pmd.AbstractRule;
04: import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
05: import net.sourceforge.pmd.ast.ASTFieldDeclaration;
06: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
07: import net.sourceforge.pmd.ast.Node;
08: import net.sourceforge.pmd.ast.SimpleNode;
09:
10: public class UnusedModifier extends AbstractRule {
11:
12: public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
13: if (!node.isInterface() && node.isNested()
14: && (node.isPublic() || node.isStatic())) {
15: ASTClassOrInterfaceDeclaration parent = node
16: .getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
17: if (parent != null && parent.isInterface()) {
18: addViolation(data, node, getMessage());
19: }
20: } else if (node.isInterface() && node.isNested()
21: && (node.isPublic() || node.isStatic())) {
22: ASTClassOrInterfaceDeclaration parent = node
23: .getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
24: if (parent.isInterface()
25: || (!parent.isInterface() && node.isStatic())) {
26: addViolation(data, node, getMessage());
27: }
28: }
29: return super .visit(node, data);
30: }
31:
32: public Object visit(ASTMethodDeclaration node, Object data) {
33: if (node.isSyntacticallyPublic()
34: || node.isSyntacticallyAbstract()) {
35: check(node, data);
36: }
37: return super .visit(node, data);
38: }
39:
40: public Object visit(ASTFieldDeclaration node, Object data) {
41: if (node.isSyntacticallyPublic()
42: || node.isSyntacticallyStatic()
43: || node.isSyntacticallyFinal()) {
44: check(node, data);
45: }
46: return super .visit(node, data);
47: }
48:
49: private void check(SimpleNode fieldOrMethod, Object data) {
50: // third ancestor could be an AllocationExpression
51: // if this is a method in an anonymous inner class
52: Node parent = fieldOrMethod.jjtGetParent().jjtGetParent()
53: .jjtGetParent();
54: if (parent instanceof ASTClassOrInterfaceDeclaration
55: && ((ASTClassOrInterfaceDeclaration) parent)
56: .isInterface()) {
57: addViolation(data, fieldOrMethod);
58: }
59: }
60:
61: }
|