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. ASTVariableDeclaratorId.java */
5   
6   package net.sourceforge.pmd.lang.java.ast;
7   
8   import java.util.List;
9   
10  import net.sourceforge.pmd.lang.ast.Node;
11  import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
12  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
13  
14  public class ASTVariableDeclaratorId extends AbstractJavaTypeNode {
15  
16      public ASTVariableDeclaratorId(int id) {
17          super(id);
18      }
19  
20      public ASTVariableDeclaratorId(JavaParser p, int id) {
21          super(p, id);
22      }
23  
24      /**
25       * Accept the visitor. *
26       */
27      @Override
28      public Object jjtAccept(JavaParserVisitor visitor, Object data) {
29          return visitor.visit(this, data);
30      }
31  
32      private int arrayDepth;
33      private VariableNameDeclaration nameDeclaration;
34      private boolean explicitReceiverParameter = false;
35  
36      public VariableNameDeclaration getNameDeclaration() {
37          return nameDeclaration;
38      }
39  
40      public void setNameDeclaration(VariableNameDeclaration decl) {
41          nameDeclaration = decl;
42      }
43  
44      public List<NameOccurrence> getUsages() {
45          return getScope().getDeclarations().get(nameDeclaration);
46      }
47  
48      public void bumpArrayDepth() {
49          arrayDepth++;
50      }
51  
52      public int getArrayDepth() {
53          return arrayDepth;
54      }
55  
56      public boolean isArray() {
57          return arrayDepth > 0;
58      }
59  
60      public boolean isExceptionBlockParameter() {
61          return jjtGetParent().jjtGetParent() instanceof ASTTryStatement;
62      }
63  
64      public void setExplicitReceiverParameter() {
65          explicitReceiverParameter = true;
66      }
67      public boolean isExplicitReceiverParameter() {
68          return explicitReceiverParameter;
69      }
70  
71      public Node getTypeNameNode() {
72          if (jjtGetParent() instanceof ASTFormalParameter) {
73              return findTypeNameNode(jjtGetParent());
74          } else if (jjtGetParent() instanceof ASTLambdaExpression) {
75              // lambda expression with lax types. The type is inferred...
76              return null;
77          } else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
78              return findTypeNameNode(jjtGetParent().jjtGetParent());
79          }
80          return null;
81      }
82  
83      /**
84       * Determines the type node of this variable id.
85       * @return the type node or <code>null</code> if there is no explicit type.
86       */
87      public ASTType getTypeNode() {
88          if (jjtGetParent() instanceof ASTFormalParameter) {
89              return ((ASTFormalParameter) jjtGetParent()).getTypeNode();
90          } else if (jjtGetParent() instanceof ASTLambdaExpression) {
91              // lambda expression with lax types. The type is inferred...
92              return null;
93          } else {
94              Node n = jjtGetParent().jjtGetParent();
95              if (n instanceof ASTLocalVariableDeclaration || n instanceof ASTFieldDeclaration) {
96                  return n.getFirstChildOfType(ASTType.class);
97              }
98          }
99          return null;
100     }
101 
102     private Node findTypeNameNode(Node node) {
103         int i = 0;
104         while (node.jjtGetChild(i) instanceof ASTAnnotation) {
105             // skip annotations
106             i++;
107         }
108         ASTType typeNode = (ASTType) node.jjtGetChild(i);
109         return typeNode.jjtGetChild(0);
110     }
111 }