01 /* Generated By:JJTree: Do not edit this line. ASTVariableDeclaratorId.java */
02
03 package net.sourceforge.pmd.lang.java.ast;
04
05 import java.util.List;
06
07 import net.sourceforge.pmd.lang.ast.Node;
08 import net.sourceforge.pmd.lang.java.symboltable.NameOccurrence;
09 import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
10
11 public class ASTVariableDeclaratorId extends AbstractJavaTypeNode {
12
13 public ASTVariableDeclaratorId(int id) {
14 super(id);
15 }
16
17 public ASTVariableDeclaratorId(JavaParser p, int id) {
18 super(p, id);
19 }
20
21 /**
22 * Accept the visitor. *
23 */
24 @Override
25 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
26 return visitor.visit(this, data);
27 }
28
29 private int arrayDepth;
30 private VariableNameDeclaration nameDeclaration;
31
32 public VariableNameDeclaration getNameDeclaration() {
33 return nameDeclaration;
34 }
35
36 public void setNameDeclaration(VariableNameDeclaration decl) {
37 nameDeclaration = decl;
38 }
39
40 public List<NameOccurrence> getUsages() {
41 return getScope().getVariableDeclarations().get(nameDeclaration);
42 }
43
44 public void bumpArrayDepth() {
45 arrayDepth++;
46 }
47
48 public int getArrayDepth() {
49 return arrayDepth;
50 }
51
52 public boolean isArray() {
53 return arrayDepth > 0;
54 }
55
56 public boolean isExceptionBlockParameter() {
57 return jjtGetParent().jjtGetParent() instanceof ASTTryStatement;
58 }
59
60 public Node getTypeNameNode() {
61 if (jjtGetParent() instanceof ASTFormalParameter) {
62 return findTypeNameNode(jjtGetParent());
63 } else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
64 return findTypeNameNode(jjtGetParent().jjtGetParent());
65 }
66 throw new RuntimeException("Don't know how to get the type for anything other than ASTLocalVariableDeclaration/ASTFormalParameter/ASTFieldDeclaration");
67 }
68
69 public ASTType getTypeNode() {
70 if (jjtGetParent() instanceof ASTFormalParameter) {
71 return ((ASTFormalParameter) jjtGetParent()).getTypeNode();
72 } else {
73 Node n = jjtGetParent().jjtGetParent();
74 if (n instanceof ASTLocalVariableDeclaration || n instanceof ASTFieldDeclaration) {
75 return n.getFirstChildOfType(ASTType.class);
76 }
77 }
78 throw new RuntimeException("Don't know how to get the type for anything other than ASTLocalVariableDeclaration/ASTFormalParameter/ASTFieldDeclaration");
79 }
80
81 private Node findTypeNameNode(Node node) {
82 int i = 0;
83 while (node.jjtGetChild(i) instanceof ASTAnnotation) {
84 // skip annotations
85 i++;
86 }
87 ASTType typeNode = (ASTType) node.jjtGetChild(i);
88 return typeNode.jjtGetChild(0);
89 }
90 }
|