public final class TreeUtils
extends java.lang.Object
Tree
.Modifier and Type | Method and Description |
---|---|
static boolean |
canHaveTypeAnnotation(com.sun.source.tree.Tree tree) |
static java.util.Set<com.sun.source.tree.Tree.Kind> |
classTreeKinds() |
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 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) |
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
{@link 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
{@link TreePath} . |
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.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 a method declaration of
methodName, in class typeName, with params parameters.
|
static java.lang.String |
getMethodName(com.sun.source.tree.Tree tree)
Compute the name of the method that the method access
tree
accesses. |
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 |
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 |
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 |
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 |
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 |
isSuperCall(com.sun.source.tree.MethodInvocationTree tree)
Checks if the method invocation is a call to super.
|
static boolean |
isTypeTree(com.sun.source.tree.Tree tree)
Is the given tree a type instantiation?
TODO: this is an under-approximation: e.g.
|
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 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 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 the first enclosing tree of the specified kind.
|
static com.sun.source.tree.ExpressionTree |
skipParens(com.sun.source.tree.ExpressionTree tree)
If the given tree is a parenthesized tree, it returns the enclosed
non-parenthesized tree.
|
static java.util.Set<com.sun.source.tree.Tree.Kind> |
typeTreeKinds() |
public static boolean isConstructor(com.sun.source.tree.MethodTree tree)
tree
- a tree defining the methodpublic static boolean isSuperCall(com.sun.source.tree.MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isSelfAccess(com.sun.source.tree.ExpressionTree tree)
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)
{@link 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)
{@link 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 skipParens(com.sun.source.tree.ExpressionTree tree)
tree
- an expression treepublic static com.sun.source.tree.Tree getAssignmentContext(com.sun.source.util.TreePath treePath)
treePath
- public static final javax.lang.model.element.TypeElement elementFromDeclaration(com.sun.source.tree.ClassTree node)
node
- public static final javax.lang.model.element.ExecutableElement elementFromDeclaration(com.sun.source.tree.MethodTree node)
node
- public static final javax.lang.model.element.VariableElement elementFromDeclaration(com.sun.source.tree.VariableTree node)
node
- public static final javax.lang.model.element.Element elementFromUse(com.sun.source.tree.ExpressionTree node)
Trees.getElement(TreePath)
instead.
TODO: remove this method, as it really doesn't do anything.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)
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 tree.public 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
operationpublic static final boolean isStringCompoundConcatenation(com.sun.source.tree.CompoundAssignmentTree tree)
public static boolean isCompileTimeString(com.sun.source.tree.ExpressionTree node)
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)
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 javax.lang.model.element.ExecutableElement getMethod(java.lang.String typeName, java.lang.String methodName, int params, javax.annotation.processing.ProcessingEnvironment env)
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 declared.fieldName
- the name of the field.env
- the processing environment.public static boolean isExpressionTree(com.sun.source.tree.Tree tree)
tree
- the Tree to test.public static boolean isEnumSuper(com.sun.source.tree.MethodInvocationTree node)
node
- the method invocation to checkEnum
constructor