1
2
3
4
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
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
35 public VariableNameDeclaration getNameDeclaration() {
36 return nameDeclaration;
37 }
38
39 public void setNameDeclaration(VariableNameDeclaration decl) {
40 nameDeclaration = decl;
41 }
42
43 public List<NameOccurrence> getUsages() {
44 return getScope().getDeclarations().get(nameDeclaration);
45 }
46
47 public void bumpArrayDepth() {
48 arrayDepth++;
49 }
50
51 public int getArrayDepth() {
52 return arrayDepth;
53 }
54
55 public boolean isArray() {
56 return arrayDepth > 0;
57 }
58
59 public boolean isExceptionBlockParameter() {
60 return jjtGetParent().jjtGetParent() instanceof ASTTryStatement;
61 }
62
63 public Node getTypeNameNode() {
64 if (jjtGetParent() instanceof ASTFormalParameter) {
65 return findTypeNameNode(jjtGetParent());
66 } else if (jjtGetParent() instanceof ASTLambdaExpression) {
67
68 return null;
69 } else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
70 return findTypeNameNode(jjtGetParent().jjtGetParent());
71 }
72 return null;
73 }
74
75
76
77
78
79 public ASTType getTypeNode() {
80 if (jjtGetParent() instanceof ASTFormalParameter) {
81 return ((ASTFormalParameter) jjtGetParent()).getTypeNode();
82 } else if (jjtGetParent() instanceof ASTLambdaExpression) {
83
84 return null;
85 } else {
86 Node n = jjtGetParent().jjtGetParent();
87 if (n instanceof ASTLocalVariableDeclaration || n instanceof ASTFieldDeclaration) {
88 return n.getFirstChildOfType(ASTType.class);
89 }
90 }
91 return null;
92 }
93
94 private Node findTypeNameNode(Node node) {
95 int i = 0;
96 while (node.jjtGetChild(i) instanceof ASTAnnotation) {
97
98 i++;
99 }
100 ASTType typeNode = (ASTType) node.jjtGetChild(i);
101 return typeNode.jjtGetChild(0);
102 }
103 }