Class ASTVariableId

    • Method Detail

      • acceptVisitor

        protected <P,​R> R acceptVisitor​(JavaVisitor<? super P,​? extends R> visitor,
                                              P data)
      • getLocalUsages

        public List<ASTAssignableExpr.ASTNamedReferenceExpr> getLocalUsages()
        Returns an unmodifiable list of the usages of this variable that are made in this file. Note that for a record component, this returns usages both for the formal parameter symbol and its field counterpart.

        Note that a variable initializer is not part of the usages (though this should be evident from the return type).

      • getExtraDimensions

        public @Nullable ASTArrayDimensions getExtraDimensions()
        Returns the extra array dimensions associated with this variable. For example in the declaration int a[], getTypeNode() returns int, and this method returns the dimensions that follow the variable ID. Returns null if there are no such dimensions.
      • isFinal

        public boolean isFinal()
        Return true if the declared variable is static. There may not be an explicit final modifier, e.g. for enum constants.
      • isStatic

        public boolean isStatic()
        Return true if the declared variable is static. There may not be an explicit static modifier, e.g. for enum constants.
        Since:
        7.1.0
      • isUnnamed

        public boolean isUnnamed()
        Return true if this variable has no name. The name is then equal to "_". A variable declaration with this name does not actually declare a variable in the current scope, since Java 22. In Java 9 to 21, the identifier _ is restricted and cannot be used to name a variable. Before Java 9, it is a regular identifier.
        See Also:
        JEP 456: Unnamed Variables & Patterns (Java 22)
      • 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.
      • isRecordComponent

        public boolean isRecordComponent()
        Returns true if this node declares a record component. The symbol born by this node is the symbol of the corresponding field (not the formal parameter of the record constructor).
      • isLocalVariable

        public boolean isLocalVariable()
        Returns true if this node declares a local variable from within a regular ASTLocalVariableDeclaration.
      • isForeachVariable

        public boolean isForeachVariable()
        Returns true if this node is a variable declared in a foreach loop.
      • isForLoopVariable

        public boolean isForLoopVariable()
        Returns true if this node is a variable declared in the init clause of a for 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().
      • isEnumConstant

        public boolean isEnumConstant()
        Returns true if this node declares an enum constant.
      • 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.
      • getInitializer

        public @Nullable ASTExpression getInitializer()
        Returns the initializer of the variable, or null if it doesn't exist.
      • getTypeNameNode

        public @Nullable 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 @Nullable 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 getTypeMirror() for an explanation.

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

        public @NonNull JTypeMirror getTypeMirror()
        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:
        getTypeMirror in interface TypeNode
        Returns:
        The type mirror. Never returns null; if the type is unresolved, returns TypeSystem.UNKNOWN.
      • acceptVisitor

        public final <P,​R> R acceptVisitor​(AstVisitor<? super P,​? extends R> visitor,
                                                 P data)
        Specified by:
        acceptVisitor in interface Node
      • addChild

        protected void addChild​(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child,
                                int index)
        Overrides:
        addChild in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,​JavaNode>
      • insertChild

        protected void insertChild​(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child,
                                   int index)
        Overrides:
        insertChild in class AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,​JavaNode>
      • removeChildAtIndex

        protected void removeChildAtIndex​(int childIndex)
        Overrides:
        removeChildAtIndex in class AbstractNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,​JavaNode>
      • setChild

        protected void setChild​(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child,
                                int index)
        Overrides:
        setChild in class AbstractNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,​JavaNode>
      • getSymbolTable

        public @NonNull JSymbolTable getSymbolTable()
        Description copied from interface: JavaNode
        Returns the symbol table for the program point represented by this node.
        Specified by:
        getSymbolTable in interface JavaNode
      • getTypeSystem

        public TypeSystem getTypeSystem()
        Description copied from interface: JavaNode
        Returns the type system with which this node was created. This is the object responsible for representing types in the compilation unit.
        Specified by:
        getTypeSystem in interface JavaNode