View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
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 isVarargs() {
40          ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
41          ASTFormalParameter parameter = astVariableDeclaratorId.getFirstParentOfType(ASTFormalParameter.class);
42          return parameter != null && parameter.isVarargs();
43      }
44  
45      public boolean isExceptionBlockParameter() {
46          return ((ASTVariableDeclaratorId) node).isExceptionBlockParameter();
47      }
48  
49      public boolean isLambdaTypelessParameter() {
50          return getAccessNodeParent() instanceof ASTLambdaExpression;
51      }
52  
53      public boolean isPrimitiveType() {
54          return !isLambdaTypelessParameter()
55                  && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTPrimitiveType;
56      }
57  
58      public String getTypeImage() {
59          TypeNode typeNode = getTypeNode();
60          if (typeNode != null) {
61              return typeNode.getImage();
62          }
63          return null;
64      }
65  
66      /**
67       * Note that an array of primitive types (int[]) is a reference type.
68       */
69      public boolean isReferenceType() {
70          return !isLambdaTypelessParameter()
71                  && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTReferenceType;
72      }
73  
74      public AccessNode getAccessNodeParent() {
75          if (node.jjtGetParent() instanceof ASTFormalParameter || node.jjtGetParent() instanceof ASTLambdaExpression) {
76              return (AccessNode) node.jjtGetParent();
77          }
78          return (AccessNode) node.jjtGetParent().jjtGetParent();
79      }
80  
81      public ASTVariableDeclaratorId getDeclaratorId() {
82          return (ASTVariableDeclaratorId) node;
83      }
84  
85      private TypeNode getTypeNode() {
86          if (isPrimitiveType()) {
87              return (TypeNode) getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0);
88          }
89          if (!isLambdaTypelessParameter()) {
90              return (TypeNode) getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0).jjtGetChild(0);
91          }
92          return null;
93      }
94  
95      public Class<?> getType() {
96          TypeNode typeNode = getTypeNode();
97          if (typeNode != null) {
98              return typeNode.getType();
99          }
100         return null;
101     }
102 
103     @Override
104     public boolean equals(Object o) {
105         if (!(o instanceof VariableNameDeclaration)) {
106             return false;
107         }
108         VariableNameDeclaration n = (VariableNameDeclaration) o;
109         return n.node.getImage().equals(node.getImage());
110     }
111 
112     @Override
113     public int hashCode() {
114         return node.getImage().hashCode();
115     }
116 
117     @Override
118     public String toString() {
119         return "Variable: image = '" + node.getImage() + "', line = " + node.getBeginLine();
120     }
121 }