本文整理汇总了Java中org.eclipse.jdt.core.ICompilationUnit.becomeWorkingCopy方法的典型用法代码示例。如果您正苦于以下问题:Java ICompilationUnit.becomeWorkingCopy方法的具体用法?Java ICompilationUnit.becomeWorkingCopy怎么用?Java ICompilationUnit.becomeWorkingCopy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.ICompilationUnit
的用法示例。
在下文中一共展示了ICompilationUnit.becomeWorkingCopy方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCodeAction_exception
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
@Test
public void testCodeAction_exception() throws JavaModelException {
URI uri = project.getFile("nopackage/Test.java").getRawLocationURI();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
try {
cu.becomeWorkingCopy(new NullProgressMonitor());
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(uri.toString()));
final Range range = new Range();
range.setStart(new Position(0, 17));
range.setEnd(new Position(0, 17));
params.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Collections.emptyList());
params.setContext(context);
List<? extends Command> commands = server.codeAction(params).join();
Assert.assertNotNull(commands);
Assert.assertEquals(0, commands.size());
} finally {
cu.discardWorkingCopy();
}
}
示例2: handleSaved
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public void handleSaved(DidSaveTextDocumentParams params) {
String uri = params.getTextDocument().getUri();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null) {
JavaLanguageServerPlugin.logError(uri + " does not resolve to a ICompilationUnit");
return;
}
// see https://github.com/redhat-developer/vscode-java/issues/274
unit = checkPackageDeclaration(uri, unit);
if (unit.isWorkingCopy()) {
try {
projectsManager.fileChanged(uri, CHANGE_TYPE.CHANGED);
unit.discardWorkingCopy();
unit.becomeWorkingCopy(new NullProgressMonitor());
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Error while handling document save", e);
}
}
}
示例3: testCompletion_javadoc
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
@Test
public void testCompletion_javadoc() throws Exception {
IJavaProject javaProject = JavaCore.create(project);
ICompilationUnit unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/TestJavadoc.java"));
unit.becomeWorkingCopy(null);
String joinOnCompletion = System.getProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION);
try {
System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, "true");
int[] loc = findCompletionLocation(unit, "inner.");
TextDocumentPositionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]));
String source = unit.getSource();
changeDocument(unit, source, 3);
Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor());
changeDocument(unit, source, 4);
CompletionList list = server.completion(position).join().getRight();
for (CompletionItem item : list.getItems()) {
server.resolveCompletionItem(item);
}
CompletionItem resolved = list.getItems().get(0);
assertEquals("Test ", resolved.getDocumentation());
} finally {
unit.discardWorkingCopy();
if (joinOnCompletion == null) {
System.clearProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION);
} else {
System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, joinOnCompletion);
}
}
}
示例4: addGeneratedAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的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);
}
示例5: checkPackageDeclaration
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
private ICompilationUnit checkPackageDeclaration(String uri, ICompilationUnit unit) {
if (unit.getResource() != null && unit.getJavaProject() != null && unit.getJavaProject().getProject().getName().equals(ProjectsManager.DEFAULT_PROJECT_NAME)) {
try {
CompilationUnit astRoot = SharedASTProvider.getInstance().getAST(unit, new NullProgressMonitor());
IProblem[] problems = astRoot.getProblems();
for (IProblem problem : problems) {
if (problem.getID() == IProblem.PackageIsNotExpectedPackage) {
IResource file = unit.getResource();
boolean toRemove = file.isLinked();
if (toRemove) {
IPath path = file.getParent().getProjectRelativePath();
if (path.segmentCount() > 0 && JDTUtils.SRC.equals(path.segments()[0])) {
String packageNameResource = path.removeFirstSegments(1).toString().replace(JDTUtils.PATH_SEPARATOR, JDTUtils.PERIOD);
path = file.getLocation();
if (path != null && path.segmentCount() > 0) {
path = path.removeLastSegments(1);
String pathStr = path.toString().replace(JDTUtils.PATH_SEPARATOR, JDTUtils.PERIOD);
if (pathStr.endsWith(packageNameResource)) {
toRemove = false;
}
}
}
}
if (toRemove) {
file.delete(true, new NullProgressMonitor());
sharedASTProvider.invalidate(unit);
unit.discardWorkingCopy();
unit = JDTUtils.resolveCompilationUnit(uri);
unit.becomeWorkingCopy(new NullProgressMonitor());
triggerValidation(unit);
}
break;
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
return unit;
}