View Javadoc

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