org.stjs.generator.javac
Class TreeUtils

java.lang.Object
  extended by org.stjs.generator.javac.TreeUtils

public final class TreeUtils
extends Object

A utility class made for helping to analyze a given Tree.


Method Summary
static boolean canHaveTypeAnnotation(com.sun.source.tree.Tree tree)
           
static boolean containsThisConstructorInvocation(com.sun.source.tree.MethodTree node)
           
static TypeElement elementFromDeclaration(com.sun.source.tree.ClassTree node)
          Gets the element for a class corresponding to a declaration.
static ExecutableElement elementFromDeclaration(com.sun.source.tree.MethodTree node)
          Gets the element for a method corresponding to a declaration.
static VariableElement elementFromDeclaration(com.sun.source.tree.VariableTree node)
          Gets the element for a variable corresponding to its declaration.
static Element elementFromUse(com.sun.source.tree.ExpressionTree node)
          Gets the element for the declaration corresponding to this use of an element.
static ExecutableElement elementFromUse(com.sun.source.tree.MethodInvocationTree node)
           
static ExecutableElement elementFromUse(com.sun.source.tree.NewClassTree node)
           
static com.sun.source.tree.ClassTree enclosingClass(com.sun.source.util.TreePath path)
          Gets the enclosing class of the tree node defined by the given {@link TreePath} .
static com.sun.source.tree.MethodTree enclosingMethod(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>
T
enclosingOfClass(com.sun.source.util.TreePath path, 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, 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
<T extends com.sun.source.tree.Tree>
com.sun.source.util.TreePath
enclosingPathOfType(com.sun.source.util.TreePath path, Class<T> clz)
          Gets the first enclosing tree in path, with any one of the specified kinds.
static 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 VariableElement getField(String typeName, String fieldName, ProcessingEnvironment env)
          Returns the VariableElement for a field declaration.
static String getFieldName(com.sun.source.tree.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(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 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 f obj .
static boolean isMethodAccess(com.sun.source.tree.Tree tree)
          Determine whether tree refers to a method element, such as m(...)
static boolean isMethodInvocation(com.sun.source.tree.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(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, 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?
static boolean isUseOfElement(com.sun.source.tree.ExpressionTree node)
          Determine whether the given ExpressionTree has an underlying element.
static Name methodName(com.sun.source.tree.MethodInvocationTree node)
           
static com.sun.source.util.TreePath pathTillOfKind(com.sun.source.util.TreePath path, 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 Set<com.sun.source.tree.Tree.Kind> typeTreeKinds()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isConstructor

public static boolean isConstructor(com.sun.source.tree.MethodTree tree)
Checks if the provided method is a constructor method or no.

Parameters:
tree - a tree defining the method
Returns:
true iff tree describes a constructor

isSuperCall

public static boolean isSuperCall(com.sun.source.tree.MethodInvocationTree tree)
Checks if the method invocation is a call to super.

Parameters:
tree - a tree defining a method invocation
Returns:
true iff tree describes a call to super

isSelfAccess

public 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. 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.

Parameters:
tree - expression tree representing an access to object member
Returns:
true iff the member is a member of this instance

enclosingOfKind

public 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.

Parameters:
path - the path defining the tree node
kind - the kind of the desired tree
Returns:
the enclosing tree of the given type as given by the path

enclosingOfKind

public static com.sun.source.tree.Tree enclosingOfKind(com.sun.source.util.TreePath path,
                                                       Set<com.sun.source.tree.Tree.Kind> kinds)
Gets the first enclosing tree in path, with any one of the specified kinds.

Parameters:
path - the path defining the tree node
kinds - the set of kinds of the desired tree
Returns:
the enclosing tree of the given type as given by the path

enclosingPathOfType

public static <T extends com.sun.source.tree.Tree> com.sun.source.util.TreePath enclosingPathOfType(com.sun.source.util.TreePath path,
                                                                                                    Class<T> clz)
Gets the first enclosing tree in path, with any one of the specified kinds.

Parameters:
path - the path defining the tree node
kinds - the set of kinds of the desired tree
Returns:
the enclosing tree of the given type as given by the path

pathTillOfKind

public 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.

Parameters:
path - the path defining the tree node
kind - the kind of the desired tree
Returns:
the path to the enclosing tree of the given type

pathTillOfKind

public static com.sun.source.util.TreePath pathTillOfKind(com.sun.source.util.TreePath path,
                                                          Set<com.sun.source.tree.Tree.Kind> kinds)
Gets path to the the first enclosing tree with any one of the specified kinds.

Parameters:
path - the path defining the tree node
kinds - the set of kinds of the desired tree
Returns:
the path to the enclosing tree of the given type

enclosingOfClass

public static <T extends com.sun.source.tree.Tree> T enclosingOfClass(com.sun.source.util.TreePath path,
                                                                      Class<T> treeClass)
Gets the first enclosing tree in path, of the specified class

Parameters:
path - the path defining the tree node
treeClass - the class of the desired tree
Returns:
the enclosing tree of the given type as given by the path

enclosingClass

public static com.sun.source.tree.ClassTree enclosingClass(com.sun.source.util.TreePath path)
Gets the enclosing class of the tree node defined by the given {@link TreePath} . It returns a {@link Tree}, from which {@code checkers.types.AnnotatedTypeMirror} or {@link Element} can be obtained.

Parameters:
path - the path defining the tree node
Returns:
the enclosing class (or interface) as given by the path, or null if one does not exist.

enclosingVariable

public 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.

Parameters:
path - the path defining the tree node
Returns:
the enclosing variable as given by the path, or null if one does not exist

enclosingMethod

public static com.sun.source.tree.MethodTree enclosingMethod(com.sun.source.util.TreePath path)
Gets the enclosing method of the tree node defined by the given {@link TreePath} . It returns a {@link Tree}, from which an {@code checkers.types.AnnotatedTypeMirror} or {@link Element} can be obtained.

Parameters:
path - the path defining the tree node
Returns:
the enclosing method as given by the path, or null if one does not exist

enclosingTopLevelBlock

public static com.sun.source.tree.BlockTree enclosingTopLevelBlock(com.sun.source.util.TreePath path)

skipParens

public 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. Otherwise, it returns the same tree.

Parameters:
tree - an expression tree
Returns:
the outermost non-parenthesized tree enclosed by the given tree

getAssignmentContext

public 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. The assignment context for the treepath is the most enclosing tree of type:

Parameters:
treePath -
Returns:
the assignment context as described.

elementFromDeclaration

public static final TypeElement elementFromDeclaration(com.sun.source.tree.ClassTree node)
Gets the element for a class corresponding to a declaration.

Parameters:
node -
Returns:
the element for the given class

elementFromDeclaration

public static final ExecutableElement elementFromDeclaration(com.sun.source.tree.MethodTree node)
Gets the element for a method corresponding to a declaration.

Parameters:
node -
Returns:
the element for the given method

elementFromDeclaration

public static final VariableElement elementFromDeclaration(com.sun.source.tree.VariableTree node)
Gets the element for a variable corresponding to its declaration.

Parameters:
node -
Returns:
the element for the given variable

elementFromUse

public static final Element elementFromUse(com.sun.source.tree.ExpressionTree node)
Gets the element for the declaration corresponding to this use of an element. To get the element for a declaration, use Trees.getElement(TreePath) instead. TODO: remove this method, as it really doesn't do anything.

Parameters:
node - the tree corresponding to a use of an element
Returns:
the element for the corresponding declaration

elementFromUse

public static final ExecutableElement elementFromUse(com.sun.source.tree.MethodInvocationTree node)

elementFromUse

public static final ExecutableElement elementFromUse(com.sun.source.tree.NewClassTree node)

isUseOfElement

public static final boolean isUseOfElement(com.sun.source.tree.ExpressionTree node)
Determine whether the given ExpressionTree has an underlying element.

Parameters:
node - the ExpressionTree to test
Returns:
whether the tree refers to an identifier, member select, or method invocation.

methodName

public static final Name methodName(com.sun.source.tree.MethodInvocationTree node)
Returns:
the name of the invoked method

containsThisConstructorInvocation

public static final boolean containsThisConstructorInvocation(com.sun.source.tree.MethodTree node)
Returns:
true if the first statement in the body is a self constructor invocation within a constructor

firstStatement

public static final com.sun.source.tree.Tree firstStatement(com.sun.source.tree.Tree tree)

hasExplicitConstructor

public static boolean hasExplicitConstructor(com.sun.source.tree.ClassTree node)
Determine whether the given class contains an explicit constructor.

Parameters:
node - A class tree.
Returns:
True, iff there is an explicit constructor.

isDiamondTree

public static final boolean isDiamondTree(com.sun.source.tree.Tree tree)
Returns true if the tree is of a diamond type


isStringConcatenation

public static final boolean isStringConcatenation(com.sun.source.tree.Tree tree)
Returns true if the tree represents a String concatenation operation


isStringCompoundConcatenation

public static final boolean isStringCompoundConcatenation(com.sun.source.tree.CompoundAssignmentTree tree)
Returns true if the compound assignment tree is a string concatenation


isCompileTimeString

public static boolean isCompileTimeString(com.sun.source.tree.ExpressionTree node)
Returns true if the node is a constant-time expression. A tree is a constant-time expression if it is:
  1. a literal tree
  2. a reference to a final variable initialized with a compile time constant
  3. a String concatenation of two compile time constants


getReceiverTree

public 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


typeTreeKinds

public static Set<com.sun.source.tree.Tree.Kind> typeTreeKinds()

isTypeTree

public static boolean isTypeTree(com.sun.source.tree.Tree tree)
Is the given tree a type instantiation? TODO: this is an under-approximation: e.g. an identifier could be either a type use or an expression. How can we distinguish.

Parameters:
tree - the tree to test
Returns:
true, iff the given tree is a type

isMethodInvocation

public static boolean isMethodInvocation(com.sun.source.tree.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.


getMethod

public 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. TODO: to precisely resolve method overloading, we should use parameter types and not just the number of parameters!


isFieldAccess

public static boolean isFieldAccess(com.sun.source.tree.Tree tree)
Determine whether tree is a field access expressions, such as
   f
   obj . f
 

Returns:
true iff if tree is a field access expression (implicit or explicit).

getFieldName

public static String getFieldName(com.sun.source.tree.Tree tree)
Compute the name of the field that the field access tree accesses. Requires tree to be a field access, as determined by isFieldAccess.

Returns:
The name of the field accessed by tree.

isMethodAccess

public static boolean isMethodAccess(com.sun.source.tree.Tree tree)
Determine whether tree refers to a method element, such as
   m(...)
   obj . m(...)
 

Returns:
true iff if tree is a method access expression (implicit or explicit).

getMethodName

public static String getMethodName(com.sun.source.tree.Tree tree)
Compute the name of the method that the method access tree accesses. Requires tree to be a method access, as determined by isMethodAccess.

Returns:
The name of the method accessed by tree.

canHaveTypeAnnotation

public static boolean canHaveTypeAnnotation(com.sun.source.tree.Tree tree)
Returns:
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?

isSpecificFieldAccess

public static boolean isSpecificFieldAccess(com.sun.source.tree.Tree tree,
                                            VariableElement var)
Returns true if and only if the given tree represents a field access of the given VariableElement .


getField

public static VariableElement getField(String typeName,
                                       String fieldName,
                                       ProcessingEnvironment env)
Returns the VariableElement for a field declaration.

Parameters:
typeName - the class where the field is declared.
fieldName - the name of the field.
env - the processing environment.
Returns:
the VariableElement for typeName.fieldName

isExpressionTree

public static boolean isExpressionTree(com.sun.source.tree.Tree tree)
Determine whether the given tree represents an ExpressionTree. TODO: is there a nicer way than an instanceof?

Parameters:
tree - the Tree to test.
Returns:
whether the tree is an ExpressionTree

isEnumSuper

public static boolean isEnumSuper(com.sun.source.tree.MethodInvocationTree node)
Parameters:
node - the method invocation to check
Returns:
true if this is a super call to the Enum constructor


Copyright © 2014. All Rights Reserved.