public static String rewriteSource(CompilationUnit cu, File file) { Document document = new Document(getSourceDoc(file.getPath())); TextEdit edits = cu.rewrite(document, formatterOptions); try { edits.apply(document); } catch (Exception e) { throw new RuntimeException(e); } return document.get(); }
private void saveChanges(ICompilationUnit copy, CompilationUnit unit) throws JavaModelException, MalformedTreeException, BadLocationException { Document document= new Document(copy.getBuffer().getContents()); TextEdit edits = unit.rewrite(document, copy.getJavaProject().getOptions(true)); edits.apply(document); copy.getBuffer().setContents(document.get()); copy.save(null, false); }
/** * Return this {@link JavaType} file as a String */ @Override public String toString() { Document document = new Document(this.document.get()); try { @SuppressWarnings("rawtypes") Map options = JavaCore.getOptions(); options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_7); options.put(CompilerOptions.OPTION_Encoding, "UTF-8"); TextEdit edit = unit.rewrite(document, options); edit.apply(document); } catch (Exception e) { throw new ParserException("Could not modify source: " + unit.toString(), e); } return Formatter.format(document.get()); }
ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setCompilerOptions(....); Document document = new Document(""); parser.setSource(document.get().toCharArray()); CompilationUnit unit = (CompilationUnit)parser.createAST(null); unit.recordModifications(); TextEdit edits = unit.rewrite(document, null); edits.apply(document); // now write document.get() to file
@Override public String toUnformattedString() { Document documentLocal = new Document(this.document.get()); try { Map<String, String> options = JDTOptions.getJDTOptions(); TextEdit edit = unit.rewrite(documentLocal, options); edit.apply(documentLocal); } catch (Exception e) { throw new ParserException("Could not modify source: " + unit.toString(), e); } return documentLocal.get(); }
/** * Return this {@link JavaType} file as a String */ @Override public String toString() { Document document = new Document(this.document.get()); try { TextEdit edit = unit.rewrite(document, null); edit.apply(document); } catch (Exception e) { throw new ParserException("Could not modify source: " + unit.toString(), e); } return Formatter.format(document.get()); }
TextEdit edits = astRoot.rewrite(doc, compilationUnit.getJavaProject().getOptions(true)); AnnotationEditFormatter formatter = new AnnotationEditFormatter(doc); formatter.apply(edits);