Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#delete()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#delete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GWTDeleteCompilationUnitParticipantTest.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public void testCreateChange() throws OperationCanceledException,
    CoreException {
  // We're going to delete compilation unit R.java
  ICompilationUnit cu = rClass.getCompilationUnit();
  IType r = cu.getType("R");

  // Verify that the index currently contains one JSNI reference to R
  Set<IIndexedJavaRef> refs = JavaRefIndex.getInstance().findTypeReferences(
      r.getFullyQualifiedName());
  assertEquals(1, refs.size());

  // Delete R.java
  cu.delete(true, null);
  assertFalse(cu.exists());

  // Now verify that the index entries pointing to R have been purged
  refs = JavaRefIndex.getInstance().findTypeReferences(r.getElementName());
  assertEquals(0, refs.size());
}
 
Example 2
Source File: SourceFileStore.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doDelete() {
    final IJavaProject project = getRepository().getJavaProject();
    try {
        final IType type = project.findType(qualifiedClassName);
        if (type != null) {
            final IPackageFragment packageFragment = type.getPackageFragment();
            final ICompilationUnit compilationUnit = type.getCompilationUnit();
            if (compilationUnit != null) {
                closeRelatedEditorIfOpened(compilationUnit);//the editor need to be closed here, otherwise the PackageFragment are not refreshed correctly
                compilationUnit.delete(true, new NullProgressMonitor());
                deleteRecursivelyEmptyPackages(project, packageFragment);
            }
        } else {
            super.doDelete();
        }
    } catch (final JavaModelException e1) {
        BonitaStudioLog.error(e1);
        super.doDelete();
    } catch (final CoreException e) {
        BonitaStudioLog.error(e);
    }
}
 
Example 3
Source File: BaseDocumentLifeCycleHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public ICompilationUnit handleClosed(DidCloseTextDocumentParams params) {
	String uri = params.getTextDocument().getUri();
	ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
	if (unit == null) {
		return unit;
	}
	try {
		synchronized (toReconcile) {
			toReconcile.remove(unit);
		}
		if (isSyntaxMode(unit) || unit.getResource().isDerived()) {
			createDiagnosticsHandler(unit).clearDiagnostics();
		} else if (hasUnsavedChanges(unit)) {
			unit.discardWorkingCopy();
			unit.becomeWorkingCopy(new NullProgressMonitor());
			publishDiagnostics(unit, new NullProgressMonitor());
		}
		if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
			sharedASTProvider.disposeAST();
		}
		unit.discardWorkingCopy();
		if (JDTUtils.isDefaultProject(unit)) {
			File f = new File(unit.getUnderlyingResource().getLocationURI());
			if (!f.exists()) {
				unit.delete(true, null);
			}
		}
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Error while handling document close. URI: " + uri, e);
	}

	return unit;
}
 
Example 4
Source File: ConvertJavaCode.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public void runJavaConverter(final Set<ICompilationUnit> compilationUnits, Shell activeShell)
		throws ExecutionException {
	Map<ICompilationUnit, ConversionResult> conversionResults = newHashMap();
	boolean canceled = convertAllWithProgress(activeShell, compilationUnits, conversionResults);
	if (canceled) {
		return;
	}
	boolean hasConversionFailures = any(conversionResults.values(), new Predicate<ConversionResult>() {
		@Override
		public boolean apply(ConversionResult input) {
			return input.getProblems().iterator().hasNext();
		}
	});
	if (hasConversionFailures) {
		ConversionProblemsDialog problemsDialog = new ConversionProblemsDialog(activeShell, conversionResults);
		problemsDialog.open();
		if (problemsDialog.getReturnCode() == Window.CANCEL) {
			return;
		}
	}

	final String storedValue = dialogs.getUserDecision(DELETE_JAVA_FILES_AFTER_CONVERSION);

	boolean deleteJavaFiles = false;
	if (MessageDialogWithToggle.PROMPT.equals(storedValue)) {
		int userAnswer = dialogs.askUser("Delete Java source files?", "Xtend converter",
				DELETE_JAVA_FILES_AFTER_CONVERSION, activeShell);
		if (userAnswer == IDialogConstants.CANCEL_ID) {
			//cancel
			return;
		} else if (userAnswer == IDialogConstants.YES_ID) {
			deleteJavaFiles = true;
		}
	} else if (MessageDialogWithToggle.ALWAYS.equals(storedValue)) {
		deleteJavaFiles = true;
	}
	for (final Entry<ICompilationUnit, ConversionResult> result : conversionResults.entrySet()) {
		ICompilationUnit compilationUnit = result.getKey();
		ConversionResult conversionResult = result.getValue();
		String xtendCode = conversionResult.getXtendCode();
		IFile xtendFileToCreate = xtendFileToCreate(compilationUnit);
		if (!conversionResult.getProblems().iterator().hasNext()) {
			String formattedCode = formatXtendCode(xtendFileToCreate, xtendCode);
			if (formattedCode != null) {
				xtendCode = formattedCode;
			}
		}
		writeToFile(xtendFileToCreate, xtendCode);
		if (deleteJavaFiles) {
			try {
				compilationUnit.delete(true, null);
			} catch (JavaModelException e) {
				handleException("Unable to delete Java file.", e, compilationUnit.getResource());
			}
		}
	}

}