1
2
3
4 package net.sourceforge.pmd.lang.java.symboltable;
5
6 import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
7 import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
8 import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
9 import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
10 import net.sourceforge.pmd.lang.java.ast.ASTType;
11 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
12 import net.sourceforge.pmd.lang.java.ast.AccessNode;
13 import net.sourceforge.pmd.lang.java.ast.Dimensionable;
14 import net.sourceforge.pmd.lang.java.ast.TypeNode;
15 import net.sourceforge.pmd.lang.symboltable.AbstractNameDeclaration;
16 import net.sourceforge.pmd.lang.symboltable.Scope;
17
18 public class VariableNameDeclaration extends AbstractNameDeclaration implements TypedNameDeclaration {
19
20 public VariableNameDeclaration(ASTVariableDeclaratorId node) {
21 super(node);
22 }
23
24 @Override
25 public Scope getScope() {
26 return node.getScope().getEnclosingScope(ClassScope.class);
27 }
28
29 public boolean isArray() {
30 ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
31 ASTType typeNode = astVariableDeclaratorId.getTypeNode();
32 if (typeNode != null) {
33 return ((Dimensionable) typeNode.jjtGetParent()).isArray();
34 } else {
35 return false;
36 }
37 }
38
39 public boolean isExceptionBlockParameter() {
40 return ((ASTVariableDeclaratorId) node).isExceptionBlockParameter();
41 }
42
43 public boolean isLambdaTypelessParameter() {
44 return getAccessNodeParent() instanceof ASTLambdaExpression;
45 }
46
47 public boolean isPrimitiveType() {
48 return !isLambdaTypelessParameter() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTPrimitiveType;
49 }
50
51 public String getTypeImage() {
52 TypeNode typeNode = getTypeNode();
53 if (typeNode != null)
54 return typeNode.getImage();
55 return null;
56 }
57
58
59
60
61 public boolean isReferenceType() {
62 return !isLambdaTypelessParameter() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTReferenceType;
63 }
64
65 public AccessNode getAccessNodeParent() {
66 if (node.jjtGetParent() instanceof ASTFormalParameter
67 || node.jjtGetParent() instanceof ASTLambdaExpression) {
68 return (AccessNode)node.jjtGetParent();
69 }
70 return (AccessNode)node.jjtGetParent().jjtGetParent();
71 }
72
73 public ASTVariableDeclaratorId getDeclaratorId() {
74 return (ASTVariableDeclaratorId) node;
75 }
76
77 private TypeNode getTypeNode() {
78 if (isPrimitiveType()) {
79 return (TypeNode)getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0);
80 }
81 if (!isLambdaTypelessParameter()) {
82 return (TypeNode)getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0).jjtGetChild(0);
83 }
84 return null;
85 }
86
87 public Class<?> getType() {
88 TypeNode typeNode = getTypeNode();
89 if (typeNode != null)
90 return typeNode.getType();
91 return null;
92 }
93
94 @Override
95 public boolean equals(Object o) {
96 if (!(o instanceof VariableNameDeclaration)) {
97 return false;
98 }
99 VariableNameDeclaration n = (VariableNameDeclaration) o;
100 return n.node.getImage().equals(node.getImage());
101 }
102
103 @Override
104 public int hashCode() {
105 return node.getImage().hashCode();
106 }
107
108 @Override
109 public String toString() {
110 return "Variable: image = '" + node.getImage() + "', line = " + node.getBeginLine();
111 }
112 }