public final class TreeUtils extends Object
Tree
.Modifier and Type | Field and Description |
---|---|
static org.plumelib.util.UniqueIdMap<Tree> |
treeUids
Unique IDs for trees.
|
Modifier and Type | Method and Description |
---|---|
static AnnotationMirror |
annotationFromAnnotationTree(AnnotationTree tree)
Converts the given AnnotationTree to an AnnotationMirror.
|
static List<? extends AnnotationMirror> |
annotationsFromArrayCreation(NewArrayTree tree,
int level)
Converts the given NewArrayTree to a list of AnnotationMirrors.
|
static List<? extends AnnotationMirror> |
annotationsFromTree(AnnotatedTypeTree tree)
Converts the given AnnotatedTypeTree to a list of AnnotationMirrors.
|
static List<? extends AnnotationMirror> |
annotationsFromTree(TypeParameterTree tree)
Converts the given TypeParameterTree to a list of AnnotationMirrors.
|
static List<AnnotationMirror> |
annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annoTreess)
Converts the given AnnotationTrees to AnnotationMirrors.
|
static boolean |
canHaveTypeAnnotation(Tree tree)
Return
true if and only if tree can have a type annotation. |
static Set<Tree.Kind> |
classTreeKinds()
Return the set of kinds that represent classes.
|
static ExecutableElement |
constructor(NewClassTree tree)
Determines the symbol for a constructor given an invocation via
new . |
static boolean |
containsThisConstructorInvocation(MethodTree node)
Returns true if the first statement in the body is a self constructor invocation within a
constructor.
|
static LiteralTree |
createLiteral(com.sun.tools.javac.code.TypeTag typeTag,
@Nullable Object value,
TypeMirror typeMirror,
ProcessingEnvironment processingEnv)
Creates a LiteralTree for the given value.
|
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 @Nullable Element |
elementFromUse(ExpressionTree node)
Gets the element for the declaration corresponding to this use of an element.
|
static @Nullable ExecutableElement |
elementFromUse(MethodInvocationTree node)
Returns the ExecutableElement for the called method, from a call.
|
static @Nullable ExecutableElement |
elementFromUse(NewClassTree node)
Gets the ExecutableElement for the called constrctor, from a constructor invocation.
|
static @Nullable ClassTree |
enclosingClass(TreePath path)
Gets the enclosing class of the tree node defined by the given
TreePath . |
static @Nullable MethodTree |
enclosingMethod(TreePath path)
Gets the enclosing method of the tree node defined by the given
TreePath . |
static @Nullable Tree |
enclosingMethodOrLambda(TreePath path)
Gets the enclosing method or lambda expression of the tree node defined by the given
TreePath . |
static Pair<Tree,Tree> |
enclosingNonParen(TreePath path)
Gets the first enclosing tree in path, that is not a parenthesis.
|
static <T extends Tree> |
enclosingOfClass(TreePath path,
Class<T> treeClass)
Gets the first enclosing tree in path, of the specified class.
|
static @Nullable Tree |
enclosingOfKind(TreePath path,
Set<Tree.Kind> kinds)
Gets the first enclosing tree in path, with any one of the specified kinds.
|
static @Nullable 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 @Nullable 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)
Returns the first statement of the tree if it is a block.
|
static @Nullable Tree |
getAssignmentContext(TreePath treePath)
Returns the tree with the assignment context for the treePath leaf node.
|
static LiteralTree |
getDefaultValueTree(TypeMirror typeMirror,
ProcessingEnvironment processingEnv)
Return a tree for the default value of the given type.
|
static List<? extends AnnotationTree> |
getExplicitAnnotationTrees(List<? extends AnnotationTree> annoTrees,
Tree typeTree)
Returns the annotations explicitly written on the given type.
|
static VariableElement |
getField(@FullyQualifiedName 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(@FullyQualifiedName String typeName,
String methodName,
int params,
ProcessingEnvironment env)
Returns the ExecutableElement for a method declaration.
|
static ExecutableElement |
getMethod(@FullyQualifiedName String typeName,
String methodName,
ProcessingEnvironment env,
String... paramTypes)
Returns the ExecutableElement for a method declaration.
|
static String |
getMethodName(Tree tree)
Compute the name of the method that the method access
tree accesses. |
static List<ExecutableElement> |
getMethods(@FullyQualifiedName String typeName,
String methodName,
int params,
ProcessingEnvironment env)
Returns all ExecutableElements for method declarations of methodName, in class typeName, with
params formal parameters.
|
static @Nullable 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 |
hasSyntheticArgument(NewClassTree tree)
Returns true if
tree has a synthetic argument. |
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)
Returns true if this is a super call to the
Enum constructor. |
static boolean |
isExplicitThisDereference(ExpressionTree tree)
Determine whether the given expression is either "this" or an outer "C.this".
|
static boolean |
isExprConstTrue(ExpressionTree node)
Determine whether an expression
ExpressionTree has the constant value true, according
to the compiler logic. |
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 |
isImplicitlyTypedLambda(Tree tree)
Returns true if
tree is an implicitly typed lambda. |
static boolean |
isLocalVariable(Tree tree)
Returns true if the tree is the declaration or use of a local variable.
|
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 |
isMethodInvocation(Tree methodTree,
List<ExecutableElement> methods,
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(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 |
isSuperConstructorCall(MethodInvocationTree tree)
Checks if the method invocation is a call to super.
|
static boolean |
isThisConstructorCall(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 boolean |
isWideningBinary(BinaryTree node)
Returns true if the binary operator may do a widening primitive conversion.
|
static Name |
methodName(MethodInvocationTree node)
Returns the name of the invoked method.
|
static @Nullable TreePath |
pathTillClass(TreePath path)
Gets path to the first enclosing class tree, where class is defined by the classTreeKinds
method.
|
static @Nullable TreePath |
pathTillOfKind(TreePath path,
Set<Tree.Kind> kinds)
Gets path to the first enclosing tree with any one of the specified kinds.
|
static @Nullable TreePath |
pathTillOfKind(TreePath path,
Tree.Kind kind)
Gets path to the first enclosing tree of the specified kind.
|
static String |
toStringOneLine(Tree tree)
Return toString(), but without line separators.
|
static String |
toStringTruncated(Tree tree,
int length)
Return either
toStringOneLine(com.sun.source.tree.Tree) if it is no more than length characters, or
toStringOneLine(com.sun.source.tree.Tree) quoted and truncated. |
static TypeMirror |
typeOf(Tree tree)
Returns the type as a TypeMirror of
tree . |
static Set<Tree.Kind> |
typeTreeKinds() |
static @PolyInterned ExpressionTree |
withoutParens(@PolyInterned ExpressionTree tree)
If the given tree is a parenthesized tree, return the enclosed non-parenthesized tree.
|
public static final org.plumelib.util.UniqueIdMap<Tree> treeUids
public static boolean isConstructor(MethodTree tree)
tree
- a tree defining the methodpublic static boolean isSuperConstructorCall(MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic static boolean isThisConstructorCall(MethodInvocationTree tree)
tree
- a tree defining a method invocationpublic 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 @Nullable Tree enclosingOfKind(TreePath path, Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treenull
otherwisepublic static @Nullable Tree enclosingOfKind(TreePath path, Set<Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treenull
otherwisepublic static @Nullable TreePath pathTillClass(TreePath path)
path
- the path defining the tree nodenull
otherwisepublic static @Nullable TreePath pathTillOfKind(TreePath path, Tree.Kind kind)
path
- the path defining the tree nodekind
- the kind of the desired treenull
otherwisepublic static @Nullable TreePath pathTillOfKind(TreePath path, Set<Tree.Kind> kinds)
path
- the path defining the tree nodekinds
- the set of kinds of the desired treenull
otherwisepublic static <T extends Tree> T enclosingOfClass(TreePath path, Class<T> treeClass)
path
- the path defining the tree nodetreeClass
- the class of the desired treenull
otherwisepublic static @Nullable ClassTree enclosingClass(TreePath path)
TreePath
. It returns a
Tree
, from which checkers.types.AnnotatedTypeMirror
or Element
can be
obtained.path
- the path defining the tree nodenull
if one does
not existpublic static @Nullable VariableTree enclosingVariable(TreePath path)
TreePath
.path
- the path defining the tree nodenull
if one does not existpublic static @Nullable MethodTree enclosingMethod(TreePath path)
TreePath
. It returns
a Tree
, from which an checkers.types.AnnotatedTypeMirror
or Element
can be obtained.path
- the path defining the tree nodenull
if one does not existpublic static @Nullable Tree enclosingMethodOrLambda(TreePath path)
TreePath
. It returns a Tree
, from which an checkers.types.AnnotatedTypeMirror
or Element
can be obtained.path
- the path defining the tree nodenull
if one does not
existpublic static @PolyInterned ExpressionTree withoutParens(@PolyInterned ExpressionTree tree)
tree
- an expression treepublic static Pair<Tree,Tree> enclosingNonParen(TreePath path)
path
- the path defining the tree nodepublic static @Nullable 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.
null
otherwise@Pure 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 TypeElement elementFromDeclaration(ClassTree node)
public static ExecutableElement elementFromDeclaration(MethodTree node)
public static VariableElement elementFromDeclaration(VariableTree node)
@Pure public static @Nullable 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 elementnull
otherwisepublic static @Nullable ExecutableElement elementFromUse(MethodInvocationTree node)
node
- a method callpublic static @Nullable ExecutableElement elementFromUse(NewClassTree node)
node
- a constructor invocationconstructor(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)
@EnsuresNonNullIf(result=true, expression="elementFromUse(#1)") @Pure public static boolean isUseOfElement(ExpressionTree node)
node
- the ExpressionTree to testpublic static boolean hasSyntheticArgument(NewClassTree tree)
tree
has a synthetic argument.
For some anonymous classes with an explicit enclosing expression, javac creates a synthetic argument to the constructor that is the enclosing expression of the NewClassTree. Suppose a programmer writes:
class Outer {
class Inner { }
void method() {
this.new Inner(){};
}
}
Java 9 javac creates the following synthetic tree for this.new Inner(){}
:
new Inner(this) {
(.Outer x0) {
x0.super();
}
}
Java 11 javac creates a different tree without the synthetic argument for this.new
Inner(){}
:
this.new Inner() {
(.Outer x0) {
x0.super();
}
}
tree
- a new class treetree
has a synthetic argumentpublic static Name methodName(MethodInvocationTree node)
public static boolean containsThisConstructorInvocation(MethodTree node)
public static Tree firstStatement(Tree tree)
tree
- any kind of treepublic static boolean hasExplicitConstructor(ClassTree node)
node
- a class treepublic static boolean isDiamondTree(Tree tree)
TreeInfo.isDiamond(JCTree)
public static boolean isStringConcatenation(Tree tree)
String
concatenation operation.public static boolean isStringCompoundConcatenation(CompoundAssignmentTree tree)
public static boolean isCompileTimeString(ExpressionTree node)
A tree is a constant-time expression if it is:
public static @Nullable ExpressionTree getReceiverTree(ExpressionTree expression)
expression
- a field access or a method invocationpublic static Set<Tree.Kind> classTreeKinds()
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 boolean isMethodInvocation(Tree methodTree, List<ExecutableElement> methods, ProcessingEnvironment processingEnv)
public static ExecutableElement getMethod(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env)
getMethod(String, String, ProcessingEnvironment, String...)
.typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environmentpublic static List<ExecutableElement> getMethods(@FullyQualifiedName String typeName, String methodName, int params, ProcessingEnvironment env)
typeName
- the class that contains the methodmethodName
- the name of the methodparams
- the number of formal parametersenv
- the processing environmentpublic static ExecutableElement getMethod(@FullyQualifiedName String typeName, String methodName, ProcessingEnvironment env, String... paramTypes)
typeName
- the class that contains the methodmethodName
- the name of the methodenv
- the processing environmentparamTypes
- the method's formal parameter typespublic static 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.true
if and only if tree
can have a type annotationpublic static boolean isSpecificFieldAccess(Tree tree, VariableElement var)
tree
represents a field access of the given
VariableElement
.public static VariableElement getField(@FullyQualifiedName 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)
tree
- the Tree to testpublic static boolean isEnumSuper(MethodInvocationTree node)
Enum
constructor.node
- the method invocation to checkEnum
constructorpublic static boolean isTypeDeclaration(Tree node)
node
- the Tree to testpublic 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 List<AnnotationMirror> annotationsFromTypeAnnotationTrees(List<? extends AnnotationTree> annoTreess)
annoTreess
- list of annotation trees to convert to annotation mirrorspublic static AnnotationMirror annotationFromAnnotationTree(AnnotationTree tree)
tree
- annotation tree to convert to an annotation mirrorpublic static List<? extends AnnotationMirror> annotationsFromTree(AnnotatedTypeTree tree)
tree
- annotated type tree to convertpublic static List<? extends AnnotationMirror> annotationsFromTree(TypeParameterTree tree)
tree
- type parameter tree to convertpublic static List<? extends AnnotationMirror> annotationsFromArrayCreation(NewArrayTree tree, int level)
tree
- new array treepublic static boolean isLocalVariable(Tree tree)
public static TypeMirror typeOf(Tree tree)
tree
. To obtain tree
's
AnnotatedTypeMirror, call AnnotatedTypeFactory.getAnnotatedType()
.tree
public static com.sun.tools.javac.code.Symbol findFunction(Tree tree, ProcessingEnvironment env)
tree
- lambda or member reference treeenv
- ProcessingEnvironmentpublic static boolean isImplicitlyTypedLambda(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.public static boolean isExprConstTrue(ExpressionTree node)
ExpressionTree
has the constant value true, according
to the compiler logic.node
- the expression to be checkednode
has the constant value true.public static String toStringOneLine(Tree tree)
tree
- a treepublic static String toStringTruncated(Tree tree, int length)
toStringOneLine(com.sun.source.tree.Tree)
if it is no more than length
characters, or
toStringOneLine(com.sun.source.tree.Tree)
quoted and truncated.tree
- a treelength
- the maximum length for the result; must be at least 6length
characters longpublic static boolean isWideningBinary(BinaryTree node)
node
- a binary treepublic static List<? extends AnnotationTree> getExplicitAnnotationTrees(List<? extends AnnotationTree> annoTrees, Tree typeTree)
annoTrees
- annotations written before a variable/method declaration; null if this type
is not from such a locationtypeTree
- the type whose annotations to returnpublic static LiteralTree getDefaultValueTree(TypeMirror typeMirror, ProcessingEnvironment processingEnv)
typeMirror
- a typeprocessingEnv
- the processing environmenttype
's default valuepublic static LiteralTree createLiteral(com.sun.tools.javac.code.TypeTag typeTag, @Nullable Object value, TypeMirror typeMirror, ProcessingEnvironment processingEnv)
typeTag
- the literal's type tagvalue
- a wrapped primitive, null, or a StringtypeMirror
- the typeMirror for the literalprocessingEnv
- the processing environment