public final class TreeUtilities extends Object
Modifier and Type | Field and Description |
---|---|
static Set<Tree.Kind> |
CLASS_TREE_KINDS
|
Modifier and Type | Method and Description |
---|---|
TypeMirror |
attributeTree(Tree tree,
Scope scope)
Attribute the given tree in the given context.
|
Scope |
attributeTreeTo(Tree tree,
Scope scope,
Tree to)
Attribute the given tree until the given
to tree is reached. |
CharSequence |
decodeIdentifier(CharSequence text)
Decode escapes defined in: https://openjdk.org/projects/coin, 3.1-3.9.
|
CharSequence |
encodeIdentifier(CharSequence ident)
Encode identifier using escapes defined in: https://openjdk.org/projects/coin, 3.1-3.9.
|
int[] |
findBodySpan(ClassTree clazz)
Find span of the
ClassTree 's body in the source. |
int[] |
findMethodParameterSpan(MethodTree method)
Find span of the
MethodTree.getParameters() parameter list in the source. |
int[] |
findNameSpan(BreakTree brk)
Find span of the
LabeledStatementTree.getLabel() identifier in the source. |
int[] |
findNameSpan(ClassTree clazz)
Find span of the
ClassTree.getSimpleName() identifier in the source. |
int[] |
findNameSpan(ContinueTree cont)
Find span of the
LabeledStatementTree.getLabel() identifier in the source. |
int[] |
findNameSpan(com.sun.source.doctree.DocCommentTree docTree,
com.sun.source.doctree.ReferenceTree ref)
Find span of the name in the DocTree's reference tree (see
TreeUtilities.getReferenceName(com.sun.source.util.DocTreePath)
identifier in the source. |
int[] |
findNameSpan(LabeledStatementTree lst)
Find span of the
LabeledStatementTree.getLabel() identifier in the source. |
int[] |
findNameSpan(MemberReferenceTree mst)
Find span of the
MemberReferenceTree.getName() identifier in the source. |
int[] |
findNameSpan(MemberSelectTree mst)
Find span of the
MemberSelectTree.getIdentifier() identifier in the source. |
int[] |
findNameSpan(MethodTree method)
Find span of the
MethodTree.getName() identifier in the source. |
int[] |
findNameSpan(TypeParameterTree tpt)
Find span of the
TypeParameterTree.getName() identifier in the source. |
int[] |
findNameSpan(VariableTree var)
Find span of the
VariableTree.getName() identifier in the source. |
StatementTree |
getBreakContinueTarget(TreePath breakOrContinue)
Find the target of
break or continue . |
Tree |
getBreakContinueTargetTree(TreePath breakOrContinue)
Find the target of
break or continue . |
List<Comment> |
getComments(Tree tree,
boolean preceding)
Returns list of comments attached to a given tree.
|
TreePath |
getPathElementOfKind(Set<Tree.Kind> kinds,
TreePath path)
Returns path to the deepest tree of one of the given kinds containing
the given starting tree.
|
TreePath |
getPathElementOfKind(Tree.Kind kind,
TreePath path)
Returns path to the deepest tree of the given kind containing the given
starting tree.
|
ExpressionTree |
getReferenceClass(DocTreePath path)
Find the type (the part before
# ) that is being referenced by the given ReferenceTree . |
Name |
getReferenceName(DocTreePath path)
Find the name (the name after
# ) that is being referenced by the given ReferenceTree . |
List<? extends Tree> |
getReferenceParameters(DocTreePath path)
Find the parameters that are specified in the given
ReferenceTree . |
Set<TypeMirror> |
getUncaughtExceptions(TreePath path)
Returns uncaught exceptions inside the given tree path.
|
Set<? extends VariableElement> |
getUninitializedFields(TreePath path)
Returns all uninitialized fields of the given class.
|
boolean |
hasError(Tree tree,
String... errors)
Check the tree has compile error in given errors.
|
boolean |
isAccessible(Scope scope,
Element member,
TypeMirror type)
Deprecated.
since 0.111,
Trees.isAccessible(Scope, Element, DeclaredType) should be used instead. |
boolean |
isAnnotation(ClassTree tree)
Deprecated.
since 0.67,
Tree.getKind() == Kind.ANNOTATION_TYPE should be used instead. |
boolean |
isClass(ClassTree tree)
Deprecated.
since 0.67,
Tree.getKind() == Kind.CLASS should be used instead. |
boolean |
isCompileTimeConstantExpression(TreePath expression)
Checks whether the given expression is a compile-time constant, as per JLS 15.28.
|
boolean |
isEndOfCompoundVariableDeclaration(Tree tree)
Check the tree is the end of compound declaration.
|
boolean |
isEnum(ClassTree tree)
Deprecated.
since 0.67,
Tree.getKind() == Kind.ENUM should be used instead. |
boolean |
isEnumConstant(VariableTree tree)
Checks whether given variable tree represents an enum constant.
|
boolean |
isExpressionStatement(ExpressionTree tree)
Checks whether given expression represents an expression statement.
|
boolean |
isInterface(ClassTree tree)
Deprecated.
since 0.67,
Tree.getKind() == Kind.INTERFACE should be used instead. |
boolean |
isModuleInfo(CompilationUnitTree tree)
Checks whether given compilation unit represents a module-info.
|
boolean |
isPackageInfo(CompilationUnitTree tree)
Checks whether given compilation unit represents a package-info.
|
boolean |
isPartOfCompoundVariableDeclaration(Tree tree)
Checks whether tree is part of compound variable declaration.
|
boolean |
isStaticContext(Scope scope)
Checks whether the given scope is in "static" context.
|
boolean |
isSynthetic(TreePath path)
Returns whether or not the given tree is synthetic - generated by the parser.
|
boolean |
isVarType(TreePath path)
Check the var type variable in given tree path
TreePath . |
ExpressionTree |
parseExpression(String expr,
SourcePositions[] sourcePositions)
Parses given expression.
|
StatementTree |
parseStatement(String stmt,
SourcePositions[] sourcePositions)
Parses given statement.
|
BlockTree |
parseStaticBlock(String block,
SourcePositions[] sourcePositions)
Parses given static block.
|
TypeMirror |
parseType(String expr,
TypeElement scope)
Parses given type in given context.
|
ExpressionTree |
parseVariableInitializer(String init,
SourcePositions[] sourcePositions)
Parses given variable initializer.
|
DocTreePath |
pathFor(DocTreePath path,
int pos)
Return the deepest DocTreePath at the given position.
|
DocTreePath |
pathFor(DocTreePath path,
int pos,
DocSourcePositions sourcePositions)
Return the deepest DocTreePath at the given position.
|
TreePath |
pathFor(int pos) |
DocTreePath |
pathFor(TreePath treepath,
com.sun.source.doctree.DocCommentTree doc,
int pos)
Return the deepest DocTreePath at the given position.
|
TreePath |
pathFor(TreePath path,
int pos) |
TreePath |
pathFor(TreePath path,
int pos,
SourcePositions sourcePositions) |
TypeMirror |
reattributeTree(Tree tree,
Scope scope) |
Scope |
reattributeTreeTo(Tree tree,
Scope scope,
Tree to) |
Scope |
scopeFor(int pos)
Computes
Scope for the given position. |
TokenSequence<org.netbeans.api.java.lexer.JavaTokenId> |
tokensFor(Tree tree)
Returns tokens for a given tree.
|
TokenSequence<org.netbeans.api.java.lexer.JavaTokenId> |
tokensFor(Tree tree,
SourcePositions sourcePositions)
Returns tokens for a given tree.
|
Scope |
toScopeWithDisabledAccessibilityChecks(Scope scope)
Creates
Scope capable to access all private methods and fields when
parsing and evaluating expressions. |
com.sun.source.doctree.DocTree |
translate(com.sun.source.doctree.DocTree original,
Map<? extends com.sun.source.doctree.DocTree,? extends com.sun.source.doctree.DocTree> original2Translated)
Returns new tree based on
original , such that each visited subtree
that occurs as a key in original2Translated is replaced by the corresponding
value from original2Translated . |
Tree |
translate(Tree original,
Map<? extends Tree,? extends Tree> original2Translated)
Returns new tree based on
original , such that each visited subtree
that occurs as a key in original2Translated is replaced by the corresponding
value from original2Translated . |
@Deprecated public boolean isClass(ClassTree tree)
Tree.getKind() == Kind.CLASS
should be used instead.@Deprecated public boolean isInterface(ClassTree tree)
Tree.getKind() == Kind.INTERFACE
should be used instead.@Deprecated public boolean isEnum(ClassTree tree)
Tree.getKind() == Kind.ENUM
should be used instead.public boolean isEnumConstant(VariableTree tree)
@Deprecated public boolean isAnnotation(ClassTree tree)
Tree.getKind() == Kind.ANNOTATION_TYPE
should be used instead.public boolean isPackageInfo(CompilationUnitTree tree)
public boolean isModuleInfo(CompilationUnitTree tree)
public boolean isExpressionStatement(ExpressionTree tree)
public boolean isSynthetic(TreePath path) throws NullPointerException
NullPointerException
- if the given tree is nullpublic TreePath getPathElementOfKind(Tree.Kind kind, TreePath path)
kind
- requested tree kindpath
- path to the starting treepublic TreePath getPathElementOfKind(Set<Tree.Kind> kinds, TreePath path)
kinds
- requested tree kindspath
- path to the starting treepublic List<Comment> getComments(Tree tree, boolean preceding)
tree
- for which comments should be returnedpreceding
- true if preceding comments should be returned, false if trailing comments should be returned.public TreePath pathFor(int pos)
public TreePath pathFor(TreePath path, int pos, SourcePositions sourcePositions)
public DocTreePath pathFor(TreePath treepath, com.sun.source.doctree.DocCommentTree doc, int pos)
treepath
- for which the doc
comment was determineddoc
- the documentation comment inside which the search should be performedpos
- the position to search forpublic DocTreePath pathFor(DocTreePath path, int pos)
path
- where the search should startpos
- the position to search forpublic DocTreePath pathFor(DocTreePath path, int pos, DocSourcePositions sourcePositions)
path
- where the search should startpos
- the position to search forsourcePositions
- to determine spans of DocTree
spublic TypeMirror parseType(String expr, TypeElement scope)
expr
- type specificationscope
- in which simple names should be resolvedTypeMirror
or null if the given specification cannot be parsedpublic StatementTree parseStatement(String stmt, SourcePositions[] sourcePositions)
stmt
- statement codesourcePositions
- return value - new SourcePositions for the new treeStatementTree
or null?public ExpressionTree parseExpression(String expr, SourcePositions[] sourcePositions)
expr
- expression codesourcePositions
- return value - new SourcePositions for the new treeExpressionTree
or null?public ExpressionTree parseVariableInitializer(String init, SourcePositions[] sourcePositions)
init
- initializer codesourcePositions
- return value - new SourcePositions for the new treeExpressionTree
or null?public BlockTree parseStaticBlock(String block, SourcePositions[] sourcePositions)
block
- block codesourcePositions
- return value - new SourcePositions for the new treeBlockTree
or null?public Scope toScopeWithDisabledAccessibilityChecks(Scope scope)
Scope
capable to access all private methods and fields when
parsing and evaluating expressions. When using this Scope, the accessibility
checks would not be enforced during a tree attribution.scope
- an existing scopepublic TypeMirror attributeTree(Tree tree, Scope scope)
public Scope attributeTreeTo(Tree tree, Scope scope, Tree to)
to
tree is reached.
Returns scope valid at point when to
is reached.public TypeMirror reattributeTree(Tree tree, Scope scope)
public TokenSequence<org.netbeans.api.java.lexer.JavaTokenId> tokensFor(Tree tree)
public TokenSequence<org.netbeans.api.java.lexer.JavaTokenId> tokensFor(Tree tree, SourcePositions sourcePositions)
SourcePositions
.@Deprecated public boolean isAccessible(Scope scope, Element member, TypeMirror type)
Trees.isAccessible(Scope, Element, DeclaredType)
should be used instead.scope
- the scope to be checkedmember
- the member to be checkedtype
- the type for which to check if the member is accessiblemember
is accessible in type
public boolean isStaticContext(Scope scope)
public Set<TypeMirror> getUncaughtExceptions(TreePath path)
public Set<? extends VariableElement> getUninitializedFields(TreePath path)
path
- to class to inspectpublic int[] findBodySpan(ClassTree clazz)
ClassTree
's body in the source.
Returns starting and ending offset of the body in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.clazz
- class which body should be searched forpublic int[] findNameSpan(ClassTree clazz)
ClassTree.getSimpleName()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.clazz
- class which name should be searched forpublic int[] findNameSpan(MethodTree method)
MethodTree.getName()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.method
- method which name should be searched forpublic int[] findNameSpan(VariableTree var)
VariableTree.getName()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.var
- variable which name should be searched forpublic int[] findNameSpan(LabeledStatementTree lst)
LabeledStatementTree.getLabel()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.lst
- labeled statement which name should be searched forpublic int[] findNameSpan(TypeParameterTree tpt)
TypeParameterTree.getName()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.tpt
- type parameter which name should be searched forpublic int[] findNameSpan(BreakTree brk)
LabeledStatementTree.getLabel()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.brk
- labeled statement which name should be searched forpublic int[] findNameSpan(ContinueTree cont)
LabeledStatementTree.getLabel()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.cont
- labeled statement which name should be searched for@CheckForNull public int[] findMethodParameterSpan(@NonNull MethodTree method)
MethodTree.getParameters()
parameter list in the source.
Returns the position of the opening and closing parentheses of the parameter list
in the source code that was parsed (ie. CompilationInfo.getText()
, which
may differ from the positions in the source document if it has been already altered.method
- method which parameter list should be searched forpublic int[] findNameSpan(MemberSelectTree mst)
MemberSelectTree.getIdentifier()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.mst
- member select which identifier should be searched forpublic int[] findNameSpan(MemberReferenceTree mst)
MemberReferenceTree.getName()
identifier in the source.
Returns starting and ending offset of the name in the source code that was parsed
(ie. CompilationInfo.getText()
, which may differ from the positions in the source
document if it has been already altered.mst
- member reference for which the identifier should be searched forpublic int[] findNameSpan(com.sun.source.doctree.DocCommentTree docTree, com.sun.source.doctree.ReferenceTree ref)
TreeUtilities.getReferenceName(com.sun.source.util.DocTreePath)
identifier in the source. Returns starting and ending offset of the name in
the source code that was parsed (ie. CompilationInfo.getText()
, which
may differ from the positions in the source document if it has been already
altered.ref
- reference for which the identifier should be foundpublic Tree getBreakContinueTargetTree(TreePath breakOrContinue) throws IllegalArgumentException
break
or continue
. The given
CompilationInfo
has to be at least in the JavaSource.Phase.RESOLVED
phase.breakOrContinue
- TreePath
to the tree that should be inspected.
The breakOrContinue.getLeaf().getKind()
has to be one of Tree.Kind.BREAK
, Tree.Kind.CONTINUE
, or Tree.Kind.YIELD
, or
an IllegalArgumentException is thrownIllegalArgumentException
- if the given tree is not a break or continue tree or if the given CompilationInfo
is not in the JavaSource.Phase.RESOLVED
phase.public StatementTree getBreakContinueTarget(TreePath breakOrContinue) throws IllegalArgumentException
break
or continue
. The given
CompilationInfo
has to be at least in the JavaSource.Phase.RESOLVED
phase.breakOrContinue
- TreePath
to the tree that should be inspected.
The breakOrContinue.getLeaf().getKind()
has to be either Tree.Kind.BREAK
or Tree.Kind.CONTINUE
, or
an IllegalArgumentException is thrownIllegalArgumentException
- if the given tree is not a break or continue tree or if the given CompilationInfo
is not in the JavaSource.Phase.RESOLVED
phase.@NonNull public CharSequence decodeIdentifier(@NonNull CharSequence text)
text
- to decode@NonNull public CharSequence encodeIdentifier(@NonNull CharSequence ident)
ident
- to encode@NonNull public Tree translate(@NonNull Tree original, @NonNull Map<? extends Tree,? extends Tree> original2Translated)
original
, such that each visited subtree
that occurs as a key in original2Translated
is replaced by the corresponding
value from original2Translated
. The value is then translated using the same
algorithm. Each key from original2Translated
is used at most once.
Unless the provided original
tree is a key in original2Translated
,
the resulting tree has the same type as original
.
Principally, the method inner workings are:
translate(original, original2Translated) { if (original2Translated.containsKey(original)) return translate(original2Translated.remove(original)); newTree = copyOf(original); for (Tree child : allChildrenOf(original)) { newTree.replace(child, translate(child, original2Translated)); } return newTree; }
original
- the tree that should be translatedoriginal2Translated
- map containing trees that should be translated@NonNull public com.sun.source.doctree.DocTree translate(@NonNull com.sun.source.doctree.DocTree original, @NonNull Map<? extends com.sun.source.doctree.DocTree,? extends com.sun.source.doctree.DocTree> original2Translated)
original
, such that each visited subtree
that occurs as a key in original2Translated
is replaced by the corresponding
value from original2Translated
. The value is then translated using the same
algorithm. Each key from original2Translated
is used at most once.
Unless the provided original
tree is a key in original2Translated
,
the resulting tree has the same type as original
.
Principally, the method inner workings are:
translate(original, original2Translated) { if (original2Translated.containsKey(original)) return translate(original2Translated.remove(original)); newTree = copyOf(original); for (Tree child : allChildrenOf(original)) { newTree.replace(child, translate(child, original2Translated)); } return newTree; }
original
- the tree that should be translatedoriginal2Translated
- map containing trees that should be translatedpublic boolean isCompileTimeConstantExpression(TreePath expression)
expression
- the expression to check@CheckForNull public ExpressionTree getReferenceClass(@NonNull DocTreePath path)
#
) that is being referenced by the given ReferenceTree
.path
- the leaf must be ReferenceTree
null
if none.@CheckForNull public Name getReferenceName(@NonNull DocTreePath path)
#
) that is being referenced by the given ReferenceTree
.path
- the leaf must be ReferenceTree
null
if none.@CheckForNull public List<? extends Tree> getReferenceParameters(@NonNull DocTreePath path)
ReferenceTree
.path
- the leaf must be ReferenceTree
null
if none.public boolean isVarType(@NonNull TreePath path)
TreePath
.path
- the path of tree TreePath
public boolean isEndOfCompoundVariableDeclaration(@NonNull Tree tree)
Tree
.tree
- the tree Tree
public boolean isPartOfCompoundVariableDeclaration(@NonNull Tree tree)
tree
- tree to examine.tree
is part of compound var declaration.