public class WorkingCopy extends CompilationController
CompilationInfo.CacheClearPolicy
Modifier and Type | Method and Description |
---|---|
static WorkingCopy |
get(Parser.Result result)
Returns an instance of the
WorkingCopy for
given Parser.Result if it is a result
of a java parser. |
TreeMaker |
getTreeMaker() |
Tree |
resolveRewriteTarget(Tree in)
Returns the tree into which the given tree was rewritten using the
WorkingCopy.rewrite(com.sun.source.tree.Tree, com.sun.source.tree.Tree) method,
transitively. |
void |
rewrite(Tree tree,
com.sun.source.doctree.DocTree oldTree,
com.sun.source.doctree.DocTree newTree)
Replaces the original doctree
oldTree with the new one -
newTree for a specific tree. |
void |
rewrite(Tree oldTree,
Tree newTree)
Replaces the original tree
oldTree with the new one -
newTree . |
void |
rewriteInComment(int start,
int length,
String newText)
Replace a part of a comment token with the given text.
|
void |
tag(Tree t,
Object tag)
Tags a tree.
|
JavaSource.Phase |
toPhase(JavaSource.Phase phase)
Moves the state to required phase.
|
doInvalidate
getCachedValue, getChangedTree, getClasspathInfo, getCompilationUnit, getDiagnostics, getDocTrees, getDocument, getElements, getElementUtilities, getFileObject, getJavaSource, getPhase, getPositionConverter, getSnapshot, getSourceVersion, getText, getTokenHierarchy, getTopLevelElements, getTrees, getTreeUtilities, getTypes, getTypeUtilities, putCachedValue
@NullUnknown public static WorkingCopy get(@NonNull Parser.Result result)
WorkingCopy
for
given Parser.Result
if it is a result
of a java parser.result
- for which the WorkingCopy
should be
returned.WorkingCopy
or null when the given result
is not a result of java parsing.@NonNull public JavaSource.Phase toPhase(@NonNull JavaSource.Phase phase) throws IOException
CompilationController
toPhase
in class CompilationController
phase
- The required phaseIOException
- when the file cannot be red@NonNull public TreeMaker getTreeMaker() throws IllegalStateException
IllegalStateException
public void rewrite(@NullAllowed Tree oldTree, @NonNull Tree newTree)
oldTree
with the new one -
newTree
.
To create a new file, use
rewrite(null, compilationUnitTree)
. Use
GeneratorUtilities.createFromTemplate()
to create a new compilation unit tree from a template.
newTree
cannot be null
, use methods in
TreeMaker
for tree element removal. If oldTree
is
null, newTree
must be of kind
COMPILATION_UNIT
.
Since 0.137, comments in the rewritten node will be automatically assigned to the newTree
node. Use TreeMaker.asRemoved(T)
to discard comments from the oldTree explicitly.
oldTree
- tree to be replaced, use tree already represented in
source code. null
to create a new file.newTree
- new tree, either created by TreeMaker
or obtained from different place. null
values are not allowed.IllegalStateException
- if toPhase()
method was not
called before.IllegalArgumentException
- when null
was passed to the
method.GeneratorUtilities.createFromTemplate(org.openide.filesystems.FileObject, java.lang.String, javax.lang.model.element.ElementKind)
,
TreeMaker
public void rewrite(@NonNull Tree tree, @NonNull com.sun.source.doctree.DocTree oldTree, @NonNull com.sun.source.doctree.DocTree newTree)
oldTree
with the new one -
newTree
for a specific tree.
To create a new javadoc comment, use
rewrite(tree, null, docCommentTree)
.
tree
and newTree
cannot be null
.
If oldTree
is null, newTree
must be of kind
DOC_COMMENT
.
tree
- the tree to which the doctrees belong.oldTree
- tree to be replaced, use tree already represented in
source code. null
to create a new file.newTree
- new tree, either created by TreeMaker
or obtained from different place. null
values are not allowed.IllegalStateException
- if toPhase()
method was not
called before.public void rewriteInComment(int start, int length, @NonNull String newText) throws IllegalArgumentException
WorkingCopy.rewrite(Tree,Tree)
.start
- absolute offset in the original text to start the replacementlength
- how many characters should be deleted from the original textnewText
- new text to be inserted at the specified offsetIllegalArgumentException
- when an attempt is made to replace non-comment textpublic void tag(@NonNull Tree t, @NonNull Object tag)
ModificationResult
to determine position of tree inside document.t
- the tree to be taggedtag
- an Object
used as tag@NonNull public Tree resolveRewriteTarget(@NonNull Tree in)
WorkingCopy.rewrite(com.sun.source.tree.Tree, com.sun.source.tree.Tree)
method,
transitively.
Will return the input tree if the input tree was never passed as the first
parameter of the WorkingCopy.rewrite(com.sun.source.tree.Tree, com.sun.source.tree.Tree)
method.
Note that the returned tree will be exactly equivalent to a tree passed as
the second parameter to WorkingCopy.rewrite(com.sun.source.tree.Tree, com.sun.source.tree.Tree)
.
No attribution or other information will be added (or removed) to (or from) the tree.
in
- the tree to inspectWorkingCopy.rewrite(com.sun.source.tree.Tree, com.sun.source.tree.Tree)
method,
transitively