org.eclipse.jdt.core.dom.CompilationUnit.rewrite()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(121)

本文整理了Java中org.eclipse.jdt.core.dom.CompilationUnit.rewrite()方法的一些代码示例,展示了CompilationUnit.rewrite()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CompilationUnit.rewrite()方法的具体详情如下:
包路径:org.eclipse.jdt.core.dom.CompilationUnit
类名称:CompilationUnit
方法名:rewrite

CompilationUnit.rewrite介绍

[英]Converts all modifications recorded for this compilation unit into an object representing the corresponding text edits to the given document containing the original source code for this compilation unit.

The compilation unit must have been created by ASTParser from the source code string in the given document, and recording must have been turned on with a prior call to #recordModifications()while the AST was still in its original state.

Calling this methods does not discard the modifications on record. Subsequence modifications made to the AST 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 effect of all those changes.
[中]将为此编译单元记录的所有修改转换为一个对象,该对象表示对包含此编译单元原始源代码的给定文档的相应文本编辑。
编译单元必须是由ASTParser根据给定文档中的源代码字符串创建的,并且必须在AST仍处于原始状态时通过调用#recordModifications()打开录制。
调用此方法不会放弃对记录的修改。对AST进行的子序列修改将添加到已记录的修改中。如果稍后再次调用此方法,则生成的文本编辑对象将准确反映所有这些更改的净累积效果。

代码示例

代码示例来源:origin: sorra/Exia

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();
}

代码示例来源:origin: stackoverflow.com

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);
 }

代码示例来源:origin: org.jboss.forge/roaster-jdt

/**
* 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());
}

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: forge/roaster

@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();
}

代码示例来源:origin: org.jboss.forge/roaster-jdt

/**
* 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());
}

代码示例来源:origin: org.eclipse/org.eclipse.jpt.core

TextEdit edits = astRoot.rewrite(doc, compilationUnit.getJavaProject().getOptions(true));
AnnotationEditFormatter formatter = new AnnotationEditFormatter(doc);
formatter.apply(edits);

相关文章

微信公众号

最新文章

更多