Class ASTVariableDeclaratorId

  • All Implemented Interfaces:
    net.sourceforge.pmd.lang.ast.Node, Dimensionable, JavaNode, TypeNode, net.sourceforge.pmd.lang.symboltable.ScopedNode

    public class ASTVariableDeclaratorId
    extends AbstractJavaTypeNode
    implements Dimensionable
    Represents an identifier in the context of variable or parameter declarations (not their use in expressions). Such a node declares a name in the scope it's defined in, and can occur in the following contexts:
    • Field declarations;
    • Local variable declarations;
    • Method, constructor and lambda parameter declarations;
    • Method and constructor explicit receiver parameter declarations;
    • Exception parameter declarations occurring in catch clauses;
    • Resource declarations occurring in try-with-resources statements.

    Since this node conventionally represents the declared variable in PMD, our symbol table populates it with a VariableNameDeclaration, and its usages can be accessed through the method getUsages().

    Type resolution assigns the type of the variable to this node. See getType()'s documentation for the contract of this method.

    • Field Summary

      • Fields inherited from class net.sourceforge.pmd.lang.ast.AbstractNode

        beginColumn, beginLine, childIndex, children, endColumn, endLine, firstToken, id, lastToken, parent
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void bumpArrayDepth()
      Deprecated.
      int getArrayDepth()
      Deprecated.
      VariableNameDeclaration getNameDeclaration()  
      java.lang.Class<?> getType()
      Returns the type of the declared variable.
      net.sourceforge.pmd.lang.ast.Node getTypeNameNode()
      Returns the first child of the node returned by getTypeNode().
      ASTType getTypeNode()
      Determines the type node of this variable id, that is, the type node belonging to the variable declaration of this node (either a FormalParameter, LocalVariableDeclaration or FieldDeclaration).
      java.util.List<net.sourceforge.pmd.lang.symboltable.NameOccurrence> getUsages()  
      java.lang.String getVariableName()
      Returns the name of the variable.
      boolean hasArrayType()
      Returns true if the declared variable has an array type.
      boolean isArray()
      Deprecated.
      boolean isExceptionBlockParameter()
      Returns true if this nodes declares an exception parameter in a catch statement.
      boolean isExplicitReceiverParameter()
      Returns true if this node is a receiver parameter for a method or constructor declaration.
      boolean isField()
      Returns true if this node declares a field.
      boolean isFinal()
      Returns true if the variable declared by this node is declared final.
      boolean isFormalParameter()
      Returns true if this node declares a formal parameter for a method declaration or a lambda expression.
      boolean isLambdaParameter()
      Returns true if this node declares a formal parameter for a lambda expression.
      boolean isLocalVariable()
      Returns true if this node declares a local variable.
      boolean isResourceDeclaration()
      Returns true if this declarator id declares a resource in a try-with-resources statement.
      boolean isTypeInferred()
      Returns true if the declared variable's type is inferred by the compiler.
      java.lang.Object jjtAccept​(JavaParserVisitor visitor, java.lang.Object data)
      Calls back the visitor's visit method corresponding to the runtime type of this Node.
      void setExplicitReceiverParameter()
      Deprecated.
      Will be made private with 7.0.0
      void setNameDeclaration​(VariableNameDeclaration decl)
      Deprecated.
      • Methods inherited from class net.sourceforge.pmd.lang.ast.AbstractNode

        appendElement, findChildNodesWithXPath, findChildrenOfType, findDescendantsOfType, findDescendantsOfType, findDescendantsOfType, getAsDocument, getBeginColumn, getBeginLine, getDataFlowNode, getEndColumn, getEndLine, getFirstChildOfType, getFirstDescendantOfType, getFirstParentOfAnyType, getFirstParentOfType, getImage, getNthParent, getParentsOfType, getUserData, getXPathAttributesIterator, hasDecendantOfAnyType, hasDescendantMatchingXPath, hasDescendantOfAnyType, hasDescendantOfType, hasImageEqualTo, isFindBoundary, isSingleLine, jjtAddChild, jjtGetChild, jjtGetChildIndex, jjtGetFirstToken, jjtGetId, jjtGetLastToken, jjtGetNumChildren, jjtGetParent, jjtSetChildIndex, jjtSetFirstToken, jjtSetLastToken, jjtSetParent, remove, removeChildAtIndex, setDataFlowNode, setImage, setUserData, testingOnlySetBeginColumn, testingOnlySetBeginLine, testingOnlySetEndColumn, testingOnlySetEndLine, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface net.sourceforge.pmd.lang.ast.Node

        findChildNodesWithXPath, findChildrenOfType, findDescendantsOfType, findDescendantsOfType, getAsDocument, getBeginColumn, getBeginLine, getDataFlowNode, getEndColumn, getEndLine, getFirstChildOfType, getFirstDescendantOfType, getFirstParentOfAnyType, getFirstParentOfType, getImage, getNthParent, getParentsOfType, getUserData, getXPathAttributesIterator, getXPathNodeName, hasDescendantMatchingXPath, hasDescendantOfType, hasImageEqualTo, isFindBoundary, jjtAddChild, jjtClose, jjtGetChild, jjtGetChildIndex, jjtGetId, jjtGetNumChildren, jjtGetParent, jjtOpen, jjtSetChildIndex, jjtSetParent, remove, removeChildAtIndex, setDataFlowNode, setImage, setUserData
      • Methods inherited from interface net.sourceforge.pmd.lang.symboltable.ScopedNode

        getScope
    • Constructor Detail

      • ASTVariableDeclaratorId

        @InternalApi
        @Deprecated
        public ASTVariableDeclaratorId​(int id)
        Deprecated.
      • ASTVariableDeclaratorId

        @InternalApi
        @Deprecated
        public ASTVariableDeclaratorId​(JavaParser p,
                                       int id)
        Deprecated.
    • Method Detail

      • jjtAccept

        public java.lang.Object jjtAccept​(JavaParserVisitor visitor,
                                          java.lang.Object data)
        Description copied from interface: JavaNode
        Calls back the visitor's visit method corresponding to the runtime type of this Node.
        Specified by:
        jjtAccept in interface JavaNode
        Overrides:
        jjtAccept in class AbstractJavaNode
        Parameters:
        visitor - Visitor to dispatch
        data - Visit data
      • setNameDeclaration

        @InternalApi
        @Deprecated
        public void setNameDeclaration​(VariableNameDeclaration decl)
        Deprecated.
      • getUsages

        public java.util.List<net.sourceforge.pmd.lang.symboltable.NameOccurrence> getUsages()
      • bumpArrayDepth

        @Deprecated
        public void bumpArrayDepth()
        Deprecated.
      • isArray

        @Deprecated
        public boolean isArray()
        Deprecated.
        Returns true if the declared variable has an array type.
        Specified by:
        isArray in interface Dimensionable
      • hasArrayType

        public boolean hasArrayType()
        Returns true if the declared variable has an array type.
      • isExceptionBlockParameter

        public boolean isExceptionBlockParameter()
        Returns true if this nodes declares an exception parameter in a catch statement.
      • isFormalParameter

        public boolean isFormalParameter()
        Returns true if this node declares a formal parameter for a method declaration or a lambda expression. In particular, returns false if the node is a receiver parameter (see isExplicitReceiverParameter()).
      • isLocalVariable

        public boolean isLocalVariable()
        Returns true if this node declares a local variable.
      • isLambdaParameter

        public boolean isLambdaParameter()
        Returns true if this node declares a formal parameter for a lambda expression. In that case, the type of this parameter is not necessarily inferred, see isTypeInferred().
      • isField

        public boolean isField()
        Returns true if this node declares a field.
      • getVariableName

        public java.lang.String getVariableName()
        Returns the name of the variable.
      • isFinal

        public boolean isFinal()
        Returns true if the variable declared by this node is declared final. Doesn't account for the "effectively-final" nuance. Resource declarations are implicitly final.
      • setExplicitReceiverParameter

        @InternalApi
        @Deprecated
        public void setExplicitReceiverParameter()
        Deprecated.
        Will be made private with 7.0.0
      • isExplicitReceiverParameter

        public boolean isExplicitReceiverParameter()
        Returns true if this node is a receiver parameter for a method or constructor declaration. The receiver parameter has the name this, and must be declared at the beginning of the parameter list. Its only purpose is to annotate the type of the object on which the method call is issued. It was introduced in Java 8.
      • isResourceDeclaration

        public boolean isResourceDeclaration()
        Returns true if this declarator id declares a resource in a try-with-resources statement.
      • isTypeInferred

        public boolean isTypeInferred()
        Returns true if the declared variable's type is inferred by the compiler. In Java 8, this can happen if it's in a formal parameter of a lambda with an inferred type (e.g. (a, b) -> a + b). Since Java 10, the type of local variables can be inferred too, e.g. var i = 2;.

        This method returns true for declarator IDs in those contexts, in which case getTypeNode() returns null, since the type node is absent.

      • getTypeNameNode

        public net.sourceforge.pmd.lang.ast.Node getTypeNameNode()
        Returns the first child of the node returned by getTypeNode(). The image of that node can usually be interpreted as the image of the type.
      • getTypeNode

        public ASTType getTypeNode()
        Determines the type node of this variable id, that is, the type node belonging to the variable declaration of this node (either a FormalParameter, LocalVariableDeclaration or FieldDeclaration).

        The type of the returned node is not necessarily the type of this node. See getType() for an explanation.

        Returns:
        the type node, or null if there is no explicit type, e.g. if isTypeInferred() returns true.
      • getType

        public java.lang.Class<?> getType()
        Returns the type of the declared variable. The type of a declarator ID is
        • 1. not necessarily the same as the type written out at the start of the declaration, e.g. int a[];
        • 2. not necessarily the same as the types of other variables declared in the same statement, e.g. int a[], b;.

        These are consequences of Java's allowing programmers to declare additional pairs of brackets on declarator ids. The type of the node returned by getTypeNode() doesn't take into account those additional array dimensions, whereas this node's type takes into account the total number of dimensions, i.e. those declared on this node plus those declared on the type node.

        The returned type also takes into account whether this variable is a varargs formal parameter.

        The type of the declarator ID is thus always the real type of the variable.

        Specified by:
        getType in interface TypeNode
        Overrides:
        getType in class AbstractJavaTypeNode
        Returns:
        The Java Class, may return null.