org.netbeans.api.java.source
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: http://wikis.sun.com/display/mlvm/ProjectCoinProposal, 3.1-3.9.
|
CharSequence |
encodeIdentifier(CharSequence ident)
Encode identifier using escapes defined in: http://wikis.sun.com/display/mlvm/ProjectCoinProposal, 3.1-3.9.
|
int[] |
findNameSpan(ClassTree clazz)
Find span of the
ClassTree.getSimpleName() 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(VariableTree var)
Find span of the
VariableTree.getName() identifier in the source. |
StatementTree |
getBreakContinueTarget(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.
|
Set<TypeMirror> |
getUncaughtExceptions(TreePath path)
Returns uncaught exceptions inside the given tree path.
|
boolean |
isAccessible(Scope scope,
Element member,
TypeMirror type)
Checks whether the given element is accessible as a member of the given
type in a given scope.
|
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 |
isEnum(ClassTree tree)
Deprecated.
since 0.67,
Tree.getKind() == Kind.ENUM should be used instead. |
boolean |
isEnumConstant(VariableTree tree)
Checks wheteher given variable tree represents an enum constant.
|
boolean |
isInterface(ClassTree tree)
Deprecated.
since 0.67,
Tree.getKind() == Kind.INTERFACE should be used instead. |
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.
|
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.
|
TreePath |
pathFor(int pos) |
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.
|
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 isSynthetic(TreePath path) throws NullPointerException
NullPointerException
- if the given tree is nullpublic 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 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 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
.public boolean isAccessible(Scope scope, Element member, TypeMirror type)
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 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(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 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 decodehttp://wikis.sun.com/display/mlvm/ProjectCoinProposal
@NonNull public CharSequence encodeIdentifier(@NonNull CharSequence ident)
text
- to encodehttp://wikis.sun.com/display/mlvm/ProjectCoinProposal
@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 translatedBuilt on March 18 2014. | Portions Copyright 1997-2014 Sun Microsystems, Inc. All rights reserved.