Class ASTVariableId

java.lang.Object
net.sourceforge.pmd.lang.ast.impl.AbstractNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
net.sourceforge.pmd.lang.java.ast.ASTVariableId
All Implemented Interfaces:
net.sourceforge.pmd.lang.ast.impl.GenericNode<JavaNode>, net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode<JavaNode>, net.sourceforge.pmd.lang.ast.Node, net.sourceforge.pmd.lang.ast.TextAvailableNode, Annotatable, JavaNode, ModifierOwner, SymbolDeclaratorNode, TypeNode, net.sourceforge.pmd.reporting.Reportable

public final class ASTVariableId extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode> implements ModifierOwner
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 and enum constant declarations;
  • Local variable declarations;
  • Method, constructor and lambda 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, it owns a JVariableSymbol and can provide access to variable usages.


 VariableId ::= <IDENTIFIER> ArrayDimensions?

 

Note: This node has been called ASTVariableDeclaratorId in PMD 6.

  • Field Details

  • Method Details

    • acceptVisitor

      protected <P, R> R acceptVisitor(JavaVisitor<? super P,? extends R> visitor, P data)
    • 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.
    • getModifiers

      public @NonNull ASTModifierList getModifiers()
      Description copied from interface: ModifierOwner
      Returns the node representing the modifier list of this node.
      Specified by:
      getModifiers in interface ModifierOwner
    • 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
    • getVisibility

      public ModifierOwner.Visibility getVisibility()
      Description copied from interface: ModifierOwner
      Returns the visibility corresponding to the effective modifiers. Eg a public method will have visibility public, a local class will have visibility local. There cannot be any conflict with ModifierOwner.hasModifiers(JModifier, JModifier...)} on well-formed code (e.g. for any n, (n.getVisibility() == V_PROTECTED) == n.hasModifiers(PROTECTED))

      TODO a public method of a private class can be considered to be private we could probably add another method later on that takes this into account

      Specified by:
      getVisibility in interface ModifierOwner
    • 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:
    • 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().
    • isField

      public boolean isField()
      Returns true if this node declares a field from a regular ASTFieldDeclaration. This returns false for enum constants (use getSymbol().isField() if you want that).
    • 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 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 @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.
    • getSymbol

      public JVariableSymbol getSymbol()
      Description copied from interface: SymbolDeclaratorNode
      Returns the symbol this node declares.
      Specified by:
      getSymbol in interface SymbolDeclaratorNode
    • getTypeMirror

      public @NonNull JTypeMirror getTypeMirror(TypingContext context)
      Specified by:
      getTypeMirror in interface TypeNode
    • jjtClose

      public void jjtClose()
      Overrides:
      jjtClose in class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • acceptVisitor

      public final <P, R> R acceptVisitor(net.sourceforge.pmd.lang.ast.AstVisitor<? super P,? extends R> visitor, P data)
      Specified by:
      acceptVisitor in interface net.sourceforge.pmd.lang.ast.Node
    • addChild

      protected void addChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
      Overrides:
      addChild in class net.sourceforge.pmd.lang.ast.impl.javacc.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 net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • removeChildAtIndex

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

      protected void setImage(String image)
      Overrides:
      setImage in class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • setFirstToken

      protected void setFirstToken(net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken token)
      Overrides:
      setFirstToken in class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • setLastToken

      protected void setLastToken(net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken token)
      Overrides:
      setLastToken in class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
    • enlargeLeft

      protected void enlargeLeft(net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken child)
      Overrides:
      enlargeLeft in class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<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 net.sourceforge.pmd.lang.ast.impl.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
    • getRoot

      public final @NonNull ASTCompilationUnit getRoot()
      Specified by:
      getRoot in interface JavaNode
      Specified by:
      getRoot in interface net.sourceforge.pmd.lang.ast.Node
    • getXPathNodeName

      public final String getXPathNodeName()
      Specified by:
      getXPathNodeName in interface net.sourceforge.pmd.lang.ast.Node