Class ASTVariableDeclaratorId

  • All Implemented Interfaces:
    Node, Dimensionable, JavaNode, TypeNode, 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.

    • Method Detail

      • getNameDeclaration

        public VariableNameDeclaration getNameDeclaration()
        Note: this might be null in certain cases.
      • bumpArrayDepth

        @Deprecated
        public void bumpArrayDepth()
        Deprecated.
      • isArray

        @Deprecated
        @DeprecatedAttribute(replaceWith="@ArrayType")
        public boolean isArray()
        Deprecated.
        Returns true if the declared variable has an array type.
        Specified by:
        isArray in interface Dimensionable
      • getName

        public String getName()
        Returns the name of the variable.
      • 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.
      • isForeachVariable

        public boolean isForeachVariable()
        Returns true if this node is a variable declared in a foreach loop.
      • 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

        @Deprecated
        @DeprecatedAttribute(replaceWith="@Name")
        public String getVariableName()
        Deprecated.
        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.

      • isPatternBinding

        public boolean isPatternBinding()
        Returns true if this is a binding variable in a pattern.
      • getTypeNameNode

        public 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 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.