Class ASTVariableDeclaratorId
- java.lang.Object
-
- net.sourceforge.pmd.lang.ast.AbstractNode
-
- net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode<JavaNode>
-
- net.sourceforge.pmd.lang.java.ast.AbstractJavaNode
-
- net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode
-
- net.sourceforge.pmd.lang.java.ast.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 methodgetUsages()
.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.java.ast.AbstractJavaNode
parser
-
Fields inherited from class net.sourceforge.pmd.lang.ast.AbstractNode
beginColumn, beginLine, childIndex, children, endColumn, endLine, firstToken, id, lastToken, parent
-
-
Constructor Summary
Constructors Constructor Description ASTVariableDeclaratorId(int id)
Deprecated.ASTVariableDeclaratorId(JavaParser p, int id)
Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
bumpArrayDepth()
Deprecated.int
getArrayDepth()
Deprecated.String
getImage()
Deprecated.UsegetName()
String
getName()
Returns the name of the variable.VariableNameDeclaration
getNameDeclaration()
Note: this might benull
in certain cases.Class<?>
getType()
Returns the type of the declared variable.Node
getTypeNameNode()
Returns the first child of the node returned bygetTypeNode()
.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).List<NameOccurrence>
getUsages()
String
getVariableName()
Deprecated.UsegetName()
boolean
hasArrayType()
Returns true if the declared variable has an array type.boolean
isArray()
Deprecated.UsehasArrayType()
boolean
isExceptionBlockParameter()
Returns true if this nodes declares an exception parameter in acatch
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
isForeachVariable()
Returns true if this node is a variable declared in a foreach 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.boolean
isPatternBinding()
Returns true if this is a binding variable in a pattern.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.Object
jjtAccept(JavaParserVisitor visitor, 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.0void
setNameDeclaration(VariableNameDeclaration decl)
Deprecated.-
Methods inherited from class net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode
getTypeDefinition, setType, setTypeDefinition
-
Methods inherited from class net.sourceforge.pmd.lang.java.ast.AbstractJavaNode
childrenAccept, comment, comment, getRoot, getScope, getXPathNodeName, jjtClose, jjtOpen, setScope
-
Methods inherited from class net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode
children, getChild, getParent
-
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, getIndexInParent, getNthParent, getNumChildren, getParentsOfType, getUserData, getUserMap, 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.java.ast.JavaNode
children, childrenAccept, getChild, getParent, getRoot, setScope
-
Methods inherited from interface net.sourceforge.pmd.lang.ast.Node
findChildNodesWithXPath, findChildrenOfType, findDescendantsOfType, findDescendantsOfType, findDescendantsOfType, getAsDocument, getBeginColumn, getBeginLine, getDataFlowNode, getEndColumn, getEndLine, getFirstChildOfType, getFirstDescendantOfType, getFirstParentOfAnyType, getFirstParentOfType, getIndexInParent, getNthParent, getNumChildren, getParentsOfType, getUserData, getUserMap, 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 Object jjtAccept(JavaParserVisitor visitor, 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 interfaceJavaNode
- Overrides:
jjtAccept
in classAbstractJavaNode
- Parameters:
visitor
- Visitor to dispatchdata
- Visit data
-
getNameDeclaration
public VariableNameDeclaration getNameDeclaration()
Note: this might benull
in certain cases.
-
setNameDeclaration
@InternalApi @Deprecated public void setNameDeclaration(VariableNameDeclaration decl)
Deprecated.
-
getUsages
public List<NameOccurrence> getUsages()
-
bumpArrayDepth
@Deprecated public void bumpArrayDepth()
Deprecated.
-
getArrayDepth
@Deprecated public int getArrayDepth()
Deprecated.- Specified by:
getArrayDepth
in interfaceDimensionable
-
isArray
@Deprecated @DeprecatedAttribute(replaceWith="@ArrayType") public boolean isArray()
Deprecated.UsehasArrayType()
Returns true if the declared variable has an array type.- Specified by:
isArray
in interfaceDimensionable
-
getImage
@DeprecatedAttribute(replaceWith="@Name") @Deprecated public String getImage()
Deprecated.UsegetName()
- Specified by:
getImage
in interfaceNode
- Overrides:
getImage
in classAbstractNode
- Returns:
-
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. In particular, returns false if the node is a receiver parameter (seeisExplicitReceiverParameter()
).
-
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, seeisTypeInferred()
.
-
isField
public boolean isField()
Returns true if this node declares a field.
-
getVariableName
@Deprecated @DeprecatedAttribute(replaceWith="@Name") public String getVariableName()
Deprecated.UsegetName()
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 namethis
, 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()
returnsnull
, 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 bygetTypeNode()
. 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. ifisTypeInferred()
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 interfaceTypeNode
- Overrides:
getType
in classAbstractJavaTypeNode
- Returns:
- The Java Class, may return
null
.
- 1. not necessarily the same as the type written out at the
start of the declaration, e.g.
-
-