View Javadoc

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