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
    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).isExceptionBlockParameter();
    33      }
    34
    35      public boolean isPrimitiveType() {
    36          return getAccessNodeParent().jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimitiveType;
    37      }
    38
    39      public String getTypeImage() {
    40          if (isPrimitiveType()) {
    41              return ((SimpleNode) (getAccessNodeParent().jjtGetChild(0).jjtGetChild(0))).getImage();
    42          }
    43          return ((SimpleNode) getAccessNodeParent().jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).getImage();
    44      }
    45
    46      /***
    47       * Note that an array of primitive types (int[]) is a reference type.
    48       */
    49      public boolean isReferenceType() {
    50          return getAccessNodeParent().jjtGetChild(0).jjtGetChild(0) instanceof ASTReferenceType;
    51      }
    52
    53      public AccessNode getAccessNodeParent() {
    54          if (node.jjtGetParent() instanceof ASTFormalParameter) {
    55              return (AccessNode) node.jjtGetParent();
    56          }
    57          return (AccessNode) node.jjtGetParent().jjtGetParent();
    58      }
    59
    60      public ASTVariableDeclaratorId getDeclaratorId() {
    61          return (ASTVariableDeclaratorId) node;
    62      }
    63
    64      public boolean equals(Object o) {
    65          VariableNameDeclaration n = (VariableNameDeclaration) o;
    66          return n.node.getImage().equals(node.getImage());
    67      }
    68
    69      public int hashCode() {
    70          return node.getImage().hashCode();
    71      }
    72
    73      public String toString() {
    74          return "Variable: image = '" + node.getImage() + "', line = " + node.getBeginLine();
    75      }
    76  }