01 /**
02 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03 */
04 package net.sourceforge.pmd.lang.java.symboltable;
05
06 import net.sourceforge.pmd.lang.ast.Node;
07 import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
08 import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
09 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
16 public class VariableNameDeclaration extends AbstractNameDeclaration {
17
18 public VariableNameDeclaration(ASTVariableDeclaratorId node) {
19 super(node);
20 }
21
22 @Override
23 public Scope getScope() {
24 return node.getScope().getEnclosingClassScope();
25 }
26
27 public boolean isArray() {
28 ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
29 ASTType typeNode = astVariableDeclaratorId.getTypeNode();
30 return ((Dimensionable) typeNode.jjtGetParent()).isArray();
31 }
32
33 public boolean isExceptionBlockParameter() {
34 return ((ASTVariableDeclaratorId) node).isExceptionBlockParameter();
35 }
36
37 public boolean isPrimitiveType() {
38 return ((Node) getAccessNodeParent()).jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimitiveType;
39 }
40
41 public String getTypeImage() {
42 if (isPrimitiveType()) {
43 return ((Node) getAccessNodeParent()).jjtGetChild(0).jjtGetChild(0).getImage();
44 }
45 return ((Node) getAccessNodeParent()).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).getImage();
46 }
47
48 /**
49 * Note that an array of primitive types (int[]) is a reference type.
50 */
51 public boolean isReferenceType() {
52 return ((Node) getAccessNodeParent()).jjtGetChild(0).jjtGetChild(0) instanceof ASTReferenceType;
53 }
54
55 public AccessNode getAccessNodeParent() {
56 if (node.jjtGetParent() instanceof ASTFormalParameter) {
57 return (AccessNode) node.jjtGetParent();
58 }
59 return (AccessNode) node.jjtGetParent().jjtGetParent();
60 }
61
62 public ASTVariableDeclaratorId getDeclaratorId() {
63 return (ASTVariableDeclaratorId) node;
64 }
65
66 public Class<?> getType() {
67 return ((TypeNode) node).getType();
68 }
69
70 @Override
71 public boolean equals(Object o) {
72 if (!(o instanceof VariableNameDeclaration)) {
73 return false;
74 }
75 VariableNameDeclaration n = (VariableNameDeclaration) o;
76 return n.node.getImage().equals(node.getImage());
77 }
78
79 @Override
80 public int hashCode() {
81 return node.getImage().hashCode();
82 }
83
84 @Override
85 public String toString() {
86 return "Variable: image = '" + node.getImage() + "', line = " + node.getBeginLine();
87 }
88 }
|