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().jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimitiveType;
49 }
50
51 public String getTypeImage() {
52 if (isPrimitiveType()) {
53 return getAccessNodeParent().jjtGetChild(0).jjtGetChild(0).getImage();
54 }
55 if (!isLambdaTypelessParameter()) {
56 return getAccessNodeParent().jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).getImage();
57 } else
58 return null;
59 }
60
61
62
63
64 public boolean isReferenceType() {
65 return !isLambdaTypelessParameter() && getAccessNodeParent().jjtGetChild(0).jjtGetChild(0) instanceof ASTReferenceType;
66 }
67
68 public AccessNode getAccessNodeParent() {
69 if (node.jjtGetParent() instanceof ASTFormalParameter
70 || node.jjtGetParent() instanceof ASTLambdaExpression) {
71 return (AccessNode)node.jjtGetParent();
72 }
73 return (AccessNode)node.jjtGetParent().jjtGetParent();
74 }
75
76 public ASTVariableDeclaratorId getDeclaratorId() {
77 return (ASTVariableDeclaratorId) node;
78 }
79
80 public Class<?> getType() {
81 return ((TypeNode) node).getType();
82 }
83
84 @Override
85 public boolean equals(Object o) {
86 if (!(o instanceof VariableNameDeclaration)) {
87 return false;
88 }
89 VariableNameDeclaration n = (VariableNameDeclaration) o;
90 return n.node.getImage().equals(node.getImage());
91 }
92
93 @Override
94 public int hashCode() {
95 return node.getImage().hashCode();
96 }
97
98 @Override
99 public String toString() {
100 return "Variable: image = '" + node.getImage() + "', line = " + node.getBeginLine();
101 }
102 }