public final class TreeUtils extends Object
Tree
.Modifier and Type | Method and Description |
---|---|
static AnnotationMirror |
annotationFromAnnotationTree(AnnotationTree tree) |
static List<? extends AnnotationMirror> |
annotationsFromArrayCreation(NewArrayTree node,
int level) |
static List<? extends AnnotationMirror> |
annotationsFromTree(AnnotatedTypeTree node) |
static List<? extends AnnotationMirror> |
annotationsFromTree(TypeParameterTree node) |
static List<AnnotationMirror> |
annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annos) |
static boolean |
canHaveTypeAnnotation(Tree tree) |
static Set<Tree.Kind> |
classTreeKinds() |
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 Element |
elementFromUse(ExpressionTree node)
Gets the element for the declaration corresponding to this use of an element.
|
static ExecutableElement |
elementFromUse(MethodInvocationTree node) |
static ExecutableElement |
elementFromUse(NewClassTree node)
Specialization for return type.
|
static @Nullable ClassTree |
enclosingClass(@Nullable TreePath path)
Gets the enclosing class of the tree node defined by the given
TreePath . |
static @Nullable MethodTree |
enclosingMethod(@Nullable TreePath path)
Gets the enclosing method of the tree node defined by the given
TreePath . |
static @Nullable Tree |
enclosingMethodOrLambda(@Nullable TreePath path)
Gets the enclosing method or lambda expression of the tree node defined by the given
TreePath . |
static <T extends Tree> |
enclosingOfClass(TreePath path,
Class<T> treeClass)
Gets the first enclosing tree in path, of the specified class
|
static Tree |
enclosingOfKind(TreePath path,
Set<Tree.Kind> kinds)
Gets the first enclosing tree in path, with any one of the specified kinds.
|
static 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 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) |
static 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 List<ExecutableElement> |
getMethodList(String typeName,
String methodName,
int params,
ProcessingEnvironment env)
Returns all ExecutableElements for method declarations of methodName, in class typeName, with
params formal parameters.
|
static String |
getMethodName(Tree tree)
Compute the name of the method that the method access
tree accesses. |
static 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 |
isGetClassInvocation(MethodInvocationTree invocationTree) |
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.
|
protected static boolean |
isNamedMethodCall(String name,
MethodInvocationTree tree) |
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 |
isSuperCall(MethodInvocationTree tree)
Checks if the method invocation is a call to super.
|
static boolean |
isThisCall(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 TreePath |
pathTillClass(TreePath path)
Gets path to the first enclosing class tree, where class is defined by the classTreeKinds
method.
|
static TreePath |
pathTillOfKind(TreePath path,
Set<Tree.Kind> kinds)
Gets path to the first enclosing tree with any one of the specified kinds.
|
static TreePath |
pathTillOfKind(TreePath path,
Tree.Kind kind)
Gets path to the first enclosing tree of the specified kind.
|
static ExpressionTree |
skipParens(ExpressionTree tree)
If the given tree is a parenthesized tree, it returns the enclosed non-parenthesized tree.
|
static TypeMirror |
typeOf(Tree tree) |
static Set<Tree.Kind> |
typeTreeKinds() |
public static boolean isConstructor(MethodTree tree)
tree
- a tree defining the methodpublic static boolean isSuperCall(MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isThisCall(MethodInvocationTree tree)
tree
- a tree defining a method invocationprotected static boolean isNamedMethodCall(String name, MethodInvocationTree tree)
public 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 Tree enclosingOfKind(TreePath path, Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treepublic static Tree enclosingOfKind(TreePath path, Set<Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treepublic static TreePath pathTillClass(TreePath path)
path
- the path defining the tree nodepublic static TreePath pathTillOfKind(TreePath path, Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treepublic static TreePath pathTillOfKind(TreePath path, Set<Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treepublic static <T extends Tree> T enclosingOfClass(TreePath path, Class<T> treeClass)
path
- the path defining the tree nodetreeClass
- the class of the desired treepublic static @Nullable ClassTree enclosingClass(@Nullable 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 VariableTree enclosingVariable(TreePath path)
TreePath
.path
- the path defining the tree nodepublic static @Nullable MethodTree enclosingMethod(@Nullable 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 Tree enclosingMethodOrLambda(@Nullable 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 ExpressionTree skipParens(ExpressionTree tree)
tree
- an expression treepublic static 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.
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 final TypeElement elementFromDeclaration(ClassTree node)
public static final ExecutableElement elementFromDeclaration(MethodTree node)
public static final VariableElement elementFromDeclaration(VariableTree node)
public static final 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 elementpublic static final ExecutableElement elementFromUse(MethodInvocationTree node)
public static final 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)
public static final boolean isUseOfElement(ExpressionTree node)
node
- the ExpressionTree to testpublic static final Name methodName(MethodInvocationTree node)
public static final boolean containsThisConstructorInvocation(MethodTree node)
public static boolean hasExplicitConstructor(ClassTree node)
node
- a class treepublic static final boolean isDiamondTree(Tree tree)
TreeInfo.isDiamond(JCTree)
public static final boolean isStringConcatenation(Tree tree)
String
concatenation operationpublic static final boolean isStringCompoundConcatenation(CompoundAssignmentTree tree)
public static boolean isCompileTimeString(ExpressionTree node)
A tree is a constant-time expression if it is:
public static ExpressionTree getReceiverTree(ExpressionTree expression)
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 ExecutableElement getMethod(String typeName, String methodName, int params, ProcessingEnvironment env)
getMethod(String,
String, ProcessingEnvironment, String...)
.public static List<ExecutableElement> getMethodList(String typeName, String methodName, int params, ProcessingEnvironment env)
public static ExecutableElement getMethod(String typeName, String methodName, ProcessingEnvironment env, String... paramTypes)
public static final 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)
TODO: is there a nicer way than an instanceof?
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 isGetClassInvocation(MethodInvocationTree invocationTree)
Object.getClass()
public 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 final List<AnnotationMirror> annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annos)
public static AnnotationMirror annotationFromAnnotationTree(AnnotationTree tree)
public static final List<? extends AnnotationMirror> annotationsFromTree(AnnotatedTypeTree node)
public static final List<? extends AnnotationMirror> annotationsFromTree(TypeParameterTree node)
public static final List<? extends AnnotationMirror> annotationsFromArrayCreation(NewArrayTree node, int level)
public static TypeMirror typeOf(Tree tree)
public static com.sun.tools.javac.code.Symbol findFunction(Tree tree, ProcessingEnvironment env)
tree
- lambda or member reference treeenv
- ProcessingEnvironment