Class TreePathUtil


  • public final class TreePathUtil
    extends java.lang.Object
    Utility methods for obtaining or analyzing a javac TreePath.
    See Also:
    TreeUtils
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static @Nullable com.sun.source.tree.ClassTree enclosingClass​(com.sun.source.util.TreePath path)
      Gets the enclosing class of the tree node defined by the given TreePath.
      static @Nullable com.sun.source.util.TreePath enclosingDeclarationPath​(com.sun.source.util.TreePath path)
      Gets the path to nearest enclosing declaration (class, method, or variable) of the tree node defined by the given TreePath.
      static @Nullable com.sun.source.tree.MethodTree enclosingMethod​(com.sun.source.util.TreePath path)
      Gets the enclosing method of the tree node defined by the given TreePath.
      static @Nullable com.sun.source.tree.Tree enclosingMethodOrLambda​(com.sun.source.util.TreePath path)
      Gets the enclosing method or lambda expression of the tree node defined by the given TreePath.
      static org.plumelib.util.IPair<com.sun.source.tree.Tree,​com.sun.source.tree.Tree> enclosingNonParen​(com.sun.source.util.TreePath path)
      Gets the first (innermost) enclosing tree in path, that is not a parenthesis.
      static <T extends com.sun.source.tree.Tree>
      @Nullable T
      enclosingOfClass​(com.sun.source.util.TreePath path, java.lang.Class<T> treeClass)
      Gets the first (innermost) enclosing tree in path, of the given class.
      static @Nullable com.sun.source.tree.Tree enclosingOfKind​(com.sun.source.util.TreePath path, com.sun.source.tree.Tree.Kind kind)
      Gets the first (innermost) enclosing tree in path, of the given kind.
      static @Nullable 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 (innermost) enclosing tree in path, with any one of the given kinds.
      static @Nullable com.sun.source.tree.BlockTree enclosingTopLevelBlock​(com.sun.source.util.TreePath path)
      Returns the top-level block that encloses the given path, or null if none does.
      static @Nullable 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 @Nullable com.sun.source.tree.Tree getAssignmentContext​(com.sun.source.util.TreePath treePath)
      Returns the "assignment context" for the leaf of treePath, which is often the leaf of the parent of treePath.
      static boolean inConstructor​(com.sun.source.util.TreePath path)
      Returns true if the tree is in a constructor or an initializer block.
      static boolean isTopLevelAssignmentInInitializerBlock​(com.sun.source.util.TreePath path)
      Returns true if the path is to a top-level (not within a loop) assignment within an initializer block.
      static boolean isTreeInStaticScope​(com.sun.source.util.TreePath path)
      Returns true if the leaf of the tree path is in a static scope.
      static java.lang.String leafToStringTruncated​(@Nullable com.sun.source.util.TreePath path, int length)
      Returns a string representation of the leaf of the given path, using TreeUtils.toStringTruncated(com.sun.source.tree.Tree, int).
      static @Nullable com.sun.source.util.TreePath pathTillClass​(com.sun.source.util.TreePath path)
      Gets path to the first (innermost) enclosing class tree, where class is defined by the TreeUtils.classTreeKinds() method.
      static @Nullable com.sun.source.util.TreePath pathTillMethod​(com.sun.source.util.TreePath path)
      Gets path to the first (innermost) enclosing method tree.
      static @Nullable com.sun.source.util.TreePath pathTillOfKind​(com.sun.source.util.TreePath path, com.sun.source.tree.Tree.Kind kind)
      Gets path to the first (innermost) enclosing tree of the given kind.
      static @Nullable 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 (innermost) enclosing tree with any one of the given kinds.
      static java.lang.String toString​(com.sun.source.util.TreePath path)
      Return a printed representation of a TreePath.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • pathTillOfKind

        public static @Nullable com.sun.source.util.TreePath pathTillOfKind​(com.sun.source.util.TreePath path,
                                                                            com.sun.source.tree.Tree.Kind kind)
        Gets path to the first (innermost) enclosing tree of the given kind. May return path itself.
        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, null otherwise
      • pathTillOfKind

        public static @Nullable 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 (innermost) enclosing tree with any one of the given kinds. May return path itself.
        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, null otherwise
      • pathTillClass

        public static @Nullable com.sun.source.util.TreePath pathTillClass​(com.sun.source.util.TreePath path)
        Gets path to the first (innermost) enclosing class tree, where class is defined by the TreeUtils.classTreeKinds() method. May return path itself.
        Parameters:
        path - the path defining the tree node
        Returns:
        the path to the enclosing class tree, null otherwise
      • pathTillMethod

        public static @Nullable com.sun.source.util.TreePath pathTillMethod​(com.sun.source.util.TreePath path)
        Gets path to the first (innermost) enclosing method tree. May return path itself.
        Parameters:
        path - the path defining the tree node
        Returns:
        the path to the enclosing class tree, null otherwise
      • enclosingOfKind

        public static @Nullable com.sun.source.tree.Tree enclosingOfKind​(com.sun.source.util.TreePath path,
                                                                         com.sun.source.tree.Tree.Kind kind)
        Gets the first (innermost) enclosing tree in path, of the given kind. May return the leaf of path itself.
        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, null otherwise
      • enclosingOfKind

        public static @Nullable 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 (innermost) enclosing tree in path, with any one of the given kinds. May return the leaf of path itself.
        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, null otherwise
      • enclosingOfClass

        public static <T extends com.sun.source.tree.Tree> @Nullable T enclosingOfClass​(com.sun.source.util.TreePath path,
                                                                                        java.lang.Class<T> treeClass)
        Gets the first (innermost) enclosing tree in path, of the given class. May return the leaf of path itself.
        Type Parameters:
        T - the type of treeClass
        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, null otherwise
      • enclosingDeclarationPath

        public static @Nullable com.sun.source.util.TreePath enclosingDeclarationPath​(com.sun.source.util.TreePath path)
        Gets the path to nearest enclosing declaration (class, method, or variable) of the tree node defined by the given TreePath. May return the leaf of path itself.
        Parameters:
        path - the path defining the tree node
        Returns:
        path to the nearest enclosing class/method/variable in the path, or null if one does not exist
      • enclosingClass

        public static @Nullable com.sun.source.tree.ClassTree enclosingClass​(com.sun.source.util.TreePath path)
        Gets the enclosing class of the tree node defined by the given TreePath. It returns a Tree, from which checkers.types.AnnotatedTypeMirror or Element can be obtained. May return the leaf of path itself.
        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 @Nullable 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. May return the leaf of path itself.
        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 @Nullable com.sun.source.tree.MethodTree enclosingMethod​(com.sun.source.util.TreePath path)
        Gets the enclosing method of the tree node defined by the given TreePath. It returns a Tree, from which an checkers.types.AnnotatedTypeMirror or Element can be obtained. May return the leaf of path itself.

        Also see AnnotatedTypeFactory#getEnclosingMethod and AnnotatedTypeFactory#getEnclosingClassOrMethod, which do not require a TreePath.

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

        public static @Nullable com.sun.source.tree.Tree enclosingMethodOrLambda​(com.sun.source.util.TreePath path)
        Gets the enclosing method or lambda expression of the tree node defined by the given TreePath. It returns a Tree, from which an checkers.types.AnnotatedTypeMirror or Element can be obtained. May return the leaf of path itself.
        Parameters:
        path - the path defining the tree node
        Returns:
        the enclosing method or lambda as given by the path, or null if one does not exist
      • enclosingTopLevelBlock

        public static @Nullable com.sun.source.tree.BlockTree enclosingTopLevelBlock​(com.sun.source.util.TreePath path)
        Returns the top-level block that encloses the given path, or null if none does. Never returns the leaf of path itself.
        Parameters:
        path - a path
        Returns:
        the top-level block that encloses the given path, or null if none does
      • enclosingNonParen

        public static org.plumelib.util.IPair<com.sun.source.tree.Tree,​com.sun.source.tree.Tree> enclosingNonParen​(com.sun.source.util.TreePath path)
        Gets the first (innermost) enclosing tree in path, that is not a parenthesis. Never returns the leaf of path itself.
        Parameters:
        path - the path defining the tree node
        Returns:
        a pair of a non-parenthesis tree that contains the argument, and its child that is the argument or is a parenthesized version of it
      • getAssignmentContext

        public static @Nullable com.sun.source.tree.Tree getAssignmentContext​(com.sun.source.util.TreePath treePath)
        Returns the "assignment context" for the leaf of treePath, which is often the leaf of the parent of treePath. (Does not handle pseudo-assignment of an argument to a parameter or a receiver expression to a receiver.) This is not the same as org.checkerframework.dataflow.cfg.node.AssignmentContext, which represents the left-hand side rather than the assignment itself.

        The assignment context for treePath is the leaf of its parent, if that leaf is one of the following trees:

        • AssignmentTree
        • CompoundAssignmentTree
        • MethodInvocationTree
        • NewArrayTree
        • NewClassTree
        • ReturnTree
        • VariableTree
        If the parent is a ConditionalExpressionTree we need to distinguish two cases: If the leaf is either the then or else branch of the ConditionalExpressionTree, then recurse on the parent. If the leaf is the condition of the ConditionalExpressionTree, then return null to not consider this assignment context.

        If the leaf is a ParenthesizedTree, then recurse on the parent.

        Otherwise, null is returned.

        Parameters:
        treePath - a path
        Returns:
        the assignment context as described, null otherwise
      • inConstructor

        public static boolean inConstructor​(com.sun.source.util.TreePath path)
        Returns true if the tree is in a constructor or an initializer block.
        Parameters:
        path - the path to test
        Returns:
        true if the path is in a constructor or an initializer block
      • isTreeInStaticScope

        public static boolean isTreeInStaticScope​(com.sun.source.util.TreePath path)
        Returns true if the leaf of the tree path is in a static scope.
        Parameters:
        path - a TreePath whose leaf may or may not be in static scope
        Returns:
        true if the leaf of the tree path is in a static scope
      • isTopLevelAssignmentInInitializerBlock

        public static boolean isTopLevelAssignmentInInitializerBlock​(com.sun.source.util.TreePath path)
        Returns true if the path is to a top-level (not within a loop) assignment within an initializer block. The initializer block might be instance or static. Will return true for a re-assignment even if there is another initialization (within this initializer block, another initializer block, a constructor, or the variable declaration).
        Parameters:
        path - the path to test
        Returns:
        true if the path is to an initialization within an initializer block
      • toString

        public static java.lang.String toString​(com.sun.source.util.TreePath path)
        Return a printed representation of a TreePath.
        Parameters:
        path - a TreePath
        Returns:
        a printed representation of the given TreePath
      • leafToStringTruncated

        public static java.lang.String leafToStringTruncated​(@Nullable com.sun.source.util.TreePath path,
                                                             int length)
        Returns a string representation of the leaf of the given path, using TreeUtils.toStringTruncated(com.sun.source.tree.Tree, int).
        Parameters:
        path - a path
        length - the maximum length for the result; must be at least 6
        Returns:
        a one-line string representation of the leaf of the given path that is no longer than length characters long