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:
net.sourceforge.pmd.lang.ast.Node,Dimensionable,JavaNode,TypeNode,net.sourceforge.pmd.lang.symboltable.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
-
-
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 voidbumpArrayDepth()Deprecated.intgetArrayDepth()Deprecated.VariableNameDeclarationgetNameDeclaration()Note: this might benullin certain cases.Class<?>getType()Returns the type of the declared variable.net.sourceforge.pmd.lang.ast.NodegetTypeNameNode()Returns the first child of the node returned bygetTypeNode().ASTTypegetTypeNode()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<net.sourceforge.pmd.lang.symboltable.NameOccurrence>getUsages()StringgetVariableName()Returns the name of the variable.booleanhasArrayType()Returns true if the declared variable has an array type.booleanisArray()Deprecated.UsehasArrayType()booleanisExceptionBlockParameter()Returns true if this nodes declares an exception parameter in acatchstatement.booleanisExplicitReceiverParameter()Returns true if this node is a receiver parameter for a method or constructor declaration.booleanisField()Returns true if this node declares a field.booleanisFinal()Returns true if the variable declared by this node is declared final.booleanisFormalParameter()Returns true if this node declares a formal parameter for a method declaration or a lambda expression.booleanisLambdaParameter()Returns true if this node declares a formal parameter for a lambda expression.booleanisLocalVariable()Returns true if this node declares a local variable.booleanisPatternBinding()Returns true if this is a binding variable in a pattern.booleanisResourceDeclaration()Returns true if this declarator id declares a resource in a try-with-resources statement.booleanisTypeInferred()Returns true if the declared variable's type is inferred by the compiler.ObjectjjtAccept(JavaParserVisitor visitor, Object data)Calls back the visitor's visit method corresponding to the runtime type of this Node.voidsetExplicitReceiverParameter()Deprecated.Will be made private with 7.0.0voidsetNameDeclaration(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, 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, getImage, 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, 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, getImage, 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
-
-
-
-
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:JavaNodeCalls back the visitor's visit method corresponding to the runtime type of this Node.- Specified by:
jjtAcceptin interfaceJavaNode- Overrides:
jjtAcceptin classAbstractJavaNode- Parameters:
visitor- Visitor to dispatchdata- Visit data
-
getNameDeclaration
public VariableNameDeclaration getNameDeclaration()
Note: this might benullin certain cases.
-
setNameDeclaration
@InternalApi @Deprecated public void setNameDeclaration(VariableNameDeclaration decl)
Deprecated.
-
getUsages
public List<net.sourceforge.pmd.lang.symboltable.NameOccurrence> getUsages()
-
bumpArrayDepth
@Deprecated public void bumpArrayDepth()
Deprecated.
-
getArrayDepth
@Deprecated public int getArrayDepth()
Deprecated.- Specified by:
getArrayDepthin interfaceDimensionable
-
isArray
@Deprecated public boolean isArray()
Deprecated.UsehasArrayType()Returns true if the declared variable has an array type.- Specified by:
isArrayin interfaceDimensionable
-
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 acatchstatement.
-
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.
-
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
public String getVariableName()
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
@Experimental public boolean isPatternBinding()
Returns true if this is a binding variable in a pattern.
-
getTypeNameNode
public net.sourceforge.pmd.lang.ast.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
nullif 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:
getTypein interfaceTypeNode- Overrides:
getTypein 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.
-
-