List of usage examples for org.eclipse.jdt.core ICompilationUnit becomeWorkingCopy
void becomeWorkingCopy(IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException;
From source file:org.eclim.plugin.jdt.util.JavaUtils.java
License:Open Source License
/** * Gets the problems for a given src file. * * @param src The src file./*w w w . jav a2 s .co m*/ * @param ids Array of problem ids to accept. * @return The problems. */ public static IProblem[] getProblems(ICompilationUnit src, int[] ids) throws Exception { ICompilationUnit workingCopy = src.getWorkingCopy(null); ProblemRequestor requestor = new ProblemRequestor(ids); try { workingCopy.discardWorkingCopy(); workingCopy.becomeWorkingCopy(requestor, null); } finally { workingCopy.discardWorkingCopy(); } List<IProblem> problems = requestor.getProblems(); return (IProblem[]) problems.toArray(new IProblem[problems.size()]); }
From source file:org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests.java
License:Open Source License
/** * This method is still necessary when we need to use an owner and a specific problem requestor * (typically while using primary owner). * @deprecated//w w w. j a va2 s.c om */ public ICompilationUnit getWorkingCopy(String path, String source, WorkingCopyOwner owner, IProblemRequestor problemRequestor) throws JavaModelException { ICompilationUnit workingCopy = getCompilationUnit(path); if (owner != null) workingCopy = workingCopy.getWorkingCopy(owner, problemRequestor, null/*no progress monitor*/); else workingCopy.becomeWorkingCopy(problemRequestor, null/*no progress monitor*/); workingCopy.getBuffer().setContents(source); if (problemRequestor instanceof ProblemRequestor) ((ProblemRequestor) problemRequestor).initialize(source.toCharArray()); workingCopy.makeConsistent(null/*no progress monitor*/); return workingCopy; }
From source file:org.jboss.tools.vscode.java.internal.handlers.DocumentLifeCycleHandler.java
License:Open Source License
private void handleOpen(DidOpenTextDocumentParams params) { String uri = params.getTextDocument().getUri(); if (MODE_SOURCEGRAPH) { // SOURCEGRAPH: URI is expected to be in form file:///foo/bar, // but we need to construct absolute file URI uri = new File(connection.getWorkpaceRoot(), uri.substring(8)).toURI().toString(); }//from w w w . j av a2 s. c o m ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (unit == null) { return; } try { // The open event can happen before the workspace element added event when a new file is added. // checks if the underlying resource exists and refreshes to sync the newly created file. if (unit.getResource() != null && !unit.getResource().isAccessible()) { try { unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor()); } catch (CoreException e) { // ignored } } IBuffer buffer = unit.getBuffer(); if (buffer != null) buffer.setContents(params.getTextDocument().getText()); // TODO: wire up cancellation. unit.becomeWorkingCopy(new DiagnosticsHandler(connection, unit.getUnderlyingResource()), null); unit.reconcile(); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Creating working copy ", e); } }
From source file:org.smartfrog.tools.eclipse.ui.project.document.BaseComponentCreationWizardPage.java
License:Open Source License
public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException { super.createType(monitor); ICompilationUnit cu = (this.getCreatedType().getCompilationUnit()); cu.createImport("org.smartfrog.sfcore.common.SmartFrogException", null, //$NON-NLS-1$ null);/*from ww w . j a v a 2s . c o m*/ cu.createImport("org.smartfrog.sfcore.prim.TerminationRecord", null, //$NON-NLS-1$ null); cu.becomeWorkingCopy(null, null); ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(cu); CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); AST ast = astRoot.getAST(); astRoot.recordModifications(); TypeDeclaration typeDeclaration = (TypeDeclaration) astRoot.types().get(0); boolean hasChange = false; if (fButtonsSelected[0]) { addSfDeployMethod(cu, astRoot, typeDeclaration, SFSTART_METHOD_NAME, true, false); hasChange = true; } if (fButtonsSelected[1]) { addSfDeployMethod(cu, astRoot, typeDeclaration, SFDEPLOY_METHOD_NAME, true, false); hasChange = true; } if (fButtonsSelected[2]) { addSfDeployMethod(cu, astRoot, typeDeclaration, SFTERMINATEWITH_METHOD_NAME, false, true); hasChange = true; } if (hasChange) { // apply the change, if any methods are added String source = cu.getBuffer().getContents(); Document document = new Document(source); TextEdit edit = astRoot.rewrite(document, null); try { edit.apply(document); } catch (MalformedTreeException e) { e.printStackTrace(); } catch (BadLocationException e) { e.printStackTrace(); } String st = document.get(); cu.getBuffer().setContents(document.get()); } cu.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1)); cu.discardWorkingCopy(); }