当前位置: 首页>>代码示例>>Java>>正文


Java CompilationUnit.recordModifications方法代码示例

本文整理汇总了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);
}
 
开发者ID:gw4e,项目名称:gw4e.project,代码行数:67,代码来源:JDTManager.java


注:本文中的org.eclipse.jdt.core.dom.CompilationUnit.recordModifications方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。