// get the ast rewriter final ASTRewrite rewriter = ASTRewrite.create(ast); // get the current document source final Document document = new Document(unit.getSource()); // compute the edits you have made to the compilation unit final TextEdit edits = rewriter.rewriteAST(); // apply the edits to the document edits.apply(document); // get the new source String newSource = document.get(); // now write this source to some other file.
ASTRewrite rewriter = ASTRewrite.create(astRoot.getAST()); for (MethodInvocation metInv : getAllMethodInvocationsToRemove()) { rewriter.remove(metInv, null); } unit.applyTextEdit(rewriter.rewriteAST(), new NullProgressMonitor());
/** * Applies the accumulated refactorings to the provided document. * * @param document the document to refactor * @throws BadLocationException if trying to access a non existing position */ public void applyTo(final IDocument document) throws BadLocationException { final TextEdit edits = rewrite.rewriteAST(document, null); commentRewriter.addEdits(document, edits); sourceRewriter.addEdits(document, edits); applyEditsToDocument(edits, document); }
public void apply(IDocument document) { TextEdit edits = m_rewriter.rewriteAST(document, null); UndoEdit undo = null; try { undo = edits.apply(document); } catch(MalformedTreeException e) { e.printStackTrace(); } catch(BadLocationException e) { e.printStackTrace(); } }
public TextEdit getModifications() { return fRewrite.rewriteAST(fBuffer.getDocument(), fCUnit.getJavaProject().getOptions(true)); }
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> groups, ImportRewrite importRewrite) throws CoreException { TextChange change = fChangeManager.get(unit); MultiTextEdit root = new MultiTextEdit(); change.setEdit(root); root.addChild(importRewrite.rewriteImports(null)); root.addChild(rewriter.rewriteAST()); for (Iterator<TextEditGroup> iter = groups.iterator(); iter.hasNext();) { change.addTextEditGroup(iter.next()); } }
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> groups, ImportRewrite importRewrite) throws CoreException { TextChange change= fChangeManager.get(unit); MultiTextEdit root= new MultiTextEdit(); change.setEdit(root); root.addChild(importRewrite.rewriteImports(null)); root.addChild(rewriter.rewriteAST()); for (Iterator<TextEditGroup> iter= groups.iterator(); iter.hasNext();) { change.addTextEditGroup(iter.next()); } }
public TextEdit getModifications() { return fRewrite.rewriteAST(fBuffer.getDocument(), fCUnit.getJavaProject().getOptions(true)); }
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> groups, ImportRewrite importRewrite) throws CoreException { TextChange change= fChangeManager.get(unit); MultiTextEdit root= new MultiTextEdit(); change.setEdit(root); root.addChild(importRewrite.rewriteImports(null)); root.addChild(rewriter.rewriteAST()); for (Iterator<TextEditGroup> iter= groups.iterator(); iter.hasNext();) { change.addTextEditGroup(iter.next()); } }
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List groups, ImportRewrite importRewrite) throws CoreException { TextChange change= fChangeManager.get(unit); MultiTextEdit root= new MultiTextEdit(); change.setEdit(root); root.addChild(importRewrite.rewriteImports(null)); root.addChild(rewriter.rewriteAST()); for (Iterator iter= groups.iterator(); iter.hasNext();) { change.addTextEditGroup((TextEditGroup)iter.next()); } }
public static String rewrite(final CompilationUnit unit, final AST ast) { final IDocument document = new Document(unit.toString()); final ASTRewrite rewriter = ASTRewrite.create(ast); final TextEdit edits = rewriter.rewriteAST(document, null); try { edits.apply(document); //log.debug("{}", formatted); return formatCode(document); } catch (BadLocationException e) { log.error("Error creating HippoBean", e); } return null; }
public static String rewrite(final CompilationUnit unit, final AST ast) { final IDocument document = new Document(unit.toString()); final ASTRewrite rewriter = ASTRewrite.create(ast); final TextEdit edits = rewriter.rewriteAST(document, null); try { edits.apply(document); final String formatted = formatCode(document); //log.debug("{}", formatted); return formatted; } catch (BadLocationException e) { log.error("Error creating HippoBean", e); } return null; }
ICompilationUnit cu = ejbType.getCompilationUnit(); cu.becomeWorkingCopy(...); CompilationUnit unit = parseAst(ejbType.getCompilationUnit()) final ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); collectChangesToUnit(unit, rewrite); cu.applyTextEdit(rewrite.rewriteAST(), ...); cu.commitWorkingCopy(false, ...);
public TextEdit getDeleteEdit() { final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST()); rewriter.remove(fDeclaration, null); return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true)); }
public TextEdit getDeleteEdit() { final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST()); rewriter.remove(fDeclaration, null); return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true)); }
public TextEdit getDeleteEdit() { final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST()); rewriter.remove(fDeclaration, null); return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true)); }
/** * Apply all changes related to a single ICompilationUnit */ private void addAllChangesFor(ICompilationUnit icu, Set vars, CompilationUnitChange unitChange) throws CoreException { CompilationUnit unit= new RefactoringASTParser(AST.JLS3).parse(icu, false); ASTRewrite unitRewriter= ASTRewrite.create(unit.getAST()); MultiTextEdit root= new MultiTextEdit(); unitChange.setEdit(root); // Adam sez don't need this, but then unitChange.addGroupDescription() fails an assertion! String typeName= updateImports(unit, root); updateCu(unit, vars, unitChange, unitRewriter, typeName); root.addChild(unitRewriter.rewriteAST()); }
private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException { // ensure cu is consistent (noop if already consistent) cu.makeConsistent(this.progressMonitor); this.parser.setSource(cu); CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); ASTNode node = ((JavaElement) elementToRemove).findNode(astCU); if (node == null) Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$ AST ast = astCU.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); rewriter.remove(node, null); TextEdit edits = rewriter.rewriteAST(); applyTextEdit(cu, edits); }
private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException { // ensure cu is consistent (noop if already consistent) cu.makeConsistent(this.progressMonitor); this.parser.setSource(cu); CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); ASTNode node = ((JavaElement) elementToRemove).findNode(astCU); if (node == null) Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$ AST ast = astCU.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); rewriter.remove(node, null); TextEdit edits = rewriter.rewriteAST(); applyTextEdit(cu, edits); }
protected static BodyDeclaration createPlaceholderForProtectedTypeDeclaration(final BodyDeclaration bodyDeclaration, final CompilationUnit declaringCuNode, final ICompilationUnit declaringCu, final ASTRewrite rewrite, final boolean removeIndentation) throws JavaModelException { String text= null; try { final ASTRewrite rewriter= ASTRewrite.create(bodyDeclaration.getAST()); ModifierRewrite.create(rewriter, bodyDeclaration).setVisibility(Modifier.PROTECTED, null); final ITrackedNodePosition position= rewriter.track(bodyDeclaration); final IDocument document= new Document(declaringCu.getBuffer().getText(declaringCuNode.getStartPosition(), declaringCuNode.getLength())); rewriter.rewriteAST(document, declaringCu.getJavaProject().getOptions(true)).apply(document, TextEdit.UPDATE_REGIONS); text= document.get(position.getStartPosition(), position.getLength()); } catch (BadLocationException exception) { text= getNewText(bodyDeclaration, declaringCu, removeIndentation); } return (BodyDeclaration) rewrite.createStringPlaceholder(text, ASTNode.TYPE_DECLARATION); }