public final class TreeUtils extends Object
Tree
.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> annoTreess)
Converts the given AnnotationTrees to AnnotationMirrors.
|
static boolean |
canHaveTypeAnnotation(Tree tree) |
static Set<Tree.Kind> |
classTreeKinds()
Return the set of kinds that represent classes.
|
static ExecutableElement |
constructor(NewClassTree tree)
Determines the symbol for a constructor given an invocation via
new . |
static boolean |
containsThisConstructorInvocation(MethodTree node) |
static TypeElement |
elementFromDeclaration(ClassTree node)
Gets the element for a class corresponding to a declaration.
|
static ExecutableElement |
elementFromDeclaration(MethodTree node)
Gets the element for a method corresponding to a declaration.
|
static VariableElement |
elementFromDeclaration(VariableTree node)
Gets the element for a variable corresponding to its declaration.
|
static @Nullable Element |
elementFromTree(Tree tree)
Gets the
Element for the given Tree API node. |
static @Nullable Element |
elementFromUse(ExpressionTree node)
Gets the element for the declaration corresponding to this use of an element.
|
static @Nullable ExecutableElement |
elementFromUse(MethodInvocationTree node)
Specialization for return type.
|
static @Nullable ExecutableElement |
elementFromUse(NewClassTree node)
Specialization for return type.
|
static @Nullable ClassTree |
enclosingClass(TreePath path)
Gets the enclosing class of the tree node defined by the given
TreePath . |
static @Nullable MethodTree |
enclosingMethod(TreePath path)
Gets the enclosing method of the tree node defined by the given
TreePath . |
static @Nullable Tree |
enclosingMethodOrLambda(TreePath path)
Gets the enclosing method or lambda expression of the tree node defined by the given
TreePath . |
static Pair<Tree,Tree> |
enclosingNonParen(TreePath path)
Gets the first enclosing tree in path, that is not a parenthesis.
|
static <T extends Tree> |
enclosingOfClass(TreePath path,
Class<T> treeClass)
Gets the first enclosing tree in path, of the specified class.
|
static @Nullable Tree |
enclosingOfKind(TreePath path,
Set<Tree.Kind> kinds)
Gets the first enclosing tree in path, with any one of the specified kinds.
|
static @Nullable Tree |
enclosingOfKind(TreePath path,
Tree.Kind kind)
Gets the first enclosing tree in path, of the specified kind.
|
static @Nullable BlockTree |
enclosingTopLevelBlock(TreePath path) |
static @Nullable VariableTree |
enclosingVariable(TreePath path)
Gets the enclosing variable of a tree node defined by the given
TreePath . |
static com.sun.tools.javac.code.Symbol |
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 @Nullable Tree |
getAssignmentContext(TreePath treePath)
Returns the tree with the assignment context for the treePath leaf node.
|
static VariableElement |
getField(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 ExecutableElement |
getMethod(String typeName,
String methodName,
int params,
ProcessingEnvironment env)
Returns the ExecutableElement for the method declaration of methodName, in class typeName,
with params formal parameters.
|
static ExecutableElement |
getMethod(String typeName,
String methodName,
ProcessingEnvironment env,
String... paramTypes)
Returns the ExecutableElement for a method declaration of methodName, in class typeName, with
formal parameters of the given types.
|
static String |
getMethodName(Tree tree)
Compute the name of the method that the method access
tree accesses. |
static List<ExecutableElement> |
getMethods(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 boolean |
hasExplicitConstructor(ClassTree node)
Determine whether the given class contains an explicit constructor.
|
static boolean |
isAnonymousConstructor(MethodTree method)
Determines whether or not the node referred to by the given
TreePath 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 |
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 |
isCompileTimeString(ExpressionTree node)
Returns true if the node is a constant-time expression.
|
static boolean |
isConstructor(MethodTree tree)
Checks if the provided method is a constructor method or no.
|
static boolean |
isDiamondTree(Tree tree)
Returns true if the tree is of a diamond type.
|
static boolean |
isEnumSuper(MethodInvocationTree node) |
static boolean |
isExplicitThisDereference(ExpressionTree tree)
Determine whether the given expression is either "this" or an outer "C.this".
|
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 expressions, such as. |
static boolean |
isImplicitlyTypedLambda(Tree tree)
Returns true if
tree is an implicitly typed lambda. |
static boolean |
isLocalVariable(Tree tree) |
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 |
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 |
isThisConstructorCall(MethodInvocationTree tree)
Checks if the method invocation is a call to "this".
|
static boolean |
isTreeInStaticScope(TreePath path)
Returns whether or not the leaf of the tree path is in a static scope.
|
static boolean |
isTypeDeclaration(Tree node)
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 node)
Determine whether the given ExpressionTree has an underlying element.
|
static Name |
methodName(MethodInvocationTree node) |
static @Nullable TreePath |
pathTillClass(TreePath path)
Gets path to the first enclosing class tree, where class is defined by the classTreeKinds
method.
|
static @Nullable TreePath |
pathTillOfKind(TreePath path,
Set<Tree.Kind> kinds)
Gets path to the first enclosing tree with any one of the specified kinds.
|
static @Nullable TreePath |
pathTillOfKind(TreePath path,
Tree.Kind kind)
Gets path to the first enclosing tree of the specified kind.
|
static TypeMirror |
typeOf(Tree tree) |
static Set<Tree.Kind> |
typeTreeKinds() |
static ExpressionTree |
withoutParens(ExpressionTree tree)
If the given tree is a parenthesized tree, return the enclosed non-parenthesized tree.
|
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 @Nullable Tree enclosingOfKind(TreePath path, Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treenull
otherwisepublic static @Nullable Tree enclosingOfKind(TreePath path, Set<Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treenull
otherwisepublic static @Nullable TreePath pathTillClass(TreePath path)
path
- the path defining the tree nodenull
otherwisepublic static @Nullable TreePath pathTillOfKind(TreePath path, Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treenull
otherwisepublic static @Nullable TreePath pathTillOfKind(TreePath path, Set<Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treenull
otherwisepublic static <T extends Tree> T enclosingOfClass(TreePath path, Class<T> treeClass)
path
- the path defining the tree nodetreeClass
- the class of the desired treenull
otherwisepublic static @Nullable ClassTree enclosingClass(TreePath path)
TreePath
. It returns a
Tree
, from which checkers.types.AnnotatedTypeMirror
or Element
can be
obtained.path
- the path defining the tree nodenull
if one does
not existpublic static @Nullable VariableTree enclosingVariable(TreePath path)
TreePath
.path
- the path defining the tree nodenull
if one does not existpublic static @Nullable MethodTree enclosingMethod(TreePath path)
TreePath
. It returns
a Tree
, from which an checkers.types.AnnotatedTypeMirror
or Element
can be obtained.path
- the path defining the tree nodenull
if one does not existpublic static @Nullable Tree enclosingMethodOrLambda(TreePath path)
TreePath
. It returns a Tree
, from which an checkers.types.AnnotatedTypeMirror
or Element
can be obtained.path
- the path defining the tree nodenull
if one does not
existpublic static ExpressionTree withoutParens(ExpressionTree tree)
tree
- an expression treepublic static Pair<Tree,Tree> enclosingNonParen(TreePath path)
path
- the path defining the tree nodepublic static @Nullable Tree getAssignmentContext(TreePath treePath)
The assignment context for the treePath
is the leaf of its parent, if the parent
is one of the following trees:
If the leaf is a ParenthesizedTree, then recurse on the parent.
Otherwise, null is returned.
null
otherwise@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. Note that this result might differ from
the result of constructor(NewClassTree)
.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 TypeElement elementFromDeclaration(ClassTree node)
public static ExecutableElement elementFromDeclaration(MethodTree node)
public static VariableElement elementFromDeclaration(VariableTree node)
@Pure public static @Nullable Element elementFromUse(ExpressionTree node)
elementFromDeclaration(ClassTree)
, elementFromDeclaration(MethodTree)
, or elementFromDeclaration(VariableTree)
instead.
This method is just a wrapper around elementFromTree(Tree)
, but this
class might be the first place someone looks for this functionality.
node
- the tree corresponding to a use of an elementnull
otherwisepublic static @Nullable ExecutableElement elementFromUse(MethodInvocationTree node)
public static @Nullable ExecutableElement elementFromUse(NewClassTree node)
constructor(NewClassTree)
public static ExecutableElement constructor(NewClassTree tree)
new
.
If the tree is a declaration of an anonymous class, then method returns constructor that gets invoked in the extended class, rather than the anonymous constructor implicitly added by the constructor (JLS 15.9.5.1)
tree
- the constructor invocationExecutableElement
corresponding to the constructor call in tree
elementFromUse(NewClassTree)
@EnsuresNonNullIf(result=true, expression="elementFromUse(#1)") @Pure public static boolean isUseOfElement(ExpressionTree node)
node
- the ExpressionTree to testpublic static Name methodName(MethodInvocationTree node)
public static boolean containsThisConstructorInvocation(MethodTree node)
public static Tree firstStatement(Tree tree)
tree
- any kind of treepublic static boolean hasExplicitConstructor(ClassTree node)
node
- a class 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 node)
A tree is a constant-time expression if it is:
public static @Nullable ExpressionTree getReceiverTree(ExpressionTree expression)
public static Set<Tree.Kind> classTreeKinds()
public static boolean isClassTree(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(String typeName, String methodName, int params, ProcessingEnvironment env)
getMethod(String,
String, ProcessingEnvironment, String...)
.public static List<ExecutableElement> getMethods(String typeName, String methodName, int params, ProcessingEnvironment env)
public static ExecutableElement getMethod(String typeName, String methodName, ProcessingEnvironment env, String... paramTypes)
public 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 expressions, such as.
f obj . f
public static String getFieldName(Tree tree)
tree
accesses. Requires tree
to be a field access, as determined by isFieldAccess
.tree
.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
.public static boolean canHaveTypeAnnotation(Tree tree)
true
if and only if tree
can have a type annotation.
TODO: is this implementation precise enough? E.g. does a .class literal work correctly?
public static boolean isSpecificFieldAccess(Tree tree, VariableElement var)
tree
represents a field access of the given
VariableElement
.public static VariableElement getField(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 isEnumSuper(MethodInvocationTree node)
node
- the method invocation to checkEnum
constructorpublic static boolean isTypeDeclaration(Tree node)
node
- the Tree to testpublic static boolean isTreeInStaticScope(TreePath path)
path
- TreePath whose leaf may or may not be in static scopepublic static boolean isArrayLengthAccess(Tree tree)
tree
- tree to checkpublic static boolean isAnonymousConstructor(MethodTree method)
TreePath
is an anonymous
constructor (the constructor for an anonymous class.method
- the TreePath
for a node that may be an anonymous constructorpublic static List<AnnotationMirror> annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annoTreess)
annoTreess
- 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)
public static TypeMirror typeOf(Tree tree)
tree
public static com.sun.tools.javac.code.Symbol 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 lambda.