checkers.util
Class TreeUtils

java.lang.Object
  extended by checkers.util.TreeUtils

@DefaultQualifier(value="checkers.nullness.quals.NonNull")
public final class TreeUtils
extends Object

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


Method Summary
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(IdentifierTree node)
          Gets the element for the declaration corresponding to this identifier.
static Element elementFromUse(MemberSelectTree node)
          Gets the element for the declaration corresponding to this member access.
static ExecutableElement elementFromUse(MethodInvocationTree node)
          Gets the element for the method corresponding to this invocation.
static ClassTree enclosingClass(TreePath path)
          Gets the enclosing method of the tree node defined by the given {@link TreePath}.
static MethodTree enclosingMethod(TreePath path)
          Gets the enclosing method of the tree node defined by the given {@link TreePath}.
static
<T extends Tree>
T
enclosingOfClass(TreePath path, Class<T> treeClass)
          Gets the first enclosing tree in path, of the specified class
static Tree enclosingOfKind(TreePath path, Tree.Kind kind)
          Gets the first enclosing tree in path, of the specified kind.
static VariableTree enclosingVariable(TreePath path)
          Gets the enclosing variable of a tree node defined by the given TreePath.
static Tree getAssignmentContext(TreePath treePath)
          Returns the tree with the assignment context for the treePath leaf node.
static boolean isConstructor(MethodTree tree)
          Checks if the provided method is a constructor method or no.
static boolean isSelfAccess(ExpressionTree tree)
          Returns true if the tree is a tree that 'looks like' either an access of a field or an invokation of a method that are owned by the same accessing instance.
static boolean isSuperCall(MethodInvocationTree tree)
          Checks if the method invocation is a call to super
static Name methodName(MethodInvocationTree node)
           
static ExpressionTree skipParens(ExpressionTree tree)
          If the given tree is a parenthesized tree, it returns the enclosed non-parenthesized tree.
static Tree skipParens(Tree tree)
          If the given tree is a parenthesized tree, it returns the enclosed non-parenthesized tree.
 
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(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

isSelfAccess

public static boolean isSelfAccess(ExpressionTree tree)
Returns true if the tree is a tree that 'looks like' either an access of a field or an invokation 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

isSuperCall

public static boolean isSuperCall(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

enclosingOfKind

public static Tree enclosingOfKind(TreePath path,
                                   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

enclosingOfClass

public static <T extends Tree> T enclosingOfClass(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 ClassTree enclosingClass(TreePath path)
Gets the enclosing method of the tree node defined by the given {@link TreePath}. It returns a Tree, from which AnnotatedTypeMirror or 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 VariableTree enclosingVariable(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 MethodTree enclosingMethod(TreePath path)
Gets the enclosing method of the tree node defined by the given {@link TreePath}. It returns a Tree, from which an AnnotatedTypeMirror or 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

skipParens

public static Tree skipParens(Tree tree)
If the given tree is a parenthesized tree, it returns the enclosed non-parenthesized tree. Otherwise, it returns the same tree.

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

skipParens

public static ExpressionTree skipParens(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 Tree getAssignmentContext(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(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(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(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 ExecutableElement elementFromUse(MethodInvocationTree node)
Gets the element for the method corresponding to this invocation. To get the element for a method declaration, use Trees.getElement(TreePath) instead.

Parameters:
node - the method invocation
Returns:
the element for the method that corresponds to this invocation

elementFromUse

public static final Element elementFromUse(IdentifierTree node)
Gets the element for the declaration corresponding to this identifier. To get the element for a declaration, use Trees.getElement(TreePath) instead.

Parameters:
node - the identifier
Returns:
the element for the declaration that corresponds to this identifier

elementFromUse

public static final Element elementFromUse(MemberSelectTree node)
Gets the element for the declaration corresponding to this member access. To get the element for a declaration, use Trees.getElement(TreePath) instead.

Parameters:
node - the member access
Returns:
the element for the declaration that corresponds to this member access

methodName

public static final Name methodName(MethodInvocationTree node)
Returns:
the name of the invoked method

containsThisConstructorInvocation

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