View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.unusedcode;
5   
6   import net.sourceforge.pmd.lang.ast.Node;
7   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
8   import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
9   import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
10  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
11  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
12  
13  public class UnusedModifierRule extends AbstractJavaRule {
14  
15      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
16          if (!node.isInterface() && node.isNested() && (node.isPublic() || node.isStatic())) {
17              ASTClassOrInterfaceDeclaration parentClassInterface = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
18              ASTEnumDeclaration parentEnum = node.getFirstParentOfType(ASTEnumDeclaration.class);
19              if (parentClassInterface != null && parentClassInterface.isInterface()
20                      ||
21                      parentEnum != null) {
22                  addViolation(data, node, getMessage());
23              }
24          } else if (node.isInterface() && node.isNested() && (node.isPublic() || node.isStatic())) {
25              ASTClassOrInterfaceDeclaration parentClassInterface = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
26              ASTEnumDeclaration parentEnum = node.getFirstParentOfType(ASTEnumDeclaration.class);
27              if (parentClassInterface != null && (parentClassInterface.isInterface() || (!parentClassInterface.isInterface() && node.isStatic()))
28                      ||
29                      parentEnum != null) {
30                  addViolation(data, node, getMessage());
31              }
32          }
33          return super.visit(node, data);
34      }
35  
36      public Object visit(ASTMethodDeclaration node, Object data) {
37          if (node.isSyntacticallyPublic() || node.isSyntacticallyAbstract()) {
38              check(node, data);
39          }
40          return super.visit(node, data);
41      }
42  
43      public Object visit(ASTFieldDeclaration node, Object data) {
44          if (node.isSyntacticallyPublic() || node.isSyntacticallyStatic() || node.isSyntacticallyFinal()) {
45              check(node, data);
46          }
47          return super.visit(node, data);
48      }
49  
50      private void check(Node fieldOrMethod, Object data) {
51          // third ancestor could be an AllocationExpression
52          // if this is a method in an anonymous inner class
53          Node parent = fieldOrMethod.jjtGetParent().jjtGetParent().jjtGetParent();
54          if (parent instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
55              addViolation(data, fieldOrMethod);
56          }
57      }
58  }