View Javadoc

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