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
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 <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 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.util.List<javax.lang.model.element.ExecutableElement> |
getMethodList(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 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 |
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 |
isGetClassInvocation(com.sun.source.tree.MethodInvocationTree invocationTree) |
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.
|
protected static boolean |
isNamedMethodCall(java.lang.String name,
com.sun.source.tree.MethodInvocationTree tree) |
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 |
isThisCall(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 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 isThisCall(com.sun.source.tree.MethodInvocationTree tree)
tree
- a tree defining a method invocationprotected static boolean isNamedMethodCall(java.lang.String name, com.sun.source.tree.MethodInvocationTree tree)
public 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.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)
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 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)
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 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 operationpublic 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 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> getMethodList(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)
TODO: is there a nicer way than an instanceof?
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 isGetClassInvocation(com.sun.source.tree.MethodInvocationTree invocationTree)
Object.getClass()
public 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 check