|
Eclipse JDT Release 3.0 |
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.jdt.core.dom.rewrite.ASTRewrite
Infrastucture for modifying code by describing changes to AST nodes.
The AST rewriter collects descriptions of modifications to nodes and
translates these descriptions into text edits that can then be applied to
the original source. The key thing is that this is all done without actually
modifying the original AST, which has the virtue of allowing one to entertain
several alternate sets of changes on the same AST (e.g., for calculating
quick fix proposals). The rewrite infrastructure tries to generate minimal
text changes, preserve existing comments and indentation, and follow code
formatter settings. If the freedom to explore multiple alternate changes is
not required, consider using the AST's built-in rewriter
(see CompilationUnit.rewrite(IDocument, Map)
).
The following code snippet illustrated usage of this class:
Document doc = new Document("import java.util.List;\nclass X {}\n"); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); AST ast = cu.getAST(); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] {"java", "util", "Set"}); ASTRewrite rewriter = ASTRewrite.create(ast); TypeDeclaration td = (TypeDeclaration) cu.types().get(0); ITrackedNodePosition tdLocation = rewriter.track(td); ListRewriter lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY); lrw.insertLast(id, null); TextEdit edits = rewriter.rewriteAST(document, null); UndoEdit undo = edits.apply(document); assert "import java.util.List;\nimport java.util.Set;\nclass X {}" .equals(doc.get().toCharArray()); // tdLocation.getStartPosition() and tdLocation.getLength() // are new source range for "class X {}" in doc.get()
This class is not intended to be subclassed.
Constructor Summary | |
protected |
ASTRewrite(AST ast)
Internal constructor. |
Method Summary | |
static ASTRewrite |
create(AST ast)
Creates a new instance for describing manipulations of the given AST. |
ASTNode |
createCopyTarget(ASTNode node)
Creates and returns a placeholder node for a true copy of the given node. |
ASTNode |
createMoveTarget(ASTNode node)
Creates and returns a placeholder node for the new locations of the given node. |
ASTNode |
createStringPlaceholder(String code,
int nodeType)
Creates and returns a placeholder node for a source string that is to be inserted into the output document at the position corresponding to the placeholder. |
AST |
getAST()
Returns the AST the rewrite was set up on. |
ListRewrite |
getListRewrite(ASTNode node,
ChildListPropertyDescriptor property)
Creates and returns a new rewriter for describing modifications to the given list property of the given node. |
protected org.eclipse.jdt.internal.core.dom.rewrite.NodeInfoStore |
getNodeStore()
Internal method. |
protected org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore |
getRewriteEventStore()
Internal method. |
void |
remove(ASTNode node,
org.eclipse.text.edits.TextEditGroup editGroup)
Removes the given node from its parent in this rewriter. |
void |
replace(ASTNode node,
ASTNode replacement,
org.eclipse.text.edits.TextEditGroup editGroup)
Replaces the given node in this rewriter. |
org.eclipse.text.edits.TextEdit |
rewriteAST(org.eclipse.jface.text.IDocument document,
Map options)
Converts all modifications recorded by this rewriter into an object representing the corresponding text edits to the given document containing the original source code. |
void |
set(ASTNode node,
StructuralPropertyDescriptor property,
Object value,
org.eclipse.text.edits.TextEditGroup editGroup)
Sets the given property of the given node. |
String |
toString()
Returns a string suitable for debugging purposes (only). |
ITrackedNodePosition |
track(ASTNode node)
Returns an object that tracks the source range of the given node across the rewrite to its AST. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
protected ASTRewrite(AST ast)
create(AST)
to create instances.
ast
- the AST being rewrittenMethod Detail |
public static ASTRewrite create(AST ast)
ast
- the AST whose nodes will be rewritten
public final AST getAST()
protected final org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore getRewriteEventStore()
protected final org.eclipse.jdt.internal.core.dom.rewrite.NodeInfoStore getNodeStore()
public org.eclipse.text.edits.TextEdit rewriteAST(org.eclipse.jface.text.IDocument document, Map options) throws IllegalArgumentException
Calling this methods does not discard the modifications on record. Subsequence modifications are added to the ones already on record. If this method is called again later, the resulting text edit object will accurately reflect the net cumulative affect of all those changes.
document
- original document containing source codeoptions
- the table of formatter options
(key type: String
; value type: String
);
or null
to use the standard global options
JavaCore.getOptions()
IllegalArgumentException
- An IllegalArgumentException
is thrown if the document passed does not correspond to the AST that is rewritten.public final void remove(ASTNode node, org.eclipse.text.edits.TextEditGroup editGroup)
node
- the node being removededitGroup
- the edit group in which to collect the corresponding
text edits, or null
if ungrouped
IllegalArgumentException
- if the node is null, or if the node is not
part of this rewriter's AST, or if the described modification is invalid
(such as removing a required node)public final void replace(ASTNode node, ASTNode replacement, org.eclipse.text.edits.TextEditGroup editGroup)
createCopyTarget(ASTNode)
or createStringPlaceholder(String, int)
). The AST itself
is not actually modified in any way; rather, the rewriter just records
a note that this node has been replaced.
node
- the node being replacedreplacement
- the replacement node, or null
if no
replacementeditGroup
- the edit group in which to collect the corresponding
text edits, or null
if ungrouped
IllegalArgumentException
- if the node is null, or if the node is not part
of this rewriter's AST, or if the replacement node is not a new node (or
placeholder), or if the described modification is otherwise invalidpublic final void set(ASTNode node, StructuralPropertyDescriptor property, Object value, org.eclipse.text.edits.TextEditGroup editGroup)
createCopyTarget(ASTNode)
or createStringPlaceholder(String, int)
); or it must be
null, indicating that the child should be deleted.
If the given property is a simple property, the value must be the new
value (primitive types must be boxed) or null.
The AST itself is not actually modified in any way; rather, the rewriter
just records a note that this node has been changed in the specified way.
node
- the nodeproperty
- the node's property; either a simple property or a child propertyvalue
- the replacement child or new value, or null
if noneeditGroup
- the edit group in which to collect the corresponding
text edits, or null
if ungrouped
IllegalArgumentException
- if the node or property is null, or if the node
is not part of this rewriter's AST, or if the property is not a node property,
or if the described modification is invalidpublic final ListRewrite getListRewrite(ASTNode node, ChildListPropertyDescriptor property)
node
- the nodeproperty
- the node's property; the child list property
IllegalArgumentException
- if the node or property is null, or if the node
is not part of this rewriter's AST, or if the property is not a node property,
or if the described modification is invalidpublic final ITrackedNodePosition track(ASTNode node)
rewrite
is called, the result object is updated to
reflect the given node's source range in the rewritten AST.
node
- the node to track
node
IllegalArgumentException
- if the node is null, or if the node
is not part of this rewriter's AST, or if the node is already being
trackedpublic final ASTNode createStringPlaceholder(String code, int nodeType)
code
- the string to be inserted; lines should should not have extra indentationnodeType
- the ASTNode type that corresponds to the passed code.
IllegalArgumentException
- if the code is null, or if the node
type is invalidpublic final ASTNode createCopyTarget(ASTNode node)
node
- the node to create a copy placeholder for
IllegalArgumentException
- if the node is null, or if the node
is not part of this rewriter's ASTpublic final ASTNode createMoveTarget(ASTNode node)
node
- the node to create a move placeholder for
IllegalArgumentException
- if the node is null, or if the node
is not part of this rewriter's ASTpublic String toString()
|
Eclipse JDT Release 3.0 |
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |