View Javadoc

    1   /* Generated By:JJTree: Do not edit this line. ASTAnnotation.java */
    2
    3   package net.sourceforge.pmd.lang.java.ast;
    4
    5   import java.util.Arrays;
    6   import java.util.List;
    7
    8   import net.sourceforge.pmd.Rule;
    9   import net.sourceforge.pmd.lang.ast.Node;
    10
    11  public class ASTAnnotation extends AbstractJavaNode {
    12
    13      private static List<String> unusedRules = Arrays.asList(new String[] { "UnusedPrivateField", "UnusedLocalVariable",
    14  	    "UnusedPrivateMethod", "UnusedFormalParameter" });
    15
    16      private static List<String> serialRules = Arrays.asList(new String[] { "BeanMembersShouldSerialize", "MissingSerialVersionUID"});
    17
    18      public ASTAnnotation(int id) {
    19  	super(id);
    20      }
    21
    22      public ASTAnnotation(JavaParser p, int id) {
    23  	super(p, id);
    24      }
    25
    26      public boolean suppresses(Rule rule) {
    27  	final String ruleAnno = "\"PMD." + rule.getName() + "\"";
    28
    29  	if (jjtGetChild(0) instanceof ASTSingleMemberAnnotation) {
    30  	    ASTSingleMemberAnnotation n = (ASTSingleMemberAnnotation) jjtGetChild(0);
    31  	    return checkAnnototation(n, ruleAnno, rule);
    32  	} else if (jjtGetChild(0) instanceof ASTNormalAnnotation) {
    33  	    ASTNormalAnnotation n = (ASTNormalAnnotation) jjtGetChild(0);
    34  	    return checkAnnototation(n, ruleAnno, rule);
    35  	}
    36  	return false;
    37      }
    38
    39      private boolean checkAnnototation(Node n, String ruleAnno, Rule rule) {
    40  	if (n.jjtGetChild(0) instanceof ASTName) {
    41  	    ASTName annName = (ASTName) n.jjtGetChild(0);
    42
    43  	    if (annName.getImage().equals("SuppressWarnings")) {
    44  		List<ASTLiteral> nodes = n.findDescendantsOfType(ASTLiteral.class);
    45  		for (ASTLiteral element : nodes) {
    46  		    if (element.hasImageEqualTo("\"PMD\"") || element.hasImageEqualTo(ruleAnno)
    47  			    // Check for standard annotations values
    48  			    || element.hasImageEqualTo("\"all\"")
    49  			    || element.hasImageEqualTo("\"serial\"") && serialRules.contains(rule.getName())
    50  			    || element.hasImageEqualTo("\"unused\"") && unusedRules.contains(rule.getName())) {
    51  			return true;
    52  		    }
    53  		}
    54  	    }
    55  	}
    56  	return false;
    57      }
    58
    59      /**
    60       * Accept the visitor.
    61       */
    62      @Override
    63      public Object jjtAccept(JavaParserVisitor visitor, Object data) {
    64  	return visitor.visit(this, data);
    65      }
    66  }