public final class TreeUtils extends Object
Tree
.Modifier and Type | Method and Description |
---|---|
static boolean |
canHaveTypeAnnotation(Tree tree) |
static Set<Tree.Kind> |
classTreeKinds() |
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 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) |
static @Nullable ClassTree |
enclosingClass(@Nullable TreePath path)
Gets the enclosing class of the tree node defined by the given
{@link TreePath} . |
static @Nullable MethodTree |
enclosingMethod(@Nullable TreePath path)
Gets the enclosing method of the tree node defined by the given
{@link 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 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 a method declaration of
methodName, in class typeName, with params 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 |
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.
|
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 |
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?
TODO: this is an under-approximation: e.g.
|
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 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 isSelfAccess(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 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)
{@link 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)
{@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 ExpressionTree skipParens(ExpressionTree tree)
tree
- an expression treepublic static Tree getAssignmentContext(TreePath treePath)
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)
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 ExecutableElement elementFromUse(MethodInvocationTree node)
public static final ExecutableElement elementFromUse(NewClassTree node)
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 tree.public 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)
public static ExpressionTree getReceiverTree(ExpressionTree expression)
public static boolean isClassTree(Tree tree)
tree
- the tree to testpublic static boolean isTypeTree(Tree tree)
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)
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 declared.fieldName
- the name of the field.env
- the processing environment.public static boolean isExpressionTree(Tree tree)
tree
- the Tree to test.public 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()