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