public final class TreeUtils
extends java.lang.Object
Tree
.Modifier and Type | Method and Description |
---|---|
static javax.lang.model.element.AnnotationMirror |
annotationFromAnnotationTree(com.sun.source.tree.AnnotationTree tree) |
static java.util.List<? extends javax.lang.model.element.AnnotationMirror> |
annotationsFromArrayCreation(com.sun.source.tree.NewArrayTree node,
int level) |
static java.util.List<? extends javax.lang.model.element.AnnotationMirror> |
annotationsFromTree(com.sun.source.tree.AnnotatedTypeTree node) |
static java.util.List<? extends javax.lang.model.element.AnnotationMirror> |
annotationsFromTree(com.sun.source.tree.TypeParameterTree node) |
static java.util.List<javax.lang.model.element.AnnotationMirror> |
annotationsFromTypeAnnotationTrees(java.util.List<? extends com.sun.source.tree.AnnotationTree> annos) |
static boolean |
canHaveTypeAnnotation(com.sun.source.tree.Tree tree) |
static java.util.Set<com.sun.source.tree.Tree.Kind> |
classTreeKinds() |
static javax.lang.model.element.ExecutableElement |
constructor(com.sun.source.tree.NewClassTree tree)
Determines the symbol for a constructor given an invocation via
new . |
static boolean |
containsThisConstructorInvocation(com.sun.source.tree.MethodTree node) |
static javax.lang.model.element.TypeElement |
elementFromDeclaration(com.sun.source.tree.ClassTree node)
Gets the element for a class corresponding to a declaration.
|
static javax.lang.model.element.ExecutableElement |
elementFromDeclaration(com.sun.source.tree.MethodTree node)
Gets the element for a method corresponding to a declaration.
|
static javax.lang.model.element.VariableElement |
elementFromDeclaration(com.sun.source.tree.VariableTree node)
Gets the element for a variable corresponding to its declaration.
|
static @Nullable javax.lang.model.element.Element |
elementFromTree(com.sun.source.tree.Tree tree)
Gets the
Element for the given Tree API node. |
static javax.lang.model.element.Element |
elementFromUse(com.sun.source.tree.ExpressionTree node)
Gets the element for the declaration corresponding to this use of an element.
|
static javax.lang.model.element.ExecutableElement |
elementFromUse(com.sun.source.tree.MethodInvocationTree node) |
static javax.lang.model.element.ExecutableElement |
elementFromUse(com.sun.source.tree.NewClassTree node)
Specialization for return type.
|
static @Nullable com.sun.source.tree.ClassTree |
enclosingClass(@Nullable com.sun.source.util.TreePath path)
Gets the enclosing class of the tree node defined by the given
TreePath . |
static @Nullable com.sun.source.tree.MethodTree |
enclosingMethod(@Nullable com.sun.source.util.TreePath path)
Gets the enclosing method of the tree node defined by the given
TreePath . |
static @Nullable com.sun.source.tree.Tree |
enclosingMethodOrLambda(@Nullable com.sun.source.util.TreePath path)
Gets the enclosing method or lambda expression of the tree node defined by the given
TreePath . |
static Pair<com.sun.source.tree.Tree,com.sun.source.tree.Tree> |
enclosingNonParen(com.sun.source.util.TreePath path)
Gets the first enclosing tree in path, that is not a parenthesis.
|
static <T extends com.sun.source.tree.Tree> |
enclosingOfClass(com.sun.source.util.TreePath path,
java.lang.Class<T> treeClass)
Gets the first enclosing tree in path, of the specified class.
|
static com.sun.source.tree.Tree |
enclosingOfKind(com.sun.source.util.TreePath path,
java.util.Set<com.sun.source.tree.Tree.Kind> kinds)
Gets the first enclosing tree in path, with any one of the specified kinds.
|
static com.sun.source.tree.Tree |
enclosingOfKind(com.sun.source.util.TreePath path,
com.sun.source.tree.Tree.Kind kind)
Gets the first enclosing tree in path, of the specified kind.
|
static @Nullable com.sun.source.tree.BlockTree |
enclosingTopLevelBlock(com.sun.source.util.TreePath path) |
static com.sun.source.tree.VariableTree |
enclosingVariable(com.sun.source.util.TreePath path)
Gets the enclosing variable of a tree node defined by the given
TreePath . |
static com.sun.tools.javac.code.Symbol |
findFunction(com.sun.source.tree.Tree tree,
javax.annotation.processing.ProcessingEnvironment env)
The type of the lambda or method reference tree is a functional interface type.
|
static com.sun.source.tree.Tree |
firstStatement(com.sun.source.tree.Tree tree) |
static com.sun.source.tree.Tree |
getAssignmentContext(com.sun.source.util.TreePath treePath)
Returns the tree with the assignment context for the treePath leaf node.
|
static javax.lang.model.element.VariableElement |
getField(java.lang.String typeName,
java.lang.String fieldName,
javax.annotation.processing.ProcessingEnvironment env)
Returns the VariableElement for a field declaration.
|
static java.lang.String |
getFieldName(com.sun.source.tree.Tree tree)
Compute the name of the field that the field access
tree accesses. |
static javax.lang.model.element.ExecutableElement |
getMethod(java.lang.String typeName,
java.lang.String methodName,
int params,
javax.annotation.processing.ProcessingEnvironment env)
Returns the ExecutableElement for the method declaration of methodName, in class typeName,
with params formal parameters.
|
static javax.lang.model.element.ExecutableElement |
getMethod(java.lang.String typeName,
java.lang.String methodName,
javax.annotation.processing.ProcessingEnvironment env,
java.lang.String... paramTypes)
Returns the ExecutableElement for a method declaration of methodName, in class typeName, with
formal parameters of the given types.
|
static java.lang.String |
getMethodName(com.sun.source.tree.Tree tree)
Compute the name of the method that the method access
tree accesses. |
static java.util.List<javax.lang.model.element.ExecutableElement> |
getMethods(java.lang.String typeName,
java.lang.String methodName,
int params,
javax.annotation.processing.ProcessingEnvironment env)
Returns all ExecutableElements for method declarations of methodName, in class typeName, with
params formal parameters.
|
static com.sun.source.tree.ExpressionTree |
getReceiverTree(com.sun.source.tree.ExpressionTree expression)
Returns the receiver tree of a field access or a method invocation.
|
static boolean |
hasExplicitConstructor(com.sun.source.tree.ClassTree node)
Determine whether the given class contains an explicit constructor.
|
static boolean |
isAnonymousConstructor(com.sun.source.tree.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(com.sun.source.tree.Tree tree)
Returns whether or not tree is an access of array length.
|
static boolean |
isClassLiteral(com.sun.source.tree.Tree tree)
Determine whether
tree is a class literal, such as. |
static boolean |
isClassTree(com.sun.source.tree.Tree tree)
Is the given tree kind a class, i.e.
|
static boolean |
isCompileTimeString(com.sun.source.tree.ExpressionTree node)
Returns true if the node is a constant-time expression.
|
static boolean |
isConstructor(com.sun.source.tree.MethodTree tree)
Checks if the provided method is a constructor method or no.
|
static boolean |
isDiamondTree(com.sun.source.tree.Tree tree)
Returns true if the tree is of a diamond type.
|
static boolean |
isEnumSuper(com.sun.source.tree.MethodInvocationTree node) |
static boolean |
isExplicitThisDereference(com.sun.source.tree.ExpressionTree tree)
Determine whether the given expression is either "this" or an outer "C.this".
|
static boolean |
isExpressionTree(com.sun.source.tree.Tree tree)
Determine whether the given tree represents an ExpressionTree.
|
static boolean |
isFieldAccess(com.sun.source.tree.Tree tree)
Determine whether
tree is a field access expressions, such as. |
static boolean |
isImplicitlyTypedLambda(com.sun.source.tree.Tree tree)
Returns true if
tree is an implicitly typed lambda. |
static boolean |
isLocalVariable(com.sun.source.tree.Tree tree) |
static boolean |
isMethodAccess(com.sun.source.tree.Tree tree)
Determine whether
tree refers to a method element, such as. |
static boolean |
isMethodInvocation(com.sun.source.tree.Tree tree,
javax.lang.model.element.ExecutableElement method,
javax.annotation.processing.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(com.sun.source.tree.Tree methodTree,
java.util.List<javax.lang.model.element.ExecutableElement> methods,
javax.annotation.processing.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(com.sun.source.tree.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(com.sun.source.tree.Tree tree,
javax.lang.model.element.VariableElement var)
Returns true if and only if the given
tree represents a field access of the given
VariableElement . |
static boolean |
isStringCompoundConcatenation(com.sun.source.tree.CompoundAssignmentTree tree)
Returns true if the compound assignment tree is a string concatenation.
|
static boolean |
isStringConcatenation(com.sun.source.tree.Tree tree)
Returns true if the tree represents a
String concatenation operation. |
static boolean |
isSuperConstructorCall(com.sun.source.tree.MethodInvocationTree tree)
Checks if the method invocation is a call to super.
|
static boolean |
isThisConstructorCall(com.sun.source.tree.MethodInvocationTree tree)
Checks if the method invocation is a call to "this".
|
static boolean |
isTreeInStaticScope(com.sun.source.util.TreePath path)
Returns whether or not the leaf of the tree path is in a static scope.
|
static boolean |
isTypeDeclaration(com.sun.source.tree.Tree node)
Determine whether the given tree represents a declaration of a type (including type
parameters).
|
static boolean |
isTypeTree(com.sun.source.tree.Tree tree)
Is the given tree a type instantiation?
|
static boolean |
isUseOfElement(com.sun.source.tree.ExpressionTree node)
Determine whether the given ExpressionTree has an underlying element.
|
static javax.lang.model.element.Name |
methodName(com.sun.source.tree.MethodInvocationTree node) |
static com.sun.source.util.TreePath |
pathTillClass(com.sun.source.util.TreePath path)
Gets path to the first enclosing class tree, where class is defined by the classTreeKinds
method.
|
static com.sun.source.util.TreePath |
pathTillOfKind(com.sun.source.util.TreePath path,
java.util.Set<com.sun.source.tree.Tree.Kind> kinds)
Gets path to the first enclosing tree with any one of the specified kinds.
|
static com.sun.source.util.TreePath |
pathTillOfKind(com.sun.source.util.TreePath path,
com.sun.source.tree.Tree.Kind kind)
Gets path to the first enclosing tree of the specified kind.
|
static javax.lang.model.type.TypeMirror |
typeOf(com.sun.source.tree.Tree tree) |
static java.util.Set<com.sun.source.tree.Tree.Kind> |
typeTreeKinds() |
static com.sun.source.tree.ExpressionTree |
withoutParens(com.sun.source.tree.ExpressionTree tree)
If the given tree is a parenthesized tree, return the enclosed non-parenthesized tree.
|
public static boolean isConstructor(com.sun.source.tree.MethodTree tree)
tree
- a tree defining the methodpublic static boolean isSuperConstructorCall(com.sun.source.tree.MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isThisConstructorCall(com.sun.source.tree.MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isSelfAccess(com.sun.source.tree.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 com.sun.source.tree.Tree enclosingOfKind(com.sun.source.util.TreePath path, com.sun.source.tree.Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treepublic static com.sun.source.tree.Tree enclosingOfKind(com.sun.source.util.TreePath path, java.util.Set<com.sun.source.tree.Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treepublic static com.sun.source.util.TreePath pathTillClass(com.sun.source.util.TreePath path)
path
- the path defining the tree nodepublic static com.sun.source.util.TreePath pathTillOfKind(com.sun.source.util.TreePath path, com.sun.source.tree.Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treepublic static com.sun.source.util.TreePath pathTillOfKind(com.sun.source.util.TreePath path, java.util.Set<com.sun.source.tree.Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treepublic static <T extends com.sun.source.tree.Tree> T enclosingOfClass(com.sun.source.util.TreePath path, java.lang.Class<T> treeClass)
path
- the path defining the tree nodetreeClass
- the class of the desired treepublic static @Nullable com.sun.source.tree.ClassTree enclosingClass(@Nullable com.sun.source.util.TreePath path)
TreePath
. It returns a
Tree
, from which checkers.types.AnnotatedTypeMirror
or Element
can be
obtained.path
- the path defining the tree nodepublic static com.sun.source.tree.VariableTree enclosingVariable(com.sun.source.util.TreePath path)
TreePath
.path
- the path defining the tree nodepublic static @Nullable com.sun.source.tree.MethodTree enclosingMethod(@Nullable com.sun.source.util.TreePath path)
TreePath
. It returns
a Tree
, from which an checkers.types.AnnotatedTypeMirror
or Element
can be obtained.path
- the path defining the tree nodepublic static @Nullable com.sun.source.tree.Tree enclosingMethodOrLambda(@Nullable com.sun.source.util.TreePath path)
TreePath
. It returns a Tree
, from which an checkers.types.AnnotatedTypeMirror
or Element
can be obtained.path
- the path defining the tree nodepublic static @Nullable com.sun.source.tree.BlockTree enclosingTopLevelBlock(com.sun.source.util.TreePath path)
public static com.sun.source.tree.ExpressionTree withoutParens(com.sun.source.tree.ExpressionTree tree)
tree
- an expression treepublic static Pair<com.sun.source.tree.Tree,com.sun.source.tree.Tree> enclosingNonParen(com.sun.source.util.TreePath path)
path
- the path defining the tree nodepublic static com.sun.source.tree.Tree getAssignmentContext(com.sun.source.util.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.
public static @Nullable javax.lang.model.element.Element elementFromTree(com.sun.source.tree.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 foundjava.lang.IllegalArgumentException
- if tree
is null or is not a valid javac-internal
tree (JCTree)public static final javax.lang.model.element.TypeElement elementFromDeclaration(com.sun.source.tree.ClassTree node)
public static final javax.lang.model.element.ExecutableElement elementFromDeclaration(com.sun.source.tree.MethodTree node)
public static final javax.lang.model.element.VariableElement elementFromDeclaration(com.sun.source.tree.VariableTree node)
public static final javax.lang.model.element.Element elementFromUse(com.sun.source.tree.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 elementpublic static final javax.lang.model.element.ExecutableElement elementFromUse(com.sun.source.tree.MethodInvocationTree node)
public static final javax.lang.model.element.ExecutableElement elementFromUse(com.sun.source.tree.NewClassTree node)
constructor(NewClassTree)
public static javax.lang.model.element.ExecutableElement constructor(com.sun.source.tree.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)
public static final boolean isUseOfElement(com.sun.source.tree.ExpressionTree node)
node
- the ExpressionTree to testpublic static final javax.lang.model.element.Name methodName(com.sun.source.tree.MethodInvocationTree node)
public static final boolean containsThisConstructorInvocation(com.sun.source.tree.MethodTree node)
public static final com.sun.source.tree.Tree firstStatement(com.sun.source.tree.Tree tree)
public static boolean hasExplicitConstructor(com.sun.source.tree.ClassTree node)
node
- a class treepublic static final boolean isDiamondTree(com.sun.source.tree.Tree tree)
TreeInfo.isDiamond(JCTree)
public static final boolean isStringConcatenation(com.sun.source.tree.Tree tree)
String
concatenation operation.public static final boolean isStringCompoundConcatenation(com.sun.source.tree.CompoundAssignmentTree tree)
public static boolean isCompileTimeString(com.sun.source.tree.ExpressionTree node)
A tree is a constant-time expression if it is:
public static com.sun.source.tree.ExpressionTree getReceiverTree(com.sun.source.tree.ExpressionTree expression)
public static java.util.Set<com.sun.source.tree.Tree.Kind> classTreeKinds()
public static boolean isClassTree(com.sun.source.tree.Tree tree)
tree
- the tree to testpublic static java.util.Set<com.sun.source.tree.Tree.Kind> typeTreeKinds()
public static boolean isTypeTree(com.sun.source.tree.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(com.sun.source.tree.Tree tree, javax.lang.model.element.ExecutableElement method, javax.annotation.processing.ProcessingEnvironment env)
public static boolean isMethodInvocation(com.sun.source.tree.Tree methodTree, java.util.List<javax.lang.model.element.ExecutableElement> methods, javax.annotation.processing.ProcessingEnvironment processingEnv)
public static javax.lang.model.element.ExecutableElement getMethod(java.lang.String typeName, java.lang.String methodName, int params, javax.annotation.processing.ProcessingEnvironment env)
getMethod(String,
String, ProcessingEnvironment, String...)
.public static java.util.List<javax.lang.model.element.ExecutableElement> getMethods(java.lang.String typeName, java.lang.String methodName, int params, javax.annotation.processing.ProcessingEnvironment env)
public static javax.lang.model.element.ExecutableElement getMethod(java.lang.String typeName, java.lang.String methodName, javax.annotation.processing.ProcessingEnvironment env, java.lang.String... paramTypes)
public static final boolean isExplicitThisDereference(com.sun.source.tree.ExpressionTree tree)
TODO: Should this also handle "super"?
public static boolean isClassLiteral(com.sun.source.tree.Tree tree)
tree
is a class literal, such as.
Object . class
public static boolean isFieldAccess(com.sun.source.tree.Tree tree)
tree
is a field access expressions, such as.
f obj . f
public static java.lang.String getFieldName(com.sun.source.tree.Tree tree)
tree
accesses. Requires tree
to be a field access, as determined by isFieldAccess
.tree
.public static boolean isMethodAccess(com.sun.source.tree.Tree tree)
tree
refers to a method element, such as.
m(...) obj . m(...)
public static java.lang.String getMethodName(com.sun.source.tree.Tree tree)
tree
accesses. Requires tree
to be a method access, as determined by isMethodAccess
.tree
.public static boolean canHaveTypeAnnotation(com.sun.source.tree.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(com.sun.source.tree.Tree tree, javax.lang.model.element.VariableElement var)
tree
represents a field access of the given
VariableElement
.public static javax.lang.model.element.VariableElement getField(java.lang.String typeName, java.lang.String fieldName, javax.annotation.processing.ProcessingEnvironment env)
typeName
- the class where the field is declaredfieldName
- the name of the fieldenv
- the processing environmentpublic static boolean isExpressionTree(com.sun.source.tree.Tree tree)
tree
- the Tree to testpublic static boolean isEnumSuper(com.sun.source.tree.MethodInvocationTree node)
node
- the method invocation to checkEnum
constructorpublic static boolean isTypeDeclaration(com.sun.source.tree.Tree node)
node
- the Tree to testpublic static boolean isTreeInStaticScope(com.sun.source.util.TreePath path)
path
- TreePath whose leaf may or may not be in static scopepublic static boolean isArrayLengthAccess(com.sun.source.tree.Tree tree)
tree
- tree to checkpublic static boolean isAnonymousConstructor(com.sun.source.tree.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 final java.util.List<javax.lang.model.element.AnnotationMirror> annotationsFromTypeAnnotationTrees(java.util.List<? extends com.sun.source.tree.AnnotationTree> annos)
public static javax.lang.model.element.AnnotationMirror annotationFromAnnotationTree(com.sun.source.tree.AnnotationTree tree)
public static final java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotationsFromTree(com.sun.source.tree.AnnotatedTypeTree node)
public static final java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotationsFromTree(com.sun.source.tree.TypeParameterTree node)
public static final java.util.List<? extends javax.lang.model.element.AnnotationMirror> annotationsFromArrayCreation(com.sun.source.tree.NewArrayTree node, int level)
public static boolean isLocalVariable(com.sun.source.tree.Tree tree)
public static javax.lang.model.type.TypeMirror typeOf(com.sun.source.tree.Tree tree)
tree
public static com.sun.tools.javac.code.Symbol findFunction(com.sun.source.tree.Tree tree, javax.annotation.processing.ProcessingEnvironment env)
tree
- lambda or member reference treeenv
- ProcessingEnvironmentpublic static boolean isImplicitlyTypedLambda(com.sun.source.tree.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.