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