Package org.checkerframework.javacutil
Class TreePathUtil
java.lang.Object
org.checkerframework.javacutil.TreePathUtil
Utility methods for obtaining or analyzing a javac 
TreePath.- See Also:
- 
Method SummaryModifier and TypeMethodDescriptionenclosingClass(TreePath path) Gets the enclosing class of the tree node defined by the givenTreePath.Gets the path to nearest enclosing declaration (class, method, or variable) of the tree node defined by the givenTreePath.static @Nullable MethodTreeenclosingMethod(TreePath path) Gets the enclosing method of the tree node defined by the givenTreePath.Gets the enclosing method or lambda expression of the tree node defined by the givenTreePath.enclosingNonParen(TreePath path) Gets the first (innermost) enclosing tree in path, that is not a parenthesis.enclosingOfClass(TreePath path, Class<T> treeClass) Gets the first (innermost) enclosing tree in path, of the given class.enclosingOfKind(TreePath path, Tree.Kind kind) Gets the first (innermost) enclosing tree in path, of the given kind.enclosingOfKind(TreePath path, Set<Tree.Kind> kinds) Gets the first (innermost) enclosing tree in path, with any one of the given kinds.Returns the top-level block that encloses the given path, or null if none does.static @Nullable VariableTreeenclosingVariable(TreePath path) Gets the enclosing variable of a tree node defined by the givenTreePath.Retrieves the nearest enclosing method or class element for the specified path in the AST.getContextForPolyExpression(TreePath treePath) Returns the tree representing the context for the poly expression which is the leaf oftreePath.static booleaninConstructor(TreePath path) Returns true if the tree is in a constructor or an initializer block.static booleanReturns true if the path is to a top-level (not within a loop) assignment within an initializer block.static booleanisTreeInStaticScope(TreePath path) Returns true if the leaf of the tree path is in a static scope.static StringleafToStringTruncated(@Nullable TreePath path, int length) Returns a string representation of the leaf of the given path, usingTreeUtils.toStringTruncated(com.sun.source.tree.Tree, int).pathTillClass(TreePath path) Gets path to the first (innermost) enclosing class tree, where class is defined by theTreeUtils.classTreeKinds()method.pathTillMethod(TreePath path) Gets path to the first (innermost) enclosing method tree.pathTillOfKind(TreePath path, Tree.Kind kind) Gets path to the first (innermost) enclosing tree of the given kind.pathTillOfKind(TreePath path, Set<Tree.Kind> kinds) Gets path to the first (innermost) enclosing tree with any one of the given kinds.static StringReturn a printed representation of a TreePath.
- 
Method Details- 
pathTillOfKindGets path to the first (innermost) enclosing tree of the given kind. May returnpathitself.- 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, nullotherwise
 
- 
pathTillOfKindGets path to the first (innermost) enclosing tree with any one of the given kinds. May returnpathitself.- 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, nullotherwise
 
- 
pathTillClassGets path to the first (innermost) enclosing class tree, where class is defined by theTreeUtils.classTreeKinds()method. May returnpathitself.- Parameters:
- path- the path defining the tree node
- Returns:
- the path to the enclosing class tree, nullotherwise
 
- 
pathTillMethodGets path to the first (innermost) enclosing method tree. May returnpathitself.- Parameters:
- path- the path defining the tree node
- Returns:
- the path to the enclosing class tree, nullotherwise
 
- 
enclosingOfKindGets the first (innermost) enclosing tree in path, of the given kind. May return the leaf ofpathitself.- 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, nullotherwise
 
- 
enclosingOfKindGets the first (innermost) enclosing tree in path, with any one of the given kinds. May return the leaf ofpathitself.- 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, nullotherwise
 
- 
enclosingOfClassGets the first (innermost) enclosing tree in path, of the given class. May return the leaf ofpathitself.- 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, nullotherwise
 
- 
enclosingDeclarationPathGets the path to nearest enclosing declaration (class, method, or variable) of the tree node defined by the givenTreePath. May return the leaf ofpathitself.- Parameters:
- path- the path defining the tree node
- Returns:
- path to the nearest enclosing class/method/variable in the path, or nullif one does not exist
 
- 
enclosingClassGets the enclosing class of the tree node defined by the givenTreePath. It returns aTree, from whichcheckers.types.AnnotatedTypeMirrororElementcan be obtained. May return the leaf ofpathitself.- Parameters:
- path- the path defining the tree node
- Returns:
- the enclosing class (or interface) as given by the path, or nullif one does not exist
 
- 
enclosingVariableGets the enclosing variable of a tree node defined by the givenTreePath. May return the leaf ofpathitself.- Parameters:
- path- the path defining the tree node
- Returns:
- the enclosing variable as given by the path, or nullif one does not exist
 
- 
enclosingMethodGets the enclosing method of the tree node defined by the givenTreePath. It returns aTree, from which ancheckers.types.AnnotatedTypeMirrororElementcan be obtained. May return the leaf ofpathitself.Also see AnnotatedTypeFactory#getEnclosingMethodandAnnotatedTypeFactory#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 nullif one does not exist
 
- 
enclosingMethodOrLambdaGets the enclosing method or lambda expression of the tree node defined by the givenTreePath. It returns aTree, from which ancheckers.types.AnnotatedTypeMirrororElementcan be obtained. May return the leaf ofpathitself.- Parameters:
- path- the path defining the tree node
- Returns:
- the enclosing method or lambda as given by the path, or nullif one does not exist
 
- 
enclosingTopLevelBlockReturns the top-level block that encloses the given path, or null if none does. Never returns the leaf ofpathitself.- Parameters:
- path- a path
- Returns:
- the top-level block that encloses the given path, or null if none does
 
- 
enclosingNonParenGets the first (innermost) enclosing tree in path, that is not a parenthesis. Never returns the leaf ofpathitself.- 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
 
- 
getContextForPolyExpressionReturns the tree representing the context for the poly expression which is the leaf oftreePath. The context then can be used to find the target type of the poly expression. Returns null if the leaf oftreePathis not a poly expression.- Parameters:
- treePath- a path. If the leaf of the path is a poly expression, then its context is returned.
- Returns:
- the tree representing the context for the poly expression which is the leaf of treePath; or null if the leaf is not a poly expression
 
- 
inConstructorReturns 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
 
- 
isTreeInStaticScopeReturns 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
 
- 
isTopLevelAssignmentInInitializerBlockReturns 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
 
- 
toStringReturn a printed representation of a TreePath.- Parameters:
- path- a TreePath
- Returns:
- a printed representation of the given TreePath
 
- 
leafToStringTruncatedReturns a string representation of the leaf of the given path, usingTreeUtils.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
     lengthcharacters long
 
- 
findNearestEnclosingElementRetrieves the nearest enclosing method or class element for the specified path in the AST. This utility method prioritizes method elements over class elements. It returns the element of the closest method scope if available; otherwise, it defaults to the enclosing class scope.
 
-