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