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. ASTLocalVariableDeclaration.java */
5   
6   package net.sourceforge.pmd.lang.java.ast;
7   
8   import net.sourceforge.pmd.Rule;
9   
10  public class ASTLocalVariableDeclaration extends AbstractJavaAccessNode implements Dimensionable, CanSuppressWarnings {
11  
12      public ASTLocalVariableDeclaration(int id) {
13          super(id);
14      }
15  
16      public ASTLocalVariableDeclaration(JavaParser p, int id) {
17          super(p, id);
18      }
19  
20      /**
21       * Accept the visitor. *
22       */
23      @Override
24      public Object jjtAccept(JavaParserVisitor visitor, Object data) {
25          return visitor.visit(this, data);
26      }
27  
28      public boolean hasSuppressWarningsAnnotationFor(Rule rule) {
29          for (int i = 0; i < jjtGetNumChildren(); i++) {
30              if (jjtGetChild(i) instanceof ASTAnnotation) {
31                  ASTAnnotation a = (ASTAnnotation) jjtGetChild(i);
32                  if (a.suppresses(rule)) {
33                      return true;
34                  }
35              }
36          }
37          return false;
38      }
39  
40      public boolean isArray() {
41          return checkType() + checkDecl() > 0;
42      }
43  
44      public int getArrayDepth() {
45          return checkType() + checkDecl();
46      }
47  
48      public ASTType getTypeNode() {
49          for (int i = 0; i < jjtGetNumChildren(); i++) {
50              if (jjtGetChild(i) instanceof ASTType) {
51                  return (ASTType) jjtGetChild(i);
52              }
53          }
54          throw new IllegalStateException("ASTType not found");
55      }
56  
57      private int checkType() {
58          return getTypeNode().getArrayDepth();
59      }
60  
61      private ASTVariableDeclaratorId getDecl() {
62          return (ASTVariableDeclaratorId) jjtGetChild(jjtGetNumChildren()-1).jjtGetChild(0);
63      }
64  
65      private int checkDecl() {
66          return getDecl().getArrayDepth();
67      }
68  
69      /**
70       * Gets the variable name of this field.
71       * This method searches the first VariableDeclartorId node and returns it's image or <code>null</code> if the child node is not found.
72       *
73       * @return a String representing the name of the variable
74       */
75      public String getVariableName() {
76          ASTVariableDeclaratorId decl = getFirstDescendantOfType(ASTVariableDeclaratorId.class);
77          if (decl != null) {
78              return decl.getImage();
79          }
80          return null;
81      }
82  }