VariableNameDeclaration.java
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 = (ASTVariableDeclaratorIdnode;
29   ASTType typeNode = astVariableDeclaratorId.getTypeNode();
30   return ((DimensionabletypeNode.jjtGetParent()).isArray();
31     }
32 
33     public boolean isExceptionBlockParameter() {
34   return ((ASTVariableDeclaratorIdnode).isExceptionBlockParameter();
35     }
36 
37     public boolean isPrimitiveType() {
38   return ((NodegetAccessNodeParent()).jjtGetChild(0).jjtGetChild(0instanceof ASTPrimitiveType;
39     }
40 
41     public String getTypeImage() {
42   if (isPrimitiveType()) {
43       return ((NodegetAccessNodeParent()).jjtGetChild(0).jjtGetChild(0).getImage();
44   }
45   return ((NodegetAccessNodeParent()).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 ((NodegetAccessNodeParent()).jjtGetChild(0).jjtGetChild(0instanceof ASTReferenceType;
53     }
54 
55     public AccessNode getAccessNodeParent() {
56   if (node.jjtGetParent() instanceof ASTFormalParameter) {
57       return (AccessNodenode.jjtGetParent();
58   }
59   return (AccessNodenode.jjtGetParent().jjtGetParent();
60     }
61 
62     public ASTVariableDeclaratorId getDeclaratorId() {
63   return (ASTVariableDeclaratorIdnode;
64     }
65 
66     public Class<?> getType() {
67   return ((TypeNodenode).getType();
68     }
69 
70     @Override
71     public boolean equals(Object o) {
72         if (!(instanceof VariableNameDeclaration)) {
73             return false;
74         }
75   VariableNameDeclaration n = (VariableNameDeclarationo;
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 }