Class TreeUtils
Tree
.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
This is a duplication ofcom.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind
, which is not part of the supported javac API. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic AnnotationMirror
Converts the given AnnotationTree to an AnnotationMirror.static List<? extends AnnotationMirror>
annotationsFromArrayCreation
(NewArrayTree tree, int level) Converts the given NewArrayTree to a list of AnnotationMirrors.static List<? extends AnnotationMirror>
Converts the given AnnotatedTypeTree to a list of AnnotationMirrors.static List<? extends AnnotationMirror>
Converts the given TypeParameterTree to a list of AnnotationMirrors.static List<AnnotationMirror>
annotationsFromTypeAnnotationTrees
(List<? extends AnnotationTree> annoTrees) Converts the given AnnotationTrees to AnnotationMirrors.static @Nullable VariableElement
asFieldAccess
(Tree tree) Return the field thattree
is a field access expression for, or null.static VariableTree
bindingPatternTreeGetVariable
(Tree bindingPatternTree) Deprecated.static boolean
canHaveTypeAnnotation
(Tree tree) Returntrue
if and only iftree
can have a type annotation.caseTreeGetBody
(CaseTree caseTree) Deprecated.static List<? extends ExpressionTree>
caseTreeGetExpressions
(CaseTree caseTree) Deprecated.Returns the set of kinds that represent classes and methods.Return the set of kinds that represent classes.static ExecutableElement
constructor
(NewClassTree tree) Deprecated.use elementFromUse insteadstatic boolean
Returns true if the first statement in the body is a self constructor invocation within a constructor.static LiteralTree
createLiteral
(com.sun.tools.javac.code.TypeTag typeTag, @Nullable Object value, TypeMirror typeMirror, ProcessingEnvironment processingEnv) Creates a LiteralTree for the given value.Return the set of kinds that represent declarations: classes, methods, and variables.static @Nullable TypeElement
Returns the type element corresponding to the given class declaration.Deprecated.Deprecated.use elementFromUsestatic @Nullable ExecutableElement
Deprecated.use elementFromUsestatic @Nullable ExecutableElement
Returns the ExecutableElement for the given method declaration.static ExecutableElement
Deprecated.use elementFromUsestatic @Nullable VariableElement
Returns the VariableElement corresponding to the given variable declaration.static @Nullable TypeElement
elementFromTree
(ClassTree tree) Deprecated.Returns the element corresponding to the given tree.Deprecated.use elementFromUsestatic @Nullable ExecutableElement
Deprecated.use elementFromUsestatic @Nullable ExecutableElement
elementFromTree
(MethodTree tree) Deprecated.use elementFromDeclarationstatic ExecutableElement
elementFromTree
(NewClassTree tree) Deprecated.use elementFromUseelementFromTree
(Tree tree) Returns theElement
for the given Tree API node.static @Nullable VariableElement
elementFromTree
(VariableTree tree) Deprecated.use elementFromDeclarationstatic @Nullable TypeElement
elementFromUse
(ClassTree tree) Deprecated.static Element
elementFromUse
(ExpressionTree tree) Gets the element for the declaration corresponding to this use of an element.static Element
Returns the element for the given expression.static ExecutableElement
Returns the ExecutableElement for the called method.static @Nullable ExecutableElement
elementFromUse
(MethodTree tree) Deprecated.use elementFromDeclarationstatic ExecutableElement
elementFromUse
(NewClassTree tree) Gets the ExecutableElement for the called constructor, from a constructor invocation.static @Nullable VariableElement
elementFromUse
(VariableTree tree) Deprecated.use elementFromDeclarationstatic ExecutableElement
findFunction
(Tree tree, ProcessingEnvironment env) The type of the lambda or method reference tree is a functional interface type.static Tree
firstStatement
(Tree tree) Returns the first statement of the tree if it is a block.static LiteralTree
getDefaultValueTree
(TypeMirror typeMirror, ProcessingEnvironment processingEnv) Return a tree for the default value of the given type.static List<? extends AnnotationTree>
getExplicitAnnotationTrees
(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) Returns the annotations explicitly written on the given type.static VariableElement
getField
(@FullyQualifiedName String typeName, String fieldName, ProcessingEnvironment env) Returns the VariableElement for a field declaration.static String
getFieldName
(Tree tree) Compute the name of the field that the field accesstree
accesses.static Tree.Kind
getKindRecordAsClass
(Tree tree) Calls getKind() on the given tree, but returns CLASS if the Kind is RECORD.static ExecutableElement
getMethod
(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration.static ExecutableElement
getMethod
(@FullyQualifiedName String typeName, String methodName, ProcessingEnvironment env, String... paramTypes) Returns the ExecutableElement for a method declaration.static ExecutableElement
getMethod
(Class<?> type, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration.static ExecutableElement
getMethod
(Class<?> type, String methodName, ProcessingEnvironment env, String... paramTypes) Returns the ExecutableElement for a method declaration.static String
getMethodName
(Tree tree) Compute the name of the method that the method accesstree
accesses.static @Nullable ExecutableElement
getMethodOrNull
(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration.static List<ExecutableElement>
getMethods
(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns all ExecutableElements for method declarations of methodName, in class typeName, with params formal parameters.static @Nullable ExpressionTree
getReceiverTree
(ExpressionTree expression) Returns the receiver tree of a field access or a method invocation.static ExecutableElement
getSuperConstructor
(NewClassTree newClassTree) Returns the constructor invoked bynewClassTree
unlessnewClassTree
is creating an anonymous class.static boolean
Determine whether the given class contains an explicit constructor.static boolean
Returns true iftree
has a synthetic argument.instanceOfTreeGetPattern
(InstanceOfTree instanceOfTree) Deprecated.static boolean
isAnonymousConstructor
(MethodTree method) Returns true if the givenMethodTree
is an anonymous constructor (the constructor for an anonymous class).static boolean
isArrayLengthAccess
(Tree tree) Returns true if tree is an access of array length.static boolean
isAutoGeneratedRecordMember
(Tree member) Returns true if the givenTree
is part of a record that has been automatically generated by the compiler.static boolean
isBinaryComparison
(BinaryTree tree) Returns true if thetree
is a binary tree that performs a comparison.static boolean
isBindingPatternTree
(Tree tree) Returns true iftree
is aBindingPatternTree
.static boolean
isCaseRule
(CaseTree caseTree) Deprecated.static boolean
isClassLiteral
(Tree tree) Determine whethertree
is a class literal, such asstatic boolean
isClassTree
(Tree tree) Is the given tree kind a class, i.e.static boolean
Returns true if the givenMethodTree
is a compact canonical constructor (the constructor for a record where the parameters are implicitly declared and implicitly assigned to the record's fields).static boolean
Returns true if the tree is a constant-time expression.static boolean
isConstantCaseLabelTree
(Tree tree) Returns true iftree
is aConstantCaseLabelTree
.static boolean
isConstructor
(MethodTree tree) Checks if the provided method is a constructor method or no.static boolean
isDeclarationTree
(Tree tree) Returns true if the given tree is a declaration.static boolean
Returns true iftree
is aDeconstructionPatternTree
.static boolean
isDefaultCaseLabelTree
(Tree tree) Returns true iftree
is aDefaultCaseLabelTree
.static boolean
isDefaultCaseTree
(CaseTree caseTree) Returns true if this is the default case for a switch statement or expression.static boolean
isDiamondTree
(Tree tree) Returns true if the tree is of a diamond type.static boolean
Returns true if this is a super call to theEnum
constructor.static boolean
Determine whether the given expression is either "this" or an outer "C.this".static boolean
Determine whether an expressionExpressionTree
has the constant value true, according to the compiler logic.static boolean
isExpressionTree
(Tree tree) Determine whether the given tree represents an ExpressionTree.static boolean
isFieldAccess
(Tree tree) Determine whethertree
is a field access expression, such asstatic boolean
isImplicitlyTypedLambda
(Tree tree) Returns true iftree
is an implicitly typed lambda.static boolean
isLocalVariable
(Tree tree) Returns true if the tree is the declaration or use of a local variable.static boolean
isMethodAccess
(Tree tree) Determine whethertree
refers to a method element, such as.static boolean
isMethodInvocation
(Tree methodTree, List<ExecutableElement> methods, ProcessingEnvironment processingEnv) Returns true if the argument is an invocation of one of the given methods, or of any method that overrides them.static boolean
isMethodInvocation
(Tree tree, ExecutableElement method, ProcessingEnvironment env) Returns true if the given element is an invocation of the method, or of any method that overrides that one.static boolean
Returns true if the given tree evaluates tonull
.static boolean
isPatternCaseLabelTree
(Tree tree) Returns whethertree
is aPatternCaseLabelTree
.static boolean
isSelfAccess
(ExpressionTree tree) Returns true if the tree is a tree that 'looks like' either an access of a field or an invocation of a method that are owned by the same accessing instance.static boolean
isSpecificFieldAccess
(Tree tree, VariableElement var) Returns true if and only if the giventree
represents a field access of the givenVariableElement
.static boolean
Returns true if the compound assignment tree is a string concatenation.static boolean
isStringConcatenation
(Tree tree) Returns true if the tree represents aString
concatenation operation.static boolean
Checks if the method invocation is a call to super.static boolean
isSwitchStatement
(Tree tree) Returns true if the given tree is a switch statement (as opposed to a switch expression).static boolean
isSynthetic
(MethodTree tree) Returns true if the given method is synthetic.static boolean
Returns true if the given method is synthetic.static boolean
Checks if the method invocation is a call to "this".static boolean
isTypeDeclaration
(Tree tree) Determine whether the given tree represents a declaration of a type (including type parameters).static boolean
isTypeTree
(Tree tree) Is the given tree a type instantiation?static boolean
isUseOfElement
(ExpressionTree tree) Determine whether the given ExpressionTree has an underlying element.static boolean
isVarArgs
(MethodInvocationTree invok) Returns true if the given method invocation is a varargs invocation.static boolean
isVarArgs
(NewClassTree newClassTree) Returns true if the given constructor invocation is a varargs invocation.static boolean
Returns true if the given method/constructor invocation is a varargs invocation.static boolean
isVariableTreeDeclaredUsingVar
(VariableTree variableTree) Returns true if thevariableTree
is declared using thevar
Java keyword.static boolean
isVoidReturn
(MethodTree tree) Is this method's declared return type "void"?static boolean
isWideningBinary
(BinaryTree tree) Returns true if the binary operator may do a widening primitive conversion.static Name
Returns the name of the invoked method.static String
nameExpressionToString
(ExpressionTree nameExpr) Given a javac ExpressionTree representing a fully qualified name such as "java.lang.Object", creates a String containing the name.static boolean
sameTree
(ExpressionTree expr1, ExpressionTree expr2) Returns true if two expressions originating from the same scope are identical, i.e.static com.sun.tools.javac.tree.JCTree.JCFieldAccess
Select
(com.sun.tools.javac.tree.TreeMaker treeMaker, Tree base, com.sun.tools.javac.code.Symbol sym) Returns the result oftreeMaker.Select(base, sym)
.static com.sun.tools.javac.tree.JCTree.JCFieldAccess
Returns the result oftreeMaker.Select(base, name)
.switchExpressionTreeGetCases
(Tree switchExpressionTree) Deprecated.static ExpressionTree
switchExpressionTreeGetExpression
(Tree switchExpressionTree) Deprecated.static String
toStringOneLine
(Tree tree) Return toString(), but without line separators.static String
toStringTruncated
(Tree tree, int length) Return eithertoStringOneLine(com.sun.source.tree.Tree)
if it is no more thanlength
characters, ortoStringOneLine(com.sun.source.tree.Tree)
quoted and truncated.static ExecutableType
Determines the type for a method invocation at its call site, which has all type variables substituted with the type arguments at the call site.static ExecutableType
typeFromUse
(NewClassTree tree) Determines the type for a constructor at its call site given an invocation vianew
, which has all type variables substituted with the type arguments at the call site.static TypeMirror
Returns the type as a TypeMirror oftree
.Return the set of kinds that represent types.static VariableElement
variableElementFromTree
(Tree tree) Returns theVariableElement
for the given Tree API node.static VariableElement
Returns the VariableElement corresponding to the given use.static @PolyInterned ExpressionTree
If the given tree is a parenthesized tree, return the enclosed non-parenthesized tree.static @PolyInterned ExpressionTree
If the given tree is a parenthesized tree or cast tree, return the enclosed non-parenthesized, non-cast tree.static ExpressionTree
yieldTreeGetValue
(Tree yieldTree) Deprecated.
-
Field Details
-
treeUids
Unique IDs for trees. Used instead of hash codes, so output is deterministic.
-
-
Method Details
-
isConstructor
Checks if the provided method is a constructor method or no.- Parameters:
tree
- a tree defining the method- Returns:
- true iff tree describes a constructor
-
isSuperConstructorCall
Checks if the method invocation is a call to super.- Parameters:
tree
- a tree defining a method invocation- Returns:
- true iff tree describes a call to super
-
isThisConstructorCall
Checks if the method invocation is a call to "this".- Parameters:
tree
- a tree defining a method invocation- Returns:
- true iff tree describes a call to this
-
isSelfAccess
Returns true if the tree is a tree that 'looks like' either an access of a field or an invocation of a method that are owned by the same accessing instance.It would only return true if the access tree is of the form:
field this.field method() this.method()
It does not perform any semantical check to differentiate between fields and local variables; local methods or imported static methods.- Parameters:
tree
- expression tree representing an access to object member- Returns:
true
iff the member is a member ofthis
instance
-
withoutParens
If the given tree is a parenthesized tree, return the enclosed non-parenthesized tree. Otherwise, return the same tree.- Parameters:
tree
- an expression tree- Returns:
- the outermost non-parenthesized tree enclosed by the given tree
-
withoutParensOrCasts
If the given tree is a parenthesized tree or cast tree, return the enclosed non-parenthesized, non-cast tree. Otherwise, return the same tree.- Parameters:
tree
- an expression tree- Returns:
- the outermost non-parenthesized non-cast tree enclosed by the given tree
-
elementFromDeclaration
Returns the type element corresponding to the given class declaration.- Parameters:
tree
- class declaration- Returns:
- the element for the given class
-
elementFromTree
Deprecated.Returns the type element corresponding to the given class declaration.The TypeElement may be null for an anonymous class.
- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the
Symbol
for the given tree, or null if one could not be found
-
elementFromUse
Deprecated.Returns the type element corresponding to the given class declaration.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the
Symbol
for the given tree, or null if one could not be found
-
elementFromDeclaration
Deprecated.Returns the element corresponding to the given tree.- Parameters:
tree
- the tree corresponding to a use of an element- Returns:
- the element for the corresponding declaration,
null
otherwise
-
elementFromTree
Returns the element corresponding to the given tree.- Parameters:
tree
- the tree corresponding to a use of an element- Returns:
- the element for the corresponding declaration,
null
otherwise
-
elementFromUse
Gets the element for the declaration corresponding to this use of an element. To get the element for a declaration, useelementFromDeclaration(ClassTree)
,elementFromDeclaration(MethodTree)
, orelementFromDeclaration(VariableTree)
instead.This method is just a wrapper around
elementFromTree(Tree)
, but this class might be the first place someone looks for this functionality.- Parameters:
tree
- the tree, which must be a use of an element- Returns:
- the element for the corresponding declaration
-
variableElementFromUse
Returns the VariableElement corresponding to the given use.- Parameters:
tree
- the tree corresponding to a use of a VariableElement- Returns:
- the element for the corresponding declaration
-
elementFromDeclaration
Deprecated.use elementFromUseReturns the element for the given expression.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the element for the given tree, or null if one could not be found
-
elementFromTree
Deprecated.use elementFromUseReturns the element for the given expression.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the element for the given tree, or null if one could not be found
-
elementFromUse
Returns the element for the given expression.- Parameters:
tree
- a method call- Returns:
- the element for the called method
-
elementFromDeclaration
@Deprecated @Pure public static @Nullable ExecutableElement elementFromDeclaration(MethodInvocationTree tree) Deprecated.use elementFromUseReturns the ExecutableElement for the called method.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the Element for the given tree, or null if one could not be found
-
elementFromTree
@Deprecated @Pure public static @Nullable ExecutableElement elementFromTree(MethodInvocationTree tree) Deprecated.use elementFromUseReturns the ExecutableElement for the called method.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the Element for the given tree, or null if one could not be found
-
elementFromUse
Returns the ExecutableElement for the called method.- Parameters:
tree
- a method call- Returns:
- the ExecutableElement for the called method
-
elementFromDeclaration
Returns the ExecutableElement for the given method declaration.The result can be null, when
tree
is a method in an anonymous class.- Parameters:
tree
- a method declaration- Returns:
- the element for the given method, or null (e.g. for a method in an anonymous class)
-
elementFromTree
Deprecated.use elementFromDeclarationReturns the ExecutableElement for the given method declaration.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the Element for the given tree, or null if one could not be found
-
elementFromUse
Deprecated.use elementFromDeclarationReturns the ExecutableElement for the given method declaration.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the Element for the given tree, or null if one could not be found
-
elementFromDeclaration
Deprecated.use elementFromUseReturns the ExecutableElement for the given constructor invocation.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the
Symbol
for the given tree, or null if one could not be found - Throws:
IllegalArgumentException
- iftree
is null or is not a valid javac-internal tree (JCTree)
-
elementFromTree
Deprecated.use elementFromUseGets the ExecutableElement for the called constructor, from a constructor invocation.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the
Symbol
for the given tree, or null if one could not be found - Throws:
IllegalArgumentException
- iftree
is null or is not a valid javac-internal tree (JCTree)
-
elementFromUse
Gets the ExecutableElement for the called constructor, from a constructor invocation.- Parameters:
tree
- a constructor invocation- Returns:
- the ExecutableElement for the called constructor
- See Also:
-
elementFromDeclaration
Returns the VariableElement corresponding to the given variable declaration.- Parameters:
tree
- the variable- Returns:
- the element for the given variable
-
elementFromTree
Deprecated.use elementFromDeclarationReturns the VariableElement corresponding to the given variable declaration.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the Element for the given tree, or null if one could not be found
-
elementFromUse
Deprecated.use elementFromDeclarationReturns the VariableElement corresponding to the given variable declaration.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the Element for the given tree, or null if one could not be found
-
variableElementFromTree
Returns theVariableElement
for the given Tree API node.- Parameters:
tree
- theTree
node to get the symbol for- Returns:
- the
Symbol
for the given tree - Throws:
IllegalArgumentException
- iftree
is null or is not a valid javac-internal tree (JCTree)
-
elementFromTree
Returns theElement
for the given Tree API node. For an object instantiation returns the value of theJCTree.JCNewClass.constructor
field.Use this only when you do not statically know whether the tree is a declaration or a use of an element.
-
getSuperConstructor
Returns the constructor invoked bynewClassTree
unlessnewClassTree
is creating an anonymous class. In which case, the super constructor is returned.- Parameters:
newClassTree
- the constructor invocation- Returns:
- the super constructor invoked in the body of the anonymous constructor; or
constructor(NewClassTree)
ifnewClassTree
is not creating an anonymous class
-
typeFromUse
Determines the type for a method invocation at its call site, which has all type variables substituted with the type arguments at the call site.TypeVariable
in the returned type should be compared usingTypesUtils.areSame(TypeVariable, TypeVariable)
because theTypeVariable
will be freshly created by this method and will not be the same usingObject.equals(Object)
orTypes.isSameType(TypeMirror, TypeMirror)
.- Parameters:
tree
- the method invocation- Returns:
- the
ExecutableType
corresponding to the method invocation at its call site
-
typeFromUse
Determines the type for a constructor at its call site given an invocation vianew
, which has all type variables substituted with the type arguments at the call site.- Parameters:
tree
- the constructor invocation- Returns:
- the
ExecutableType
corresponding to the constructor call (i.e., the giventree
) at its call site
-
constructor
Deprecated.use elementFromUse insteadDetermines the symbol for a constructor given an invocation vianew
.- Parameters:
tree
- the constructor invocation- Returns:
- the
ExecutableElement
corresponding to the constructor call intree
- See Also:
-
isUseOfElement
@EnsuresNonNullIf(result=true,expression="elementFromTree(#1)") @EnsuresNonNullIf(result=true,expression="elementFromUse(#1)") @Pure public static boolean isUseOfElement(ExpressionTree tree) Determine whether the given ExpressionTree has an underlying element.- Parameters:
tree
- the ExpressionTree to test- Returns:
- whether the tree refers to an identifier, member select, or method invocation
-
hasSyntheticArgument
Returns true iftree
has a synthetic argument.For some anonymous classes with an explicit enclosing expression, javac creates a synthetic argument to the constructor that is the enclosing expression of the NewClassTree. Suppose a programmer writes:
Java 9 javac creates the following synthetic tree forclass Outer { class Inner { } void method() { this.new Inner(){}; } }
this.new Inner(){}
:
Java 11 javac creates a different tree without the synthetic argument fornew Inner(this) { (.Outer x0) { x0.super(); } }
this.new Inner(){}
; the first line in the below code differs:this.new Inner() { (.Outer x0) { x0.super(); } }
- Parameters:
tree
- a new class tree- Returns:
- true if
tree
has a synthetic argument
-
methodName
Returns the name of the invoked method.- Parameters:
tree
- the method invocation- Returns:
- the name of the invoked method
-
containsThisConstructorInvocation
Returns true if the first statement in the body is a self constructor invocation within a constructor.- Parameters:
tree
- the method declaration- Returns:
- true if the first statement in the body is a self constructor invocation within a constructor
-
firstStatement
Returns the first statement of the tree if it is a block. If it is not a block or an empty block, tree is returned.- Parameters:
tree
- any kind of tree- Returns:
- the first statement of the tree if it is a block. If it is not a block or an empty block, tree is returned.
-
hasExplicitConstructor
Determine whether the given class contains an explicit constructor.- Parameters:
tree
- a class tree- Returns:
- true iff there is an explicit constructor
-
isSynthetic
Returns true if the given method is synthetic. Also returns true if the method is a generated default constructor, which does not appear in source code but is not considered synthetic.- Parameters:
ee
- a method or constructor element- Returns:
- true iff the given method is synthetic
-
isSynthetic
Returns true if the given method is synthetic.- Parameters:
tree
- a method declaration tree- Returns:
- true iff the given method is synthetic
-
isDiamondTree
Returns true if the tree is of a diamond type. In contrast to the implementation in TreeInfo, this version works on Trees.- See Also:
-
TreeInfo.isDiamond(JCTree)
-
isStringConcatenation
Returns true if the tree represents aString
concatenation operation. -
isStringCompoundConcatenation
Returns true if the compound assignment tree is a string concatenation. -
isVoidReturn
Is this method's declared return type "void"?- Parameters:
tree
- a method declaration- Returns:
- true iff method's declared return type is "void"
-
isCompileTimeString
Returns true if the tree is a constant-time expression.A tree is a constant-time expression if it is:
- a literal tree
- a reference to a final variable initialized with a compile time constant
- a String concatenation of two compile time constants
- Parameters:
tree
- the tree to check- Returns:
- true if the tree is a constant-time expression
-
getReceiverTree
Returns the receiver tree of a field access or a method invocation.- Parameters:
expression
- a field access or a method invocation- Returns:
- the expression's receiver tree, or null if it does not have an explicit receiver
-
classAndMethodTreeKinds
Returns the set of kinds that represent classes and methods.- Returns:
- the set of kinds that represent classes and methods
-
classTreeKinds
Return the set of kinds that represent classes.- Returns:
- the set of kinds that represent classes
-
isClassTree
Is the given tree kind a class, i.e. a class, enum, interface, or annotation type.- Parameters:
tree
- the tree to test- Returns:
- true, iff the given kind is a class kind
-
declarationTreeKinds
Return the set of kinds that represent declarations: classes, methods, and variables.- Returns:
- the set of kinds that represent declarations
-
isDeclarationTree
Returns true if the given tree is a declaration.- Parameters:
tree
- the tree to test- Returns:
- true if the given tree is a declaration
-
typeTreeKinds
Return the set of kinds that represent types.- Returns:
- the set of kinds that represent types
-
isTypeTree
Is the given tree a type instantiation?TODO: this is an under-approximation: e.g. an identifier could be either a type use or an expression. How can we distinguish.
- Parameters:
tree
- the tree to test- Returns:
- true, iff the given tree is a type
-
isMethodInvocation
public static boolean isMethodInvocation(Tree tree, ExecutableElement method, ProcessingEnvironment env) Returns true if the given element is an invocation of the method, or of any method that overrides that one. -
isMethodInvocation
public static boolean isMethodInvocation(Tree methodTree, List<ExecutableElement> methods, ProcessingEnvironment processingEnv) Returns true if the argument is an invocation of one of the given methods, or of any method that overrides them. -
getMethod
public static ExecutableElement getMethod(Class<?> type, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration. Errs if there is not exactly one matching method. If more than one method takes the same number of formal parameters, then usegetMethod(String, String, ProcessingEnvironment, String...)
.- Parameters:
type
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environment- Returns:
- the ExecutableElement for the specified method
-
getMethod
public static ExecutableElement getMethod(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration. Errs if there is not exactly one matching method. If more than one method takes the same number of formal parameters, then usegetMethod(String, String, ProcessingEnvironment, String...)
.- Parameters:
typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environment- Returns:
- the ExecutableElement for the specified method
-
getMethodOrNull
public static @Nullable ExecutableElement getMethodOrNull(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration. Returns null there is no matching method. Errs if there is more than one matching method. If more than one method takes the same number of formal parameters, then usegetMethod(String, String, ProcessingEnvironment, String...)
.- Parameters:
typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environment- Returns:
- the ExecutableElement for the specified method, or null
-
getMethods
public static List<ExecutableElement> getMethods(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns all ExecutableElements for method declarations of methodName, in class typeName, with params formal parameters.- Parameters:
typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environment- Returns:
- the ExecutableElements for all matching methods
-
getMethod
public static ExecutableElement getMethod(Class<?> type, String methodName, ProcessingEnvironment env, String... paramTypes) Returns the ExecutableElement for a method declaration. Errs if there is no matching method.- Parameters:
type
- the class that contains the methodmethodName
- the name of the methodenv
- the processing environmentparamTypes
- the method's formal parameter types- Returns:
- the ExecutableElement for the specified method
-
getMethod
public static ExecutableElement getMethod(@FullyQualifiedName String typeName, String methodName, ProcessingEnvironment env, String... paramTypes) Returns the ExecutableElement for a method declaration. Errs if there is no matching method.- Parameters:
typeName
- the class that contains the methodmethodName
- the name of the methodenv
- the processing environmentparamTypes
- the method's formal parameter types- Returns:
- the ExecutableElement for the specified method
-
isExplicitThisDereference
Determine whether the given expression is either "this" or an outer "C.this".TODO: Should this also handle "super"?
-
isClassLiteral
Determine whethertree
is a class literal, such asObject . class
- Returns:
- true iff if tree is a class literal
-
isFieldAccess
Determine whethertree
is a field access expression, such asf obj . f
This method currently also returns true for class literals and qualified this.- Parameters:
tree
- a tree that might be a field access- Returns:
- true iff if tree is a field access expression (implicit or explicit)
-
asFieldAccess
Return the field thattree
is a field access expression for, or null.f obj . f
This method currently also returns non-null true for class literals and qualified this.- Parameters:
tree
- a tree that might be a field access- Returns:
- the element if tree is a field access expression (implicit or explicit); null otherwise
-
getFieldName
Compute the name of the field that the field accesstree
accesses. Requirestree
to be a field access, as determined byisFieldAccess
(which currently also returns true for class literals and qualified this).- Parameters:
tree
- a field access tree- Returns:
- the name of the field accessed by
tree
-
isMethodAccess
Determine whethertree
refers to a method element, such as.m(...) obj . m(...)
- Returns:
- true iff if tree is a method access expression (implicit or explicit)
-
getMethodName
Compute the name of the method that the method accesstree
accesses. Requirestree
to be a method access, as determined byisMethodAccess
.- Parameters:
tree
- a method access tree- Returns:
- the name of the method accessed by
tree
-
canHaveTypeAnnotation
Returntrue
if and only iftree
can have a type annotation.- Returns:
true
if and only iftree
can have a type annotation
-
isSpecificFieldAccess
Returns true if and only if the giventree
represents a field access of the givenVariableElement
. -
getField
public static VariableElement getField(@FullyQualifiedName String typeName, String fieldName, ProcessingEnvironment env) Returns the VariableElement for a field declaration.- Parameters:
typeName
- the class where the field is declaredfieldName
- the name of the fieldenv
- the processing environment- Returns:
- the VariableElement for typeName.fieldName
-
isExpressionTree
Determine whether the given tree represents an ExpressionTree.- Parameters:
tree
- the Tree to test- Returns:
- whether the tree is an ExpressionTree
-
isEnumSuperCall
Returns true if this is a super call to theEnum
constructor.- Parameters:
tree
- the method invocation to check- Returns:
- true if this is a super call to the
Enum
constructor
-
isTypeDeclaration
Determine whether the given tree represents a declaration of a type (including type parameters).- Parameters:
tree
- the Tree to test- Returns:
- true if the tree is a type declaration
-
isArrayLengthAccess
Returns true if tree is an access of array length.- Parameters:
tree
- tree to check- Returns:
- true if tree is an access of array length
-
isAnonymousConstructor
Returns true if the givenMethodTree
is an anonymous constructor (the constructor for an anonymous class).- Parameters:
method
- a method tree that may be an anonymous constructor- Returns:
- true if the given path points to an anonymous constructor, false if it does not
-
isCompactCanonicalRecordConstructor
Returns true if the givenMethodTree
is a compact canonical constructor (the constructor for a record where the parameters are implicitly declared and implicitly assigned to the record's fields). This may be an explicitly declared compact canonical constructor or an implicitly generated one.- Parameters:
method
- a method tree that may be a compact canonical constructor- Returns:
- true if the given method is a compact canonical constructor
-
isAutoGeneratedRecordMember
Returns true if the givenTree
is part of a record that has been automatically generated by the compiler. This can be a field that is derived from the record's header field list, or an automatically generated canonical constructor.- Parameters:
member
- theTree
for a member of a record- Returns:
- true if the given path is generated by the compiler
-
annotationsFromTypeAnnotationTrees
public static List<AnnotationMirror> annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annoTrees) Converts the given AnnotationTrees to AnnotationMirrors.- Parameters:
annoTrees
- list of annotation trees to convert to annotation mirrors- Returns:
- list of annotation mirrors that represent the given annotation trees
-
annotationFromAnnotationTree
Converts the given AnnotationTree to an AnnotationMirror.- Parameters:
tree
- annotation tree to convert to an annotation mirror- Returns:
- annotation mirror that represent the given annotation tree
-
annotationsFromTree
Converts the given AnnotatedTypeTree to a list of AnnotationMirrors.- Parameters:
tree
- annotated type tree to convert- Returns:
- list of AnnotationMirrors from the tree
-
annotationsFromTree
Converts the given TypeParameterTree to a list of AnnotationMirrors.- Parameters:
tree
- type parameter tree to convert- Returns:
- list of AnnotationMirrors from the tree
-
annotationsFromArrayCreation
public static List<? extends AnnotationMirror> annotationsFromArrayCreation(NewArrayTree tree, int level) Converts the given NewArrayTree to a list of AnnotationMirrors.- Parameters:
tree
- new array tree- Returns:
- list of AnnotationMirrors from the tree
-
isLocalVariable
Returns true if the tree is the declaration or use of a local variable.- Parameters:
tree
- the tree to check- Returns:
- true if the tree is the declaration or use of a local variable
-
typeOf
Returns the type as a TypeMirror oftree
. To obtaintree
's AnnotatedTypeMirror, callAnnotatedTypeFactory.getAnnotatedType()
.- Returns:
- the type as a TypeMirror of
tree
-
findFunction
The type of the lambda or method reference tree is a functional interface type. This method returns the single abstract method declared by that functional interface. (The type of this method is referred to as the function type.)- Parameters:
tree
- lambda or member reference treeenv
- the processing environment- Returns:
- the single abstract method declared by the type of the tree
-
isImplicitlyTypedLambda
Returns true iftree
is an implicitly typed lambda.A lambda expression whose formal type parameters have inferred types is an implicitly typed lambda. (See JLS 15.27.1)
- Parameters:
tree
- any kind of tree- Returns:
- true iff
tree
is an implicitly typed lambda
-
isExprConstTrue
Determine whether an expressionExpressionTree
has the constant value true, according to the compiler logic.- Parameters:
tree
- the expression to be checked- Returns:
- true if
tree
has the constant value true
-
toStringOneLine
Return toString(), but without line separators.- Parameters:
tree
- a tree- Returns:
- a one-line string representation of the tree
-
toStringTruncated
Return eithertoStringOneLine(com.sun.source.tree.Tree)
if it is no more thanlength
characters, ortoStringOneLine(com.sun.source.tree.Tree)
quoted and truncated.- Parameters:
tree
- a treelength
- the maximum length for the result; must be at least 6- Returns:
- a one-line string representation of the tree that is no longer than
length
characters long
-
nameExpressionToString
Given a javac ExpressionTree representing a fully qualified name such as "java.lang.Object", creates a String containing the name.- Parameters:
nameExpr
- an ExpressionTree representing a fully qualified name- Returns:
- a String representation of the fully qualified name
-
isWideningBinary
Returns true if the binary operator may do a widening primitive conversion. See JLS chapter 5.- Parameters:
tree
- a binary tree- Returns:
- true if the tree's operator does numeric promotion on its arguments
-
getExplicitAnnotationTrees
public static List<? extends AnnotationTree> getExplicitAnnotationTrees(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) Returns the annotations explicitly written on the given type.- Parameters:
annoTrees
- annotations written before a variable/method declaration; null if this type is not from such a location. This might contain type annotations that the Java parser attached to the declaration rather than to the type.typeTree
- the type whose annotations to return- Returns:
- the annotations explicitly written on the given type
-
getDefaultValueTree
public static LiteralTree getDefaultValueTree(TypeMirror typeMirror, ProcessingEnvironment processingEnv) Return a tree for the default value of the given type. The default value is 0, false, or null.- Parameters:
typeMirror
- a typeprocessingEnv
- the processing environment- Returns:
- a tree for
type
's default value
-
createLiteral
public static LiteralTree createLiteral(com.sun.tools.javac.code.TypeTag typeTag, @Nullable Object value, TypeMirror typeMirror, ProcessingEnvironment processingEnv) Creates a LiteralTree for the given value.- Parameters:
typeTag
- the literal's type tagvalue
- a wrapped primitive, null, or a StringtypeMirror
- the typeMirror for the literalprocessingEnv
- the processing environment- Returns:
- a LiteralTree for the given type tag and value
-
isNullExpression
Returns true if the given tree evaluates tonull
.- Parameters:
t
- a tree- Returns:
- true if the given tree evaluates to
null
-
sameTree
Returns true if two expressions originating from the same scope are identical, i.e. they are syntactically represented in the same way (modulo parentheses) and represent the same value.If the expression includes one or more method calls, assumes the method calls are deterministic.
- Parameters:
expr1
- the first expression to compareexpr2
- the second expression to compare; expr2 must originate from the same scope as expr1- Returns:
- true if the expressions expr1 and expr2 are syntactically identical
-
isDefaultCaseTree
Returns true if this is the default case for a switch statement or expression. (Also, returns true ifcaseTree
iscase null, default:
.)- Parameters:
caseTree
- a case tree- Returns:
- true if
caseTree
is the default case for a switch statement or expression
-
isDefaultCaseLabelTree
Returns true iftree
is aDefaultCaseLabelTree
.- Parameters:
tree
- a tree to check- Returns:
- true if
tree
is aDefaultCaseLabelTree
-
isConstantCaseLabelTree
Returns true iftree
is aConstantCaseLabelTree
.- Parameters:
tree
- a tree to check- Returns:
- true if
tree
is aConstantCaseLabelTree
-
isPatternCaseLabelTree
Returns whethertree
is aPatternCaseLabelTree
.- Parameters:
tree
- a tree to check- Returns:
- true if
tree
is aPatternCaseLabelTree
-
isCaseRule
Deprecated.Returns true if this is a case rule (as opposed to a case statement).- Parameters:
caseTree
- a case tree- Returns:
- true if
caseTree
is a case rule
-
caseTreeGetExpressions
Deprecated.Get the list of expressions from a case expression. For the default case, this is empty. Otherwise, in JDK 11 and earlier, this is a singleton list. In JDK 12 onwards, there can be multiple expressions per case.- Parameters:
caseTree
- the case expression to get the expressions from- Returns:
- the list of expressions in the case
-
caseTreeGetBody
Deprecated.Returns the body of the case statement if it is of the formcase <expression> -> <expression>
. This method should only be called ifCaseTree.getStatements()
returns null.- Parameters:
caseTree
- the case expression to get the body from- Returns:
- the body of the case tree
-
isBindingPatternTree
Returns true iftree
is aBindingPatternTree
.- Parameters:
tree
- a tree to check- Returns:
- true if
tree
is aBindingPatternTree
-
bindingPatternTreeGetVariable
Deprecated.Returns the binding variable ofbindingPatternTree
.- Parameters:
bindingPatternTree
- the BindingPatternTree whose binding variable is returned- Returns:
- the binding variable of
bindingPatternTree
-
isDeconstructionPatternTree
Returns true iftree
is aDeconstructionPatternTree
.- Parameters:
tree
- a tree to check- Returns:
- true if
tree
is aDeconstructionPatternTree
-
instanceOfTreeGetPattern
Deprecated.Returns the pattern ofinstanceOfTree
tree. Returns null if the instanceof does not have a pattern, including if the JDK version does not support instance-of patterns.- Parameters:
instanceOfTree
- theInstanceOfTree
whose pattern is returned- Returns:
- the
PatternTree
ofinstanceOfTree
or null if it doesn't exist
-
switchExpressionTreeGetExpression
@Deprecated public static ExpressionTree switchExpressionTreeGetExpression(Tree switchExpressionTree) Deprecated.Returns the selector expression ofswitchExpressionTree
. For exampleswitch ( expression ) { ... }
- Parameters:
switchExpressionTree
- the switch expression whose selector expression is returned- Returns:
- the selector expression of
switchExpressionTree
-
switchExpressionTreeGetCases
@Deprecated public static List<? extends CaseTree> switchExpressionTreeGetCases(Tree switchExpressionTree) Deprecated.Returns the cases ofswitchExpressionTree
. For exampleswitch ( expression ) { cases }
- Parameters:
switchExpressionTree
- the switch expression whose cases are returned- Returns:
- the cases of
switchExpressionTree
-
isSwitchStatement
Returns true if the given tree is a switch statement (as opposed to a switch expression).- Parameters:
tree
- the switch statement or expression to check- Returns:
- true if the given tree is a switch statement (as opposed to a switch expression)
-
yieldTreeGetValue
Deprecated.Returns the value (expression) foryieldTree
.- Parameters:
yieldTree
- the yield tree- Returns:
- the value (expression) for
yieldTree
-
isVariableTreeDeclaredUsingVar
Returns true if thevariableTree
is declared using thevar
Java keyword.- Parameters:
variableTree
- the variableTree to check- Returns:
- true if the variableTree is declared using the
var
Java keyword
-
isVarArgs
Returns true if the given method/constructor invocation is a varargs invocation.- Parameters:
tree
- a method/constructor invocation- Returns:
- true if the given method/constructor invocation is a varargs invocation
-
isVarArgs
Returns true if the given method invocation is a varargs invocation.- Parameters:
invok
- the method invocation- Returns:
- true if the given method invocation is a varargs invocation
-
isVarArgs
Returns true if the given constructor invocation is a varargs invocation.- Parameters:
newClassTree
- the constructor invocation- Returns:
- true if the given method invocation is a varargs invocation
-
getKindRecordAsClass
Calls getKind() on the given tree, but returns CLASS if the Kind is RECORD. This is needed because the Checker Framework runs on JDKs before the RECORD item was added, so RECORD can't be used in case statements, and usually we want to treat them the same as classes.- Parameters:
tree
- the tree to get the kind for- Returns:
- the kind of the tree, but CLASS if the kind was RECORD
-
isBinaryComparison
Returns true if thetree
is a binary tree that performs a comparison.- Parameters:
tree
- the tree to check- Returns:
- whether the tree represents a binary comparison
-
Select
public static com.sun.tools.javac.tree.JCTree.JCFieldAccess Select(com.sun.tools.javac.tree.TreeMaker treeMaker, Tree base, com.sun.tools.javac.code.Symbol sym) Returns the result oftreeMaker.Select(base, sym)
.- Parameters:
treeMaker
- the TreeMaker to usebase
- the expression for the selectsym
- the symbol to select- Returns:
- the JCFieldAccess tree to select sym in base
-
Select
public static com.sun.tools.javac.tree.JCTree.JCFieldAccess Select(com.sun.tools.javac.tree.TreeMaker treeMaker, Tree base, com.sun.tools.javac.util.Name name) Returns the result oftreeMaker.Select(base, name)
.- Parameters:
treeMaker
- the TreeMaker to usebase
- the expression for the selectname
- the name to select- Returns:
- the JCFieldAccess tree to select sym in base
-
TreeUtilsAfterJava11.BindingPatternUtils.getVariable(Tree)