View Javadoc

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