Class ASTVariableId
- java.lang.Object
-
- net.sourceforge.pmd.lang.ast.impl.AbstractNode<B,N>
-
- 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:
GenericNode<JavaNode>
,JjtreeNode<JavaNode>
,Node
,TextAvailableNode
,Annotatable
,JavaNode
,ModifierOwner
,SymbolDeclaratorNode
,TypeNode
,Reportable
public final class ASTVariableId extends 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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.sourceforge.pmd.lang.java.ast.ModifierOwner
ModifierOwner.Visibility
-
-
Field Summary
Fields Modifier and Type Field Description protected JSymbolTable
symbolTable
-
Fields inherited from class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode
id
-
Fields inherited from interface net.sourceforge.pmd.lang.ast.Node
COORDS_COMPARATOR
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <P,R>
RacceptVisitor(AstVisitor<? super P,? extends R> visitor, P data)
protected <P,R>
RacceptVisitor(JavaVisitor<? super P,? extends R> visitor, P data)
protected void
addChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
@Nullable ASTArrayDimensions
getExtraDimensions()
Returns the extra array dimensions associated with this variable.@Nullable ASTExpression
getInitializer()
Returns the initializer of the variable, or null if it doesn't exist.List<ASTAssignableExpr.ASTNamedReferenceExpr>
getLocalUsages()
Returns an unmodifiable list of the usages of this variable that are made in this file.@NonNull ASTModifierList
getModifiers()
Returns the node representing the modifier list of this node.String
getName()
Returns the name of the variable.@NonNull ASTCompilationUnit
getRoot()
@NonNull T
getSymbol()
Returns the symbol this node declares.@NonNull JSymbolTable
getSymbolTable()
Returns the symbol table for the program point represented by this node.@NonNull JTypeMirror
getTypeMirror()
Returns the type of the declared variable.@NonNull JTypeMirror
getTypeMirror(TypingContext context)
@Nullable Node
getTypeNameNode()
Returns the first child of the node returned bygetTypeNode()
.@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).TypeSystem
getTypeSystem()
Returns the type system with which this node was created.ModifierOwner.Visibility
getVisibility()
Returns the visibility corresponding to theeffective modifiers
.String
getXPathNodeName()
boolean
hasArrayType()
Returns true if the declared variable has an array type.protected void
insertChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
boolean
isEnumConstant()
Returns true if this node declares an enum constant.boolean
isExceptionBlockParameter()
Returns true if this nodes declares an exception parameter in acatch
statement.boolean
isField()
Returns true if this node declares a field from a regularASTFieldDeclaration
.boolean
isFinal()
Return true if the declared variable is static.boolean
isForeachVariable()
Returns true if this node is a variable declared in a foreach loop.boolean
isForLoopVariable()
Returns true if this node is a variable declared in the init clause of a for loop.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 from within a regularASTLocalVariableDeclaration
.boolean
isPatternBinding()
Returns true if this is a binding variable in a pattern.boolean
isRecordComponent()
Returns true if this node declares a record component.boolean
isResourceDeclaration()
Returns true if this declarator id declares a resource in a try-with-resources statement.boolean
isStatic()
Return true if the declared variable is static.boolean
isTypeInferred()
Returns true if the declared variable's type is inferred by the compiler.boolean
isUnnamed()
Return true if this variable has no name.void
jjtClose()
protected void
removeChildAtIndex(int childIndex)
protected void
setChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
protected void
setFirstToken(JavaccToken token)
protected void
setImage(String image)
protected void
setLastToken(JavaccToken token)
-
Methods inherited from class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode
compareLocation, fitTokensToChildren, getFirstToken, getImage, getLastToken, getTextRegion, jjtOpen, toString
-
Methods inherited from class net.sourceforge.pmd.lang.ast.impl.AbstractNode
children, firstChild, getChild, getIndexInParent, getNumChildren, getParent, getUserMap, remove, setParent
-
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.java.ast.Annotatable
getAnnotation, isAnnotationPresent, isAnnotationPresent, isAnyAnnotationPresent
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.impl.GenericNode
ancestors, ancestorsOrSelf, asStream, children, descendants, descendantsOrSelf, getChild, getFirstChild, getLastChild, getNextSibling, getParent, getPreviousSibling
-
Methods inherited from interface net.sourceforge.pmd.lang.java.ast.JavaNode
getEnclosingType, getRoot, getSymbolTable, getTypeSystem
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode
getFirstToken, getLastToken, tokens
-
Methods inherited from interface net.sourceforge.pmd.lang.java.ast.ModifierOwner
getDeclaredAnnotations, getEffectiveVisibility, hasExplicitModifiers, hasModifiers, hasVisibility
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.Node
acceptVisitor, ancestors, children, compareLocation, descendants, firstChild, getAstInfo, getBeginColumn, getBeginLine, getEndColumn, getEndLine, getImage, getIndexInParent, getLanguageVersion, getNumChildren, getReportLocation, getTextDocument, getUserMap, getXPathAttributesIterator, getXPathNodeName, hasImageEqualTo, isFindBoundary
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.TextAvailableNode
getOriginalText, getText, getTextRegion
-
-
-
-
Field Detail
-
symbolTable
protected JSymbolTable symbolTable
-
-
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 declarationint a[]
,getTypeNode()
returnsint
, 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 interfaceModifierOwner
-
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 theeffective modifiers
. Eg a public method will have visibilitypublic
, a local class will have visibilitylocal
. There cannot be any conflict withModifierOwner.hasModifiers(JModifier, JModifier...)
} on well-formed code (e.g. for anyn
,(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 interfaceModifierOwner
-
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.
-
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 acatch
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 regularASTLocalVariableDeclaration
.
-
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, seeisTypeInferred()
.
-
isField
public boolean isField()
Returns true if this node declares a field from a regularASTFieldDeclaration
. This returns false for enum constants (usegetSymbol().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()
returnsnull
, 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 bygetTypeNode()
. 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. ifisTypeInferred()
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 interfaceTypeNode
- Returns:
- The type mirror. Never returns null; if the type is unresolved, returns
TypeSystem.UNKNOWN
.
- 1. not necessarily the same as the type written out at the
start of the declaration, e.g.
-
getSymbol
public @NonNull T getSymbol()
Description copied from interface:SymbolDeclaratorNode
Returns the symbol this node declares.- Specified by:
getSymbol
in interfaceSymbolDeclaratorNode
-
getTypeMirror
public @NonNull JTypeMirror getTypeMirror(TypingContext context)
- Specified by:
getTypeMirror
in interfaceTypeNode
-
jjtClose
public void jjtClose()
- Overrides:
jjtClose
in classAbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
-
acceptVisitor
public final <P,R> R acceptVisitor(AstVisitor<? super P,? extends R> visitor, P data)
- Specified by:
acceptVisitor
in interfaceNode
-
addChild
protected void addChild(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode child, int index)
- Overrides:
addChild
in classAbstractJjtreeNode<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 classAbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
-
removeChildAtIndex
protected void removeChildAtIndex(int childIndex)
- Overrides:
removeChildAtIndex
in classAbstractNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
-
setImage
protected void setImage(String image)
- Overrides:
setImage
in classAbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
-
setFirstToken
protected void setFirstToken(JavaccToken token)
- Overrides:
setFirstToken
in classAbstractJjtreeNode<net.sourceforge.pmd.lang.java.ast.AbstractJavaNode,JavaNode>
-
setLastToken
protected void setLastToken(JavaccToken token)
- Overrides:
setLastToken
in classAbstractJjtreeNode<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 classAbstractNode<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 interfaceJavaNode
-
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 interfaceJavaNode
-
getRoot
public final @NonNull ASTCompilationUnit getRoot()
-
getXPathNodeName
public final String getXPathNodeName()
- Specified by:
getXPathNodeName
in interfaceNode
-
-