本文整理汇总了Java中org.eclipse.jdt.core.ICompilationUnit.discardWorkingCopy方法的典型用法代码示例。如果您正苦于以下问题:Java ICompilationUnit.discardWorkingCopy方法的具体用法?Java ICompilationUnit.discardWorkingCopy怎么用?Java ICompilationUnit.discardWorkingCopy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.ICompilationUnit
的用法示例。
在下文中一共展示了ICompilationUnit.discardWorkingCopy方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: handleClosed
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public void handleClosed(DidCloseTextDocumentParams params) {
String uri = params.getTextDocument().getUri();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null) {
return;
}
try {
if (JDTUtils.isDefaultProject(unit) || !JDTUtils.isOnClassPath(unit)) {
new DiagnosticsHandler(connection, unit).clearDiagnostics();
}
sharedASTProvider.invalidate(unit);
unit.discardWorkingCopy();
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Error while handling document close", e);
}
}
示例3: 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);
}
}
}
示例4: formatUnitSourceCode
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
/**
* Format a Unit Source Code
*
* @param testInterface
* @param monitor
* @throws CoreException
*/
@SuppressWarnings("unchecked")
public static void formatUnitSourceCode(IFile file, IProgressMonitor monitor) throws CoreException {
@SuppressWarnings("rawtypes")
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
subMonitor.split(50);
ICompilationUnit workingCopy = unit.getWorkingCopy(monitor);
Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
DefaultCodeFormatterConstants.createAlignmentValue(true,
DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
ISourceRange range = unit.getSourceRange();
TextEdit formatEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(),
range.getOffset(), range.getLength(), 0, null);
subMonitor.split(30);
if (formatEdit != null /* && formatEdit.hasChildren()*/) {
workingCopy.applyTextEdit(formatEdit, monitor);
workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null);
workingCopy.commitWorkingCopy(true, null);
workingCopy.discardWorkingCopy();
}
file.refreshLocal(IResource.DEPTH_INFINITE, subMonitor);
subMonitor.split(20);
}
示例5: updatePathGenerator
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public static void updatePathGenerator (IFile ifile, String oldPathGenerator,String newPathGenerator) throws CoreException {
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(ifile);
ICompilationUnit workingCopy = cu.getWorkingCopy(new NullProgressMonitor());
IBuffer buffer = ((IOpenable)workingCopy).getBuffer();
String source = buffer.getContents();
int start = source.indexOf(oldPathGenerator);
buffer.replace(start, oldPathGenerator.length(), newPathGenerator);
workingCopy.reconcile(ICompilationUnit.NO_AST, false, workingCopy.getOwner(), new NullProgressMonitor());
workingCopy.commitWorkingCopy(true, null);
workingCopy.discardWorkingCopy();
ifile.touch(new NullProgressMonitor ());
}
示例6: 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);
}
}
}
示例7: tearDown
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
@After
public void tearDown() throws Exception {
javaClient.disconnect();
for (ICompilationUnit cu : JavaCore.getWorkingCopies(null)) {
cu.discardWorkingCopy();
}
FileUtils.deleteQuietly(temp);
}
示例8: 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);
}
示例9: findSimilarElement
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public static SimilarElement[] findSimilarElement(ICompilationUnit cu, Name name, int kind) throws JavaModelException {
int pos= name.getStartPosition();
int nArguments= -1;
String identifier= ASTNodes.getSimpleNameIdentifier(name);
String returnType= null;
ICompilationUnit preparedCU= null;
try {
if (name.isQualifiedName()) {
pos= ((QualifiedName) name).getName().getStartPosition();
} else {
pos= name.getStartPosition() + 1; // first letter must be included, other
}
Javadoc javadoc= (Javadoc) ASTNodes.getParent(name, ASTNode.JAVADOC);
if (javadoc != null) {
preparedCU= createPreparedCU(cu, javadoc, name.getStartPosition());
cu= preparedCU;
}
SimilarElementsRequestor requestor= new SimilarElementsRequestor(identifier, kind, nArguments, returnType);
requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, true);
requestor.setIgnored(CompletionProposal.KEYWORD, true);
requestor.setIgnored(CompletionProposal.LABEL_REF, true);
requestor.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
requestor.setIgnored(CompletionProposal.PACKAGE_REF, true);
requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
requestor.setIgnored(CompletionProposal.METHOD_REF, true);
requestor.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, true);
requestor.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, true);
requestor.setIgnored(CompletionProposal.FIELD_REF, true);
requestor.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, true);
requestor.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
requestor.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
requestor.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
return requestor.process(cu, pos);
} finally {
if (preparedCU != null) {
preparedCU.discardWorkingCopy();
}
}
}
示例10: getStaticImportFavorites
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
StringBuffer dummyCU= new StringBuffer();
String packName= cu.getParent().getElementName();
IType type= cu.findPrimaryType();
if (type == null) {
return new String[0];
}
if (packName.length() > 0) {
dummyCU.append("package ").append(packName).append(';'); //$NON-NLS-1$
}
dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName); // static initializer //$NON-NLS-1$//$NON-NLS-2$
int offset= dummyCU.length();
dummyCU.append("\n}\n }"); //$NON-NLS-1$
ICompilationUnit newCU= null;
try {
newCU= cu.getWorkingCopy(null);
newCU.getBuffer().setContents(dummyCU.toString());
final HashSet<String> result= new HashSet<>();
CompletionRequestor requestor= new CompletionRequestor(true) {
@Override
public void accept(CompletionProposal proposal) {
if (elementName.equals(new String(proposal.getName()))) {
CompletionProposal[] requiredProposals= proposal.getRequiredProposals();
for (int i= 0; i < requiredProposals.length; i++) {
CompletionProposal curr= requiredProposals[i];
if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
}
}
}
}
};
if (isMethod) {
requestor.setIgnored(CompletionProposal.METHOD_REF, false);
requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
} else {
requestor.setIgnored(CompletionProposal.FIELD_REF, false);
requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
}
requestor.setFavoriteReferences(favorites);
newCU.codeComplete(offset, requestor);
return result.toArray(new String[result.size()]);
} finally {
if (newCU != null) {
newCU.discardWorkingCopy();
}
}
}
示例11: 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;
}