public final class TreeUtils extends Object
Tree
.TreePathUtil
Modifier and Type | Field and Description |
---|---|
static org.plumelib.util.UniqueIdMap<Tree> |
treeUids
Unique IDs for trees.
|
Modifier and Type | Method and Description |
---|---|
static AnnotationMirror |
annotationFromAnnotationTree(AnnotationTree tree)
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> |
annotationsFromTree(AnnotatedTypeTree tree)
Converts the given AnnotatedTypeTree to a list of AnnotationMirrors.
|
static List<? extends AnnotationMirror> |
annotationsFromTree(TypeParameterTree tree)
Converts the given TypeParameterTree to a list of AnnotationMirrors.
|
static List<AnnotationMirror> |
annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annoTrees)
Converts the given AnnotationTrees to AnnotationMirrors.
|
static VariableTree |
bindingPatternTreeGetVariable(Tree bindingPatternTree)
Returns the binding variable of
bindingPatternTree . |
static boolean |
canHaveTypeAnnotation(Tree tree)
Return
true if and only if tree can have a type annotation. |
static @Nullable Tree |
caseTreeGetBody(CaseTree caseTree)
Returns the body of the case statement if it is of the form
case <expression> ->
<expression> . |
static List<? extends ExpressionTree> |
caseTreeGetExpressions(CaseTree caseTree)
Get the list of expressions from a case expression.
|
static Set<Tree.Kind> |
classTreeKinds()
Return the set of kinds that represent classes.
|
static ExecutableElement |
constructor(NewClassTree tree)
Deprecated.
use elementFromUse instead
|
static boolean |
containsThisConstructorInvocation(MethodTree tree)
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.
|
static Set<Tree.Kind> |
declarationTreeKinds()
Return the set of kinds that represent declarations: classes, methods, and variables.
|
static @Nullable TypeElement |
elementFromDeclaration(ClassTree tree)
Returns the type element corresponding to the given class declaration.
|
static @Nullable Element |
elementFromDeclaration(ExpressionTree tree)
Deprecated.
use elementFromUse or elementFromTree
|
static Element |
elementFromDeclaration(MemberSelectTree tree)
Deprecated.
use elementFromUse
|
static ExecutableElement |
elementFromDeclaration(MethodInvocationTree tree)
Deprecated.
use elementFromUse
|
static @Nullable ExecutableElement |
elementFromDeclaration(MethodTree tree)
Returns the ExecutableElement for the given method declaration.
|
static ExecutableElement |
elementFromDeclaration(NewClassTree tree)
Deprecated.
use elementFromUse
|
static @Nullable VariableElement |
elementFromDeclaration(VariableTree tree)
Returns the VariableElement corresponding to the given variable declaration.
|
static @Nullable TypeElement |
elementFromTree(ClassTree tree)
Deprecated.
use elementFromDeclaration
|
static @Nullable Element |
elementFromTree(ExpressionTree tree)
Returns the element corresponding to the given tree.
|
static Element |
elementFromTree(MemberSelectTree tree)
Deprecated.
use elementFromUse
|
static ExecutableElement |
elementFromTree(MethodInvocationTree tree)
Deprecated.
use elementFromUse
|
static @Nullable ExecutableElement |
elementFromTree(MethodTree tree)
Deprecated.
use elementFromDeclaration
|
static ExecutableElement |
elementFromTree(NewClassTree tree)
Deprecated.
use elementFromUse
|
static @Nullable Element |
elementFromTree(Tree tree)
Returns the
Element for the given Tree API node. |
static @Nullable VariableElement |
elementFromTree(VariableTree tree)
Deprecated.
use elementFromDeclaration
|
static @Nullable TypeElement |
elementFromUse(ClassTree tree)
Deprecated.
use elementFromDeclaration
|
static Element |
elementFromUse(ExpressionTree tree)
Returns the element corresponding to the given use.
|
static Element |
elementFromUse(MemberSelectTree tree)
Returns the element for the given expression.
|
static ExecutableElement |
elementFromUse(MethodInvocationTree tree)
Returns the ExecutableElement for the called method.
|
static @Nullable ExecutableElement |
elementFromUse(MethodTree tree)
Deprecated.
use elementFromDeclaration
|
static ExecutableElement |
elementFromUse(NewClassTree tree)
Returns the ExecutableElement for the given constructor invocation.
|
static @Nullable VariableElement |
elementFromUse(VariableTree tree)
Deprecated.
use elementFromDeclaration
|
static 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 access
tree accesses. |
static Tree.Kind |
getKindRecordAsClass(Tree tree)
Calls getKind() on the given tree, but returns CLASS if the Kind is RECORD.
|
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 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 String |
getMethodName(Tree tree)
Compute the name of the method that the method access
tree 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 by
newClassTree unless newClassTree is creating
an anonymous class. |
static boolean |
hasExplicitConstructor(ClassTree tree)
Determine whether the given class contains an explicit constructor.
|
static boolean |
hasSyntheticArgument(NewClassTree tree)
Returns true if
tree has a synthetic argument. |
static @Nullable Tree |
instanceOfGetPattern(InstanceOfTree instanceOfTree)
Returns the pattern of
instanceOfTree tree. |
static boolean |
isAnonymousConstructor(MethodTree method)
Determines whether or not the given
MethodTree is an anonymous constructor (the
constructor for an anonymous class). |
static boolean |
isArrayLengthAccess(Tree tree)
Returns whether or not tree is an access of array length.
|
static boolean |
isAutoGeneratedRecordMember(Element e)
Returns true if the given
Element is part of a record that has been automatically
generated by the compiler. |
static boolean |
isAutoGeneratedRecordMember(Tree member)
Returns true if the given
Tree is part of a record that has been automatically
generated by the compiler. |
static boolean |
isClassLiteral(Tree tree)
Determine whether
tree is a class literal, such as |
static boolean |
isClassTree(Tree tree)
Is the given tree kind a class, i.e.
|
static boolean |
isCompactCanonicalRecordConstructor(MethodTree method)
Returns true if the given
MethodTree 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 |
isCompileTimeString(ExpressionTree tree)
Returns true if the tree is a constant-time expression.
|
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 |
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 |
isEnumSuperCall(MethodInvocationTree tree)
Returns true if this is a super call to the
Enum constructor. |
static boolean |
isExplicitThisDereference(ExpressionTree tree)
Determine whether the given expression is either "this" or an outer "C.this".
|
static boolean |
isExprConstTrue(ExpressionTree tree)
Determine whether an expression
ExpressionTree 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 whether
tree is a field access expression, such as |
static boolean |
isImplicitlyTypedLambda(Tree tree)
Returns true if
tree 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 whether
tree refers to a method element, such as. |
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 |
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 |
isNullExpression(Tree t)
Returns true if the given tree evaluates to
null . |
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 given
tree represents a field access of the given
VariableElement . |
static boolean |
isStringCompoundConcatenation(CompoundAssignmentTree tree)
Returns true if the compound assignment tree is a string concatenation.
|
static boolean |
isStringConcatenation(Tree tree)
Returns true if the tree represents a
String concatenation operation. |
static boolean |
isSuperConstructorCall(MethodInvocationTree tree)
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(ExecutableElement ee)
Returns true if the given method is synthetic.
|
static boolean |
isSynthetic(MethodTree tree)
Returns true if the given method is synthetic.
|
static boolean |
isThisConstructorCall(MethodInvocationTree tree)
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 |
isVarArgs(Tree tree)
Returns true if the given method/constructor invocation is a varargs invocation.
|
static boolean |
isWideningBinary(BinaryTree tree)
Returns true if the binary operator may do a widening primitive conversion.
|
static Name |
methodName(MethodInvocationTree tree)
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 List<? extends CaseTree> |
switchExpressionTreeGetCases(Tree switchExpressionTree)
Returns the cases of
switchExpressionTree . |
static ExpressionTree |
switchExpressionTreeGetExpression(Tree switchExpressionTree)
Returns the selector expression of
switchExpressionTree . |
static String |
toStringOneLine(Tree tree)
Return toString(), but without line separators.
|
static String |
toStringTruncated(Tree tree,
int length)
Return either
toStringOneLine(com.sun.source.tree.Tree) if it is no more than length characters, or
toStringOneLine(com.sun.source.tree.Tree) quoted and truncated. |
static TypeMirror |
typeOf(Tree tree)
Returns the type as a TypeMirror of
tree . |
static Set<Tree.Kind> |
typeTreeKinds() |
static VariableElement |
variableElementFromTree(Tree tree)
Returns the
VariableElement for the given Tree API node. |
static VariableElement |
variableElementFromUse(ExpressionTree tree)
Returns the VariableElement corresponding to the given use.
|
static @PolyInterned ExpressionTree |
withoutParens(@PolyInterned ExpressionTree tree)
If the given tree is a parenthesized tree, return the enclosed non-parenthesized tree.
|
static @PolyInterned ExpressionTree |
withoutParensOrCasts(@PolyInterned ExpressionTree tree)
If the given tree is a parenthesized tree or cast tree, return the enclosed non-parenthesized,
non-cast tree.
|
static ExpressionTree |
yieldTreeGetValue(Tree yieldTree)
Returns the value (expression) for
yieldTree . |
public static final org.plumelib.util.UniqueIdMap<Tree> treeUids
public static boolean isConstructor(MethodTree tree)
tree
- a tree defining the methodpublic static boolean isSuperConstructorCall(MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isThisConstructorCall(MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isSelfAccess(ExpressionTree tree)
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.
tree
- expression tree representing an access to object membertrue
iff the member is a member of this
instancepublic static @PolyInterned ExpressionTree withoutParens(@PolyInterned ExpressionTree tree)
tree
- an expression treepublic static @PolyInterned ExpressionTree withoutParensOrCasts(@PolyInterned ExpressionTree tree)
tree
- an expression treepublic static @Nullable TypeElement elementFromDeclaration(ClassTree tree)
tree
- class declaration@Deprecated @Pure public static @Nullable TypeElement elementFromTree(ClassTree tree)
The TypeElement may be null for an anonymous class.
tree
- the Tree
node to get the symbol forSymbol
for the given tree, or null if one could not be found@Deprecated @Pure public static @Nullable TypeElement elementFromUse(ClassTree tree)
tree
- the Tree
node to get the symbol forSymbol
for the given tree, or null if one could not be found@Pure @Deprecated public static @Nullable Element elementFromDeclaration(ExpressionTree tree)
tree
- the tree corresponding to a use of an elementnull
otherwise@Pure public static @Nullable Element elementFromTree(ExpressionTree tree)
tree
- the tree corresponding to a use of an elementnull
otherwise@Pure public static Element elementFromUse(ExpressionTree tree)
tree
- the tree, which must be a use of an element@Pure public static VariableElement variableElementFromUse(ExpressionTree tree)
tree
- the tree corresponding to a use of a VariableElementnull
otherwise@Deprecated @Pure public static Element elementFromDeclaration(MemberSelectTree tree)
tree
- the Tree
node to get the symbol for@Deprecated @Pure public static Element elementFromTree(MemberSelectTree tree)
tree
- the Tree
node to get the symbol for@Pure public static Element elementFromUse(MemberSelectTree tree)
tree
- a method call@Deprecated @Pure public static ExecutableElement elementFromDeclaration(MethodInvocationTree tree)
tree
- the Tree
node to get the symbol for@Deprecated @Pure public static ExecutableElement elementFromTree(MethodInvocationTree tree)
tree
- the Tree
node to get the symbol for@Pure public static ExecutableElement elementFromUse(MethodInvocationTree tree)
tree
- a method callpublic static @Nullable ExecutableElement elementFromDeclaration(MethodTree tree)
The result can be null, when tree
is a method in an anonymous class.
tree
- a method declaration@Deprecated @Pure public static @Nullable ExecutableElement elementFromTree(MethodTree tree)
tree
- the Tree
node to get the symbol for@Deprecated @Pure public static @Nullable ExecutableElement elementFromUse(MethodTree tree)
tree
- the Tree
node to get the symbol for@Deprecated @Pure public static ExecutableElement elementFromDeclaration(NewClassTree tree)
tree
- the Tree
node to get the symbol forSymbol
for the given tree, or null if one could not be foundIllegalArgumentException
- if tree
is null or is not a valid javac-internal tree
(JCTree)@Deprecated @Pure public static ExecutableElement elementFromTree(NewClassTree tree)
tree
- the Tree
node to get the symbol forSymbol
for the given tree, or null if one could not be foundIllegalArgumentException
- if tree
is null or is not a valid javac-internal tree
(JCTree)@Pure public static ExecutableElement elementFromUse(NewClassTree tree)
tree
- a constructor invocationconstructor(NewClassTree)
public static @Nullable VariableElement elementFromDeclaration(VariableTree tree)
tree
- the variable@Deprecated @Pure public static @Nullable VariableElement elementFromTree(VariableTree tree)
tree
- the Tree
node to get the symbol for@Deprecated @Pure public static @Nullable VariableElement elementFromUse(VariableTree tree)
tree
- the Tree
node to get the symbol for@Pure public static VariableElement variableElementFromTree(Tree tree)
VariableElement
for the given Tree API node.tree
- the Tree
node to get the symbol forSymbol
for the given treeIllegalArgumentException
- if tree
is null or is not a valid javac-internal tree
(JCTree)@Pure public static @Nullable Element elementFromTree(Tree tree)
Element
for the given Tree API node. For an object instantiation returns
the value of the JCTree.JCNewClass.constructor
field.
Use this only when you do not statically know whether the tree is a declaration or a use of an element.
tree
- the Tree
node to get the symbol forSymbol
for the given tree, or null if one could not be foundIllegalArgumentException
- if tree
is null or is not a valid javac-internal tree
(JCTree)public static ExecutableElement getSuperConstructor(NewClassTree newClassTree)
newClassTree
unless newClassTree
is creating
an anonymous class. In which case, the super constructor is returned.newClassTree
- the constructor invocationconstructor(NewClassTree)
if newClassTree
is not creating an anonymous class@Deprecated public static ExecutableElement constructor(NewClassTree tree)
new
.tree
- the constructor invocationExecutableElement
corresponding to the constructor call in tree
elementFromUse(NewClassTree)
@EnsuresNonNullIf(result=true,expression="elementFromTree(#1)") @EnsuresNonNullIf(result=true,expression="elementFromUse(#1)") @Pure public static boolean isUseOfElement(ExpressionTree tree)
tree
- the ExpressionTree to testpublic static boolean hasSyntheticArgument(NewClassTree tree)
tree
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:
class Outer {
class Inner { }
void method() {
this.new Inner(){};
}
}
Java 9 javac creates the following synthetic tree for this.new Inner(){}
:
new Inner(this) {
(.Outer x0) {
x0.super();
}
}
Java 11 javac creates a different tree without the synthetic argument for this.new
Inner(){}
; the first line in the below code differs:
this.new Inner() {
(.Outer x0) {
x0.super();
}
}
tree
- a new class treetree
has a synthetic argumentpublic static Name methodName(MethodInvocationTree tree)
tree
- the method invocationpublic static boolean containsThisConstructorInvocation(MethodTree tree)
tree
- the method declarationpublic static Tree firstStatement(Tree tree)
tree
- any kind of treepublic static boolean hasExplicitConstructor(ClassTree tree)
tree
- a class treepublic static boolean isSynthetic(ExecutableElement ee)
ee
- a method or constructor elementpublic static boolean isSynthetic(MethodTree tree)
tree
- a method declaration treepublic static boolean isDiamondTree(Tree tree)
TreeInfo.isDiamond(JCTree)
public static boolean isStringConcatenation(Tree tree)
String
concatenation operation.public static boolean isStringCompoundConcatenation(CompoundAssignmentTree tree)
public static boolean isCompileTimeString(ExpressionTree tree)
A tree is a constant-time expression if it is:
tree
- the tree to checkpublic static @Nullable ExpressionTree getReceiverTree(ExpressionTree expression)
expression
- a field access or a method invocationpublic static Set<Tree.Kind> classTreeKinds()
public static Set<Tree.Kind> declarationTreeKinds()
public static boolean isClassTree(Tree tree)
tree
- the tree to testpublic static boolean isDeclarationTree(Tree tree)
tree
- the tree to testpublic static boolean isTypeTree(Tree tree)
TODO: this is an under-approximation: e.g. an identifier could be either a type use or an expression. How can we distinguish.
tree
- the tree to testpublic static boolean isMethodInvocation(Tree tree, ExecutableElement method, ProcessingEnvironment env)
public static boolean isMethodInvocation(Tree methodTree, List<ExecutableElement> methods, ProcessingEnvironment processingEnv)
public static ExecutableElement getMethod(Class<?> type, String methodName, int params, ProcessingEnvironment env)
getMethod(String, String, ProcessingEnvironment, String...)
.type
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environmentpublic static ExecutableElement getMethod(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env)
getMethod(String, String, ProcessingEnvironment, String...)
.typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environmentpublic static @Nullable ExecutableElement getMethodOrNull(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env)
getMethod(String, String, ProcessingEnvironment,
String...)
.typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environmentpublic static List<ExecutableElement> getMethods(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env)
typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environmentpublic static ExecutableElement getMethod(Class<?> type, String methodName, ProcessingEnvironment env, String... paramTypes)
type
- the class that contains the methodmethodName
- the name of the methodenv
- the processing environmentparamTypes
- the method's formal parameter typespublic static ExecutableElement getMethod(@FullyQualifiedName String typeName, String methodName, ProcessingEnvironment env, String... paramTypes)
typeName
- the class that contains the methodmethodName
- the name of the methodenv
- the processing environmentparamTypes
- the method's formal parameter typespublic static boolean isExplicitThisDereference(ExpressionTree tree)
TODO: Should this also handle "super"?
public static boolean isClassLiteral(Tree tree)
tree
is a class literal, such as
Object . class
public static boolean isFieldAccess(Tree tree)
tree
is a field access expression, such as
f obj . fThis method currently also returns true for class literals and qualified this.
tree
- a tree that might be a field accesspublic static String getFieldName(Tree tree)
tree
accesses. Requires tree
to be a field access, as determined by isFieldAccess
(which currently also
returns true for class literals and qualified this).tree
- a field access treetree
public static boolean isMethodAccess(Tree tree)
tree
refers to a method element, such as.
m(...) obj . m(...)
public static String getMethodName(Tree tree)
tree
accesses. Requires tree
to be a method access, as determined by isMethodAccess
.tree
- a method access treetree
public static boolean canHaveTypeAnnotation(Tree tree)
true
if and only if tree
can have a type annotation.true
if and only if tree
can have a type annotationpublic static boolean isSpecificFieldAccess(Tree tree, VariableElement var)
tree
represents a field access of the given
VariableElement
.public static VariableElement getField(@FullyQualifiedName String typeName, String fieldName, ProcessingEnvironment env)
typeName
- the class where the field is declaredfieldName
- the name of the fieldenv
- the processing environmentpublic static boolean isExpressionTree(Tree tree)
tree
- the Tree to testpublic static boolean isEnumSuperCall(MethodInvocationTree tree)
Enum
constructor.tree
- the method invocation to checkEnum
constructorpublic static boolean isTypeDeclaration(Tree tree)
tree
- the Tree to testpublic static boolean isArrayLengthAccess(Tree tree)
tree
- tree to checkpublic static boolean isAnonymousConstructor(MethodTree method)
MethodTree
is an anonymous constructor (the
constructor for an anonymous class).method
- a method tree that may be an anonymous constructorpublic static boolean isCompactCanonicalRecordConstructor(MethodTree method)
MethodTree
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.method
- a method tree that may be a compact canonical constructorpublic static boolean isAutoGeneratedRecordMember(Tree member)
Tree
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.member
- the Tree
for a member of a recordpublic static boolean isAutoGeneratedRecordMember(Element e)
Element
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.e
- the Element
for a member of a recordpublic static List<AnnotationMirror> annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annoTrees)
annoTrees
- list of annotation trees to convert to annotation mirrorspublic static AnnotationMirror annotationFromAnnotationTree(AnnotationTree tree)
tree
- annotation tree to convert to an annotation mirrorpublic static List<? extends AnnotationMirror> annotationsFromTree(AnnotatedTypeTree tree)
tree
- annotated type tree to convertpublic static List<? extends AnnotationMirror> annotationsFromTree(TypeParameterTree tree)
tree
- type parameter tree to convertpublic static List<? extends AnnotationMirror> annotationsFromArrayCreation(NewArrayTree tree, int level)
tree
- new array treepublic static boolean isLocalVariable(Tree tree)
tree
- the tree to checkpublic static TypeMirror typeOf(Tree tree)
tree
. To obtain tree
's AnnotatedTypeMirror,
call AnnotatedTypeFactory.getAnnotatedType()
.tree
public static ExecutableElement findFunction(Tree tree, ProcessingEnvironment env)
tree
- lambda or member reference treeenv
- ProcessingEnvironmentpublic static boolean isImplicitlyTypedLambda(Tree tree)
tree
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)
tree
- any kind of treetree
is an implicitly typed lambdapublic static boolean isExprConstTrue(ExpressionTree tree)
ExpressionTree
has the constant value true, according
to the compiler logic.tree
- the expression to be checkedtree
has the constant value truepublic static String toStringOneLine(Tree tree)
tree
- a treepublic static String toStringTruncated(Tree tree, int length)
toStringOneLine(com.sun.source.tree.Tree)
if it is no more than length
characters, or
toStringOneLine(com.sun.source.tree.Tree)
quoted and truncated.tree
- a treelength
- the maximum length for the result; must be at least 6length
characters longpublic static String nameExpressionToString(ExpressionTree nameExpr)
nameExpr
- an ExpressionTree representing a fully qualified namepublic static boolean isWideningBinary(BinaryTree tree)
tree
- a binary treepublic static List<? extends AnnotationTree> getExplicitAnnotationTrees(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree)
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 returnpublic static LiteralTree getDefaultValueTree(TypeMirror typeMirror, ProcessingEnvironment processingEnv)
typeMirror
- a typeprocessingEnv
- the processing environmenttype
's default valuepublic static LiteralTree createLiteral(com.sun.tools.javac.code.TypeTag typeTag, @Nullable Object value, TypeMirror typeMirror, ProcessingEnvironment processingEnv)
typeTag
- the literal's type tagvalue
- a wrapped primitive, null, or a StringtypeMirror
- the typeMirror for the literalprocessingEnv
- the processing environmentpublic static boolean isNullExpression(Tree t)
null
.t
- a treenull
public static boolean sameTree(ExpressionTree expr1, ExpressionTree expr2)
If the expression includes one or more method calls, assumes the method calls are deterministic.
expr1
- the first expression to compareexpr2
- the second expression to compare; expr2 must originate from the same scope as
expr1public static boolean isDefaultCaseTree(CaseTree caseTree)
caseTree
- a case treecaseTree
is the default case for a switch statement or expressionpublic static List<? extends ExpressionTree> caseTreeGetExpressions(CaseTree caseTree)
caseTree
- the case expression to get the expressions frompublic static @Nullable Tree caseTreeGetBody(CaseTree caseTree)
case <expression> ->
<expression>
. This method should only be called if CaseTree.getStatements()
returns
null.caseTree
- the case expression to get the body frompublic static VariableTree bindingPatternTreeGetVariable(Tree bindingPatternTree)
bindingPatternTree
.bindingPatternTree
- the BindingPatternTree whose binding variable is returnedbindingPatternTree
public static @Nullable Tree instanceOfGetPattern(InstanceOfTree instanceOfTree)
instanceOfTree
tree. Returns null if the instanceof does not
have a pattern, including if the JDK version does not support instance-of patterns.instanceOfTree
- the InstanceOfTree
whose pattern is returnedPatternTree
of instanceOfTree
or null if it doesn't existpublic static ExpressionTree switchExpressionTreeGetExpression(Tree switchExpressionTree)
switchExpressionTree
. For example
switch ( expression ) { ... }
switchExpressionTree
- the switch expression whose selector expression is returnedswitchExpressionTree
public static List<? extends CaseTree> switchExpressionTreeGetCases(Tree switchExpressionTree)
switchExpressionTree
. For example
switch ( expression ) { cases }
switchExpressionTree
- the switch expression whose cases are returnedswitchExpressionTree
public static boolean isSwitchStatement(Tree tree)
tree
- the switch statement or expression to checkpublic static ExpressionTree yieldTreeGetValue(Tree yieldTree)
yieldTree
.yieldTree
- the yield treeyieldTree
public static boolean isVarArgs(Tree tree)
tree
- a method/constructor invocationpublic static boolean isVarArgs(MethodInvocationTree invok)
invok
- the method invocationpublic static boolean isVarArgs(NewClassTree newClassTree)
newClassTree
- the constructor invocationpublic static Tree.Kind getKindRecordAsClass(Tree tree)
tree
- the tree to get the kind for