本文整理汇总了Java中org.eclipse.jdt.core.dom.CompilationUnit.recordModifications方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.recordModifications方法的具体用法?Java CompilationUnit.recordModifications怎么用?Java CompilationUnit.recordModifications使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.dom.CompilationUnit
的用法示例。
在下文中一共展示了CompilationUnit.recordModifications方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addGeneratedAnnotation
import org.eclipse.jdt.core.dom.CompilationUnit; //导入方法依赖的package包/类
/**
* @param file
* @param info
* @param monitor
* @throws MalformedTreeException
* @throws BadLocationException
* @throws CoreException
*/
@SuppressWarnings("deprecation")
public static void addGeneratedAnnotation(IFile file, IFile graphFile, IProgressMonitor monitor)
throws MalformedTreeException, BadLocationException, CoreException {
ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(file);
try {
String source = compilationUnit.getSource();
Document document = new Document(source);
compilationUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(new SubProgressMonitor(monitor, 1));
astRoot.recordModifications();
final ImportRewrite importRewrite = ImportRewrite.create(astRoot, true);
importRewrite.addImport("javax.annotation.Generated");
astRoot.accept(new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
ASTNode copiedNode = null;
// Add Generated annotation
ClassExtension ce;
try {
ce = new ClassExtension(false, false, false, false, false, false, "", "", null, false, false,
"", "", "", graphFile);
NormalAnnotation annotation = ce.getGeneratedClassAnnotation();
if (annotation != null) {
copiedNode = ASTNode.copySubtree(node.getAST(), annotation);
node.modifiers().add(0, copiedNode);
}
} catch (JavaModelException e) {
ResourceManager.logException(e);
}
return super.visit(node);
}
});
TextEdit rewrite = astRoot.rewrite(document, compilationUnit.getJavaProject().getOptions(true));
rewrite.apply(document);
TextEdit rewriteImports = importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
rewriteImports.apply(document);
String newSource = document.get();
compilationUnit.getBuffer().setContents(newSource);
compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, new SubProgressMonitor(monitor, 1));
compilationUnit.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
} finally {
compilationUnit.discardWorkingCopy();
monitor.done();
}
// WorkbenchFacade.JDTManager.reorganizeImport(compilationUnit);
}