Class TreeUtils
Tree.- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic enumThis is a duplication ofcom.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind, which is not part of the supported javac API.
- 
Field SummaryFields
- 
Method SummaryModifier and TypeMethodDescriptionstatic AnnotationMirrorConverts 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 VariableElementasFieldAccess(Tree tree) Return the field thattreeis a field access expression for, or null.static VariableTreebindingPatternTreeGetVariable(Tree bindingPatternTree) Returns the binding variable ofbindingPatternTree.static booleancanHaveTypeAnnotation(Tree tree) Returntrueif and only iftreecan have a type annotation.caseTreeGetBody(CaseTree caseTree) Returns the body of the case statement if it is of the formcase <expression> -> <expression>.static List<? extends ExpressionTree>caseTreeGetExpressions(CaseTree caseTree) Get the list of expressions from a case expression.Returns the set of kinds that represent classes and methods.Return the set of kinds that represent classes.static ExecutableElementconstructor(NewClassTree tree) Deprecated.use elementFromUse insteadstatic booleanReturns true if the first statement in the body is a self constructor invocation within a constructor.static LiteralTreecreateLiteral(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 TypeElementReturns the type element corresponding to the given class declaration.Deprecated.Deprecated.use elementFromUsestatic @Nullable ExecutableElementDeprecated.use elementFromUsestatic @Nullable ExecutableElementReturns the ExecutableElement for the given method declaration.static ExecutableElementDeprecated.use elementFromUsestatic @Nullable VariableElementReturns the VariableElement corresponding to the given variable declaration.static @Nullable TypeElementelementFromTree(ClassTree tree) Deprecated.Returns the element corresponding to the given tree.Deprecated.use elementFromUsestatic @Nullable ExecutableElementDeprecated.use elementFromUsestatic @Nullable ExecutableElementelementFromTree(MethodTree tree) Deprecated.use elementFromDeclarationstatic ExecutableElementelementFromTree(NewClassTree tree) Deprecated.use elementFromUseelementFromTree(Tree tree) Returns theElementfor the given Tree API node.static @Nullable VariableElementelementFromTree(VariableTree tree) Deprecated.use elementFromDeclarationstatic @Nullable TypeElementelementFromUse(ClassTree tree) Deprecated.static ElementelementFromUse(ExpressionTree tree) Gets the element for the declaration corresponding to this use of an element.static ElementReturns the element for the given expression.static ExecutableElementReturns the ExecutableElement for the called method.static @Nullable ExecutableElementelementFromUse(MethodTree tree) Deprecated.use elementFromDeclarationstatic ExecutableElementelementFromUse(NewClassTree tree) Gets the ExecutableElement for the called constructor, from a constructor invocation.static @Nullable VariableElementelementFromUse(VariableTree tree) Deprecated.use elementFromDeclarationstatic ExecutableElementfindFunction(Tree tree, ProcessingEnvironment env) The type of the lambda or method reference tree is a functional interface type.static TreefirstStatement(Tree tree) Returns the first statement of the tree if it is a block.static LiteralTreegetDefaultValueTree(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 VariableElementgetField(@FullyQualifiedName String typeName, String fieldName, ProcessingEnvironment env) Returns the VariableElement for a field declaration.static StringgetFieldName(Tree tree) Compute the name of the field that the field accesstreeaccesses.static Tree.KindgetKindRecordAsClass(Tree tree) Calls getKind() on the given tree, but returns CLASS if the Kind is RECORD.static ExecutableElementgetMethod(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration.static ExecutableElementgetMethod(@FullyQualifiedName String typeName, String methodName, ProcessingEnvironment env, String... paramTypes) Returns the ExecutableElement for a method declaration.static ExecutableElementgetMethod(Class<?> type, String methodName, int params, ProcessingEnvironment env) Returns the ExecutableElement for a method declaration.static ExecutableElementgetMethod(Class<?> type, String methodName, ProcessingEnvironment env, String... paramTypes) Returns the ExecutableElement for a method declaration.static StringgetMethodName(Tree tree) Compute the name of the method that the method accesstreeaccesses.static @Nullable ExecutableElementgetMethodOrNull(@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 ExpressionTreegetReceiverTree(ExpressionTree expression) Returns the receiver tree of a field access or a method invocation.static ExecutableElementgetSuperConstructor(NewClassTree newClassTree) Returns the constructor invoked bynewClassTreeunlessnewClassTreeis creating an anonymous class.static booleanDetermine whether the given class contains an explicit constructor.static booleanReturns true iftreehas a synthetic argument.instanceOfTreeGetPattern(InstanceOfTree instanceOfTree) Returns the pattern ofinstanceOfTreetree.static booleanisAnonymousConstructor(MethodTree method) Determines whether or not the givenMethodTreeis an anonymous constructor (the constructor for an anonymous class).static booleanisArrayLengthAccess(Tree tree) Returns whether or not tree is an access of array length.static booleanisAutoGeneratedRecordMember(Tree member) Returns true if the givenTreeis part of a record that has been automatically generated by the compiler.static booleanisBinaryComparison(BinaryTree tree) Returns true if thetreeis a binary tree that performs a comparison.static booleanisCaseRule(CaseTree caseTree) Returns true if this is a case rule (as opposed to a case statement).static booleanisClassLiteral(Tree tree) Determine whethertreeis a class literal, such asstatic booleanisClassTree(Tree tree) Is the given tree kind a class, i.e.static booleanReturns true if the givenMethodTreeis a compact canonical constructor (the constructor for a record where the parameters are implicitly declared and implicitly assigned to the record's fields).static booleanReturns true if the tree is a constant-time expression.static booleanisConstructor(MethodTree tree) Checks if the provided method is a constructor method or no.static booleanisDeclarationTree(Tree tree) Returns true if the given tree is a declaration.static booleanisDefaultCaseTree(CaseTree caseTree) Returns true if this is the default case for a switch statement or expression.static booleanisDiamondTree(Tree tree) Returns true if the tree is of a diamond type.static booleanReturns true if this is a super call to theEnumconstructor.static booleanDetermine whether the given expression is either "this" or an outer "C.this".static booleanDetermine whether an expressionExpressionTreehas the constant value true, according to the compiler logic.static booleanisExpressionTree(Tree tree) Determine whether the given tree represents an ExpressionTree.static booleanisFieldAccess(Tree tree) Determine whethertreeis a field access expression, such asstatic booleanisImplicitlyTypedLambda(Tree tree) Returns true iftreeis an implicitly typed lambda.static booleanisLocalVariable(Tree tree) Returns true if the tree is the declaration or use of a local variable.static booleanisMethodAccess(Tree tree) Determine whethertreerefers to a method element, such as.static booleanisMethodInvocation(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 booleanisMethodInvocation(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 booleanReturns true if the given tree evaluates tonull.static booleanisSelfAccess(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 booleanisSpecificFieldAccess(Tree tree, VariableElement var) Returns true if and only if the giventreerepresents a field access of the givenVariableElement.static booleanReturns true if the compound assignment tree is a string concatenation.static booleanisStringConcatenation(Tree tree) Returns true if the tree represents aStringconcatenation operation.static booleanChecks if the method invocation is a call to super.static booleanisSwitchStatement(Tree tree) Returns true if the given tree is a switch statement (as opposed to a switch expression).static booleanisSynthetic(MethodTree tree) Returns true if the given method is synthetic.static booleanReturns true if the given method is synthetic.static booleanChecks if the method invocation is a call to "this".static booleanisTypeDeclaration(Tree tree) Determine whether the given tree represents a declaration of a type (including type parameters).static booleanisTypeTree(Tree tree) Is the given tree a type instantiation?static booleanisUseOfElement(ExpressionTree tree) Determine whether the given ExpressionTree has an underlying element.static booleanisVarArgs(MethodInvocationTree invok) Returns true if the given method invocation is a varargs invocation.static booleanisVarArgs(NewClassTree newClassTree) Returns true if the given constructor invocation is a varargs invocation.static booleanReturns true if the given method/constructor invocation is a varargs invocation.static booleanisVariableTreeDeclaredUsingVar(VariableTree variableTree) Returns true if thevariableTreeis declared using thevarJava keyword.static booleanisVoidReturn(MethodTree tree) Is this method's declared return type "void"?static booleanisWideningBinary(BinaryTree tree) Returns true if the binary operator may do a widening primitive conversion.static NameReturns the name of the invoked method.static StringnameExpressionToString(ExpressionTree nameExpr) Given a javac ExpressionTree representing a fully qualified name such as "java.lang.Object", creates a String containing the name.static booleansameTree(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.JCFieldAccessSelect(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.JCFieldAccessReturns the result oftreeMaker.Select(base, name).switchExpressionTreeGetCases(Tree switchExpressionTree) Returns the cases ofswitchExpressionTree.static ExpressionTreeswitchExpressionTreeGetExpression(Tree switchExpressionTree) Returns the selector expression ofswitchExpressionTree.static StringtoStringOneLine(Tree tree) Return toString(), but without line separators.static StringtoStringTruncated(Tree tree, int length) Return eithertoStringOneLine(com.sun.source.tree.Tree)if it is no more thanlengthcharacters, ortoStringOneLine(com.sun.source.tree.Tree)quoted and truncated.static ExecutableTypeDetermines 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 ExecutableTypetypeFromUse(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 TypeMirrorReturns the type as a TypeMirror oftree.Return the set of kinds that represent types.static VariableElementvariableElementFromTree(Tree tree) Returns theVariableElementfor the given Tree API node.static VariableElementReturns the VariableElement corresponding to the given use.static @PolyInterned ExpressionTreeIf the given tree is a parenthesized tree, return the enclosed non-parenthesized tree.static @PolyInterned ExpressionTreeIf the given tree is a parenthesized tree or cast tree, return the enclosed non-parenthesized, non-cast tree.static ExpressionTreeyieldTreeGetValue(Tree yieldTree) Returns the value (expression) foryieldTree.
- 
Field Details- 
treeUidsUnique IDs for trees. Used instead of hash codes, so output is deterministic.
 
- 
- 
Method Details- 
isConstructorChecks if the provided method is a constructor method or no.- Parameters:
- tree- a tree defining the method
- Returns:
- true iff tree describes a constructor
 
- 
isSuperConstructorCallChecks 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
 
- 
isThisConstructorCallChecks 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
 
- 
isSelfAccessReturns 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:
- trueiff the member is a member of- thisinstance
 
- 
withoutParensIf 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
 
- 
withoutParensOrCastsIf 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
 
- 
elementFromDeclarationReturns the type element corresponding to the given class declaration.- Parameters:
- tree- class declaration
- Returns:
- the element for the given class
 
- 
elementFromTreeDeprecated.Returns the type element corresponding to the given class declaration.The TypeElement may be null for an anonymous class. - Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Symbolfor the given tree, or null if one could not be found
 
- 
elementFromUseDeprecated.Returns the type element corresponding to the given class declaration.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Symbolfor the given tree, or null if one could not be found
 
- 
elementFromDeclarationDeprecated.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, nullotherwise
 
- 
elementFromTreeReturns 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, nullotherwise
 
- 
elementFromUseGets 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
 
- 
variableElementFromUseReturns 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
 
- 
elementFromDeclarationDeprecated.use elementFromUseReturns the element for the given expression.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the element for the given tree, or null if one could not be found
 
- 
elementFromTreeDeprecated.use elementFromUseReturns the element for the given expression.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the element for the given tree, or null if one could not be found
 
- 
elementFromUseReturns 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- the- Treenode 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- the- Treenode to get the symbol for
- Returns:
- the Element for the given tree, or null if one could not be found
 
- 
elementFromUseReturns the ExecutableElement for the called method.- Parameters:
- tree- a method call
- Returns:
- the ExecutableElement for the called method
 
- 
elementFromDeclarationReturns the ExecutableElement for the given method declaration.The result can be null, when treeis 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)
 
- 
elementFromTreeDeprecated.use elementFromDeclarationReturns the ExecutableElement for the given method declaration.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Element for the given tree, or null if one could not be found
 
- 
elementFromUseDeprecated.use elementFromDeclarationReturns the ExecutableElement for the given method declaration.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Element for the given tree, or null if one could not be found
 
- 
elementFromDeclarationDeprecated.use elementFromUseReturns the ExecutableElement for the given constructor invocation.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Symbolfor the given tree, or null if one could not be found
- Throws:
- IllegalArgumentException- if- treeis null or is not a valid javac-internal tree (JCTree)
 
- 
elementFromTreeDeprecated.use elementFromUseGets the ExecutableElement for the called constructor, from a constructor invocation.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Symbolfor the given tree, or null if one could not be found
- Throws:
- IllegalArgumentException- if- treeis null or is not a valid javac-internal tree (JCTree)
 
- 
elementFromUseGets the ExecutableElement for the called constructor, from a constructor invocation.- Parameters:
- tree- a constructor invocation
- Returns:
- the ExecutableElement for the called constructor
- See Also:
 
- 
elementFromDeclarationReturns the VariableElement corresponding to the given variable declaration.- Parameters:
- tree- the variable
- Returns:
- the element for the given variable
 
- 
elementFromTreeDeprecated.use elementFromDeclarationReturns the VariableElement corresponding to the given variable declaration.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Element for the given tree, or null if one could not be found
 
- 
elementFromUseDeprecated.use elementFromDeclarationReturns the VariableElement corresponding to the given variable declaration.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Element for the given tree, or null if one could not be found
 
- 
variableElementFromTreeReturns theVariableElementfor the given Tree API node.- Parameters:
- tree- the- Treenode to get the symbol for
- Returns:
- the Symbolfor the given tree
- Throws:
- IllegalArgumentException- if- treeis null or is not a valid javac-internal tree (JCTree)
 
- 
elementFromTreeReturns theElementfor the given Tree API node. For an object instantiation returns the value of theJCTree.JCNewClass.constructorfield.Use this only when you do not statically know whether the tree is a declaration or a use of an element. 
- 
getSuperConstructorReturns the constructor invoked bynewClassTreeunlessnewClassTreeis 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)ifnewClassTreeis not creating an anonymous class
 
- 
typeFromUseDetermines the type for a method invocation at its call site, which has all type variables substituted with the type arguments at the call site.TypeVariablein the returned type should be compared usingTypesUtils.areSame(TypeVariable, TypeVariable)because theTypeVariablewill 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 ExecutableTypecorresponding to the method invocation at its call site
 
- 
typeFromUseDetermines 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 ExecutableTypecorresponding to the constructor call (i.e., the giventree) at its call site
 
- 
constructorDeprecated.use elementFromUse insteadDetermines the symbol for a constructor given an invocation vianew.- Parameters:
- tree- the constructor invocation
- Returns:
- the ExecutableElementcorresponding 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
 
- 
hasSyntheticArgumentReturns true iftreehas 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 treehas a synthetic argument
 
- 
methodNameReturns the name of the invoked method.- Parameters:
- tree- the method invocation
- Returns:
- the name of the invoked method
 
- 
containsThisConstructorInvocationReturns 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
 
- 
firstStatementReturns 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.
 
- 
hasExplicitConstructorDetermine whether the given class contains an explicit constructor.- Parameters:
- tree- a class tree
- Returns:
- true iff there is an explicit constructor
 
- 
isSyntheticReturns 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
 
- 
isSyntheticReturns true if the given method is synthetic.- Parameters:
- tree- a method declaration tree
- Returns:
- true iff the given method is synthetic
 
- 
isDiamondTreeReturns 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)
 
 
- 
isStringConcatenationReturns true if the tree represents aStringconcatenation operation.
- 
isStringCompoundConcatenationReturns true if the compound assignment tree is a string concatenation.
- 
isVoidReturnIs this method's declared return type "void"?- Parameters:
- tree- a method declaration
- Returns:
- true iff method's declared return type is "void"
 
- 
isCompileTimeStringReturns 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
 
- 
getReceiverTreeReturns 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
 
- 
classAndMethodTreeKindsReturns the set of kinds that represent classes and methods.- Returns:
- the set of kinds that represent classes and methods
 
- 
classTreeKindsReturn the set of kinds that represent classes.- Returns:
- the set of kinds that represent classes
 
- 
isClassTreeIs 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
 
- 
declarationTreeKindsReturn the set of kinds that represent declarations: classes, methods, and variables.- Returns:
- the set of kinds that represent declarations
 
- 
isDeclarationTreeReturns true if the given tree is a declaration.- Parameters:
- tree- the tree to test
- Returns:
- true if the given tree is a declaration
 
- 
typeTreeKindsReturn the set of kinds that represent types.- Returns:
- the set of kinds that represent types
 
- 
isTypeTreeIs 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
 
- 
isMethodInvocationpublic 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.
- 
isMethodInvocationpublic 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.
- 
getMethodpublic 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 method
- methodName- the name of the method
- params- the number of formal parameters
- env- the processing environment
- Returns:
- the ExecutableElement for the specified method
 
- 
getMethodpublic 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 method
- methodName- the name of the method
- params- the number of formal parameters
- env- the processing environment
- Returns:
- the ExecutableElement for the specified method
 
- 
getMethodOrNullpublic 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 method
- methodName- the name of the method
- params- the number of formal parameters
- env- the processing environment
- Returns:
- the ExecutableElement for the specified method, or null
 
- 
getMethodspublic 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 method
- methodName- the name of the method
- params- the number of formal parameters
- env- the processing environment
- Returns:
- the ExecutableElements for all matching methods
 
- 
getMethodpublic 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 method
- methodName- the name of the method
- env- the processing environment
- paramTypes- the method's formal parameter types
- Returns:
- the ExecutableElement for the specified method
 
- 
getMethodpublic 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 method
- methodName- the name of the method
- env- the processing environment
- paramTypes- the method's formal parameter types
- Returns:
- the ExecutableElement for the specified method
 
- 
isExplicitThisDereferenceDetermine whether the given expression is either "this" or an outer "C.this".TODO: Should this also handle "super"? 
- 
isClassLiteralDetermine whethertreeis a class literal, such asObject . class - Returns:
- true iff if tree is a class literal
 
- 
isFieldAccessDetermine whethertreeis 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)
 
- 
asFieldAccessReturn the field thattreeis 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
 
- 
getFieldNameCompute the name of the field that the field accesstreeaccesses. Requirestreeto 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
 
- 
isMethodAccessDetermine whethertreerefers to a method element, such as.m(...) obj . m(...) - Returns:
- true iff if tree is a method access expression (implicit or explicit)
 
- 
getMethodNameCompute the name of the method that the method accesstreeaccesses. Requirestreeto be a method access, as determined byisMethodAccess.- Parameters:
- tree- a method access tree
- Returns:
- the name of the method accessed by tree
 
- 
canHaveTypeAnnotationReturntrueif and only iftreecan have a type annotation.- Returns:
- trueif and only if- treecan have a type annotation
 
- 
isSpecificFieldAccessReturns true if and only if the giventreerepresents a field access of the givenVariableElement.
- 
getFieldpublic 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 declared
- fieldName- the name of the field
- env- the processing environment
- Returns:
- the VariableElement for typeName.fieldName
 
- 
isExpressionTreeDetermine whether the given tree represents an ExpressionTree.- Parameters:
- tree- the Tree to test
- Returns:
- whether the tree is an ExpressionTree
 
- 
isEnumSuperCallReturns true if this is a super call to theEnumconstructor.- Parameters:
- tree- the method invocation to check
- Returns:
- true if this is a super call to the Enumconstructor
 
- 
isTypeDeclarationDetermine 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
 
- 
isArrayLengthAccessReturns whether or not tree is an access of array length.- Parameters:
- tree- tree to check
- Returns:
- true if tree is an access of array length
 
- 
isAnonymousConstructorDetermines whether or not the givenMethodTreeis 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
 
- 
isCompactCanonicalRecordConstructorReturns true if the givenMethodTreeis 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
 
- 
isAutoGeneratedRecordMemberReturns true if the givenTreeis 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- the- Treefor a member of a record
- Returns:
- true if the given path is generated by the compiler
 
- 
annotationsFromTypeAnnotationTreespublic 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
 
- 
annotationFromAnnotationTreeConverts 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
 
- 
annotationsFromTreeConverts the given AnnotatedTypeTree to a list of AnnotationMirrors.- Parameters:
- tree- annotated type tree to convert
- Returns:
- list of AnnotationMirrors from the tree
 
- 
annotationsFromTreeConverts the given TypeParameterTree to a list of AnnotationMirrors.- Parameters:
- tree- type parameter tree to convert
- Returns:
- list of AnnotationMirrors from the tree
 
- 
annotationsFromArrayCreationpublic 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
 
- 
isLocalVariableReturns 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
 
- 
typeOfReturns the type as a TypeMirror oftree. To obtaintree's AnnotatedTypeMirror, callAnnotatedTypeFactory.getAnnotatedType().- Returns:
- the type as a TypeMirror of tree
 
- 
findFunctionThe 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 tree
- env- the processing environment
- Returns:
- the single abstract method declared by the type of the tree
 
- 
isImplicitlyTypedLambdaReturns true iftreeis 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 treeis an implicitly typed lambda
 
- 
isExprConstTrueDetermine whether an expressionExpressionTreehas the constant value true, according to the compiler logic.- Parameters:
- tree- the expression to be checked
- Returns:
- true if treehas the constant value true
 
- 
toStringOneLineReturn toString(), but without line separators.- Parameters:
- tree- a tree
- Returns:
- a one-line string representation of the tree
 
- 
toStringTruncatedReturn eithertoStringOneLine(com.sun.source.tree.Tree)if it is no more thanlengthcharacters, ortoStringOneLine(com.sun.source.tree.Tree)quoted and truncated.- Parameters:
- tree- a tree
- length- 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 lengthcharacters long
 
- 
nameExpressionToStringGiven 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
 
- 
isWideningBinaryReturns 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
 
- 
getExplicitAnnotationTreespublic 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
 
- 
getDefaultValueTreepublic 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 type
- processingEnv- the processing environment
- Returns:
- a tree for type's default value
 
- 
createLiteralpublic 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 tag
- value- a wrapped primitive, null, or a String
- typeMirror- the typeMirror for the literal
- processingEnv- the processing environment
- Returns:
- a LiteralTree for the given type tag and value
 
- 
isNullExpressionReturns true if the given tree evaluates tonull.- Parameters:
- t- a tree
- Returns:
- true if the given tree evaluates to null
 
- 
sameTreeReturns 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 compare
- expr2- 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
 
- 
isDefaultCaseTreeReturns true if this is the default case for a switch statement or expression.- Parameters:
- caseTree- a case tree
- Returns:
- true if caseTreeis the default case for a switch statement or expression
 
- 
isCaseRuleReturns true if this is a case rule (as opposed to a case statement).- Parameters:
- caseTree- a case tree
- Returns:
- true if caseTreeis a case rule
 
- 
caseTreeGetExpressionsGet 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
 
- 
caseTreeGetBodyReturns 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
 
- 
bindingPatternTreeGetVariableReturns the binding variable ofbindingPatternTree.- Parameters:
- bindingPatternTree- the BindingPatternTree whose binding variable is returned
- Returns:
- the binding variable of bindingPatternTree
 
- 
instanceOfTreeGetPatternReturns the pattern ofinstanceOfTreetree. Returns null if the instanceof does not have a pattern, including if the JDK version does not support instance-of patterns.- Parameters:
- instanceOfTree- the- InstanceOfTreewhose pattern is returned
- Returns:
- the PatternTreeofinstanceOfTreeor null if it doesn't exist
 
- 
switchExpressionTreeGetExpressionReturns the selector expression ofswitchExpressionTree. For exampleswitch ( expression ) { ... }- Parameters:
- switchExpressionTree- the switch expression whose selector expression is returned
- Returns:
- the selector expression of switchExpressionTree
 
- 
switchExpressionTreeGetCasesReturns the cases ofswitchExpressionTree. For exampleswitch ( expression ) { cases }- Parameters:
- switchExpressionTree- the switch expression whose cases are returned
- Returns:
- the cases of switchExpressionTree
 
- 
isSwitchStatementReturns 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)
 
- 
yieldTreeGetValueReturns the value (expression) foryieldTree.- Parameters:
- yieldTree- the yield tree
- Returns:
- the value (expression) for yieldTree
 
- 
isVariableTreeDeclaredUsingVarReturns true if thevariableTreeis declared using thevarJava keyword.- Parameters:
- variableTree- the variableTree to check
- Returns:
- true if the variableTree is declared using the varJava keyword
 
- 
isVarArgsReturns 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
 
- 
isVarArgsReturns 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
 
- 
isVarArgsReturns 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
 
- 
getKindRecordAsClassCalls 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
 
- 
isBinaryComparisonReturns true if thetreeis a binary tree that performs a comparison.- Parameters:
- tree- the tree to check
- Returns:
- whether the tree represents a binary comparison
 
- 
Selectpublic 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 use
- base- the expression for the select
- sym- the symbol to select
- Returns:
- the JCFieldAccess tree to select sym in base
 
- 
Selectpublic 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 use
- base- the expression for the select
- name- the name to select
- Returns:
- the JCFieldAccess tree to select sym in base
 
 
-