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.NameDeclaration;
13 import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
14
15 public class ASTVariableDeclaratorId extends AbstractJavaTypeNode {
16
17 public ASTVariableDeclaratorId(int id) {
18 super(id);
19 }
20
21 public ASTVariableDeclaratorId(JavaParser p, int id) {
22 super(p, id);
23 }
24
25
26
27
28 @Override
29 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
30 return visitor.visit(this, data);
31 }
32
33 private int arrayDepth;
34 private VariableNameDeclaration nameDeclaration;
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 Node getTypeNameNode() {
65 if (jjtGetParent() instanceof ASTFormalParameter) {
66 return findTypeNameNode(jjtGetParent());
67 } else if (jjtGetParent() instanceof ASTLambdaExpression) {
68
69 return null;
70 } else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
71 return findTypeNameNode(jjtGetParent().jjtGetParent());
72 }
73 return null;
74 }
75
76
77
78
79
80 public ASTType getTypeNode() {
81 if (jjtGetParent() instanceof ASTFormalParameter) {
82 return ((ASTFormalParameter) jjtGetParent()).getTypeNode();
83 } else if (jjtGetParent() instanceof ASTLambdaExpression) {
84
85 return null;
86 } else {
87 Node n = jjtGetParent().jjtGetParent();
88 if (n instanceof ASTLocalVariableDeclaration || n instanceof ASTFieldDeclaration) {
89 return n.getFirstChildOfType(ASTType.class);
90 }
91 }
92 return null;
93 }
94
95 private Node findTypeNameNode(Node node) {
96 int i = 0;
97 while (node.jjtGetChild(i) instanceof ASTAnnotation) {
98
99 i++;
100 }
101 ASTType typeNode = (ASTType) node.jjtGetChild(i);
102 return typeNode.jjtGetChild(0);
103 }
104 }