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

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#save() . 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: DeleteElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes this element from its compilation unit.
 * @see MultiOperation
 */
protected void processElement(IJavaElement element) throws JavaModelException {
	ICompilationUnit cu = (ICompilationUnit) element;

	// keep track of the import statements - if all are removed, delete
	// the import container (and report it in the delta)
	int numberOfImports = cu.getImports().length;

	JavaElementDelta delta = new JavaElementDelta(cu);
	IJavaElement[] cuElements = ((IRegion) this.childrenToRemove.get(cu)).getElements();
	for (int i = 0, length = cuElements.length; i < length; i++) {
		IJavaElement e = cuElements[i];
		if (e.exists()) {
			deleteElement(e, cu);
			delta.removed(e);
			if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
				numberOfImports--;
				if (numberOfImports == 0) {
					delta.removed(cu.getImportContainer());
				}
			}
		}
	}
	if (delta.getAffectedChildren().length > 0) {
		cu.save(getSubProgressMonitor(1), this.force);
		if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta
			addDelta(delta);
			setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
		}
	}
}
 
Example 2
Source File: CreateElementInCUOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Execute the operation - generate new source for the compilation unit
 * and save the results.
 *
 * @exception JavaModelException if the operation is unable to complete
 */
protected void executeOperation() throws JavaModelException {
	try {
		beginTask(getMainTaskName(), getMainAmountOfWork());
		JavaElementDelta delta = newJavaElementDelta();
		ICompilationUnit unit = getCompilationUnit();
		generateNewCompilationUnitAST(unit);
		if (this.creationOccurred) {
			//a change has really occurred
			unit.save(null, false);
			boolean isWorkingCopy = unit.isWorkingCopy();
			if (!isWorkingCopy)
				setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
			worked(1);
			this.resultElements = generateResultHandles();
			if (!isWorkingCopy // if unit is working copy, then save will have already fired the delta
					&& !Util.isExcluded(unit)
					&& unit.getParent().exists()) {
				for (int i = 0; i < this.resultElements.length; i++) {
					delta.added(this.resultElements[i]);
				}
				addDelta(delta);
			} // else unit is created outside classpath
			  // non-java resource delta will be notified by delta processor
		}
	} finally {
		done();
	}
}
 
Example 3
Source File: CreateCompilationUnitOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a compilation unit.
 *
 * @exception JavaModelException if unable to create the compilation unit.
 */
protected void executeOperation() throws JavaModelException {
	try {
		beginTask(Messages.operation_createUnitProgress, 2);
		JavaElementDelta delta = newJavaElementDelta();
		ICompilationUnit unit = getCompilationUnit();
		IPackageFragment pkg = (IPackageFragment) getParentElement();
		IContainer folder = (IContainer) pkg.getResource();
		worked(1);
		IFile compilationUnitFile = folder.getFile(new Path(this.name));
		if (compilationUnitFile.exists()) {
			// update the contents of the existing unit if fForce is true
			if (this.force) {
				IBuffer buffer = unit.getBuffer();
				if (buffer == null) return;
				buffer.setContents(this.source);
				unit.save(new NullProgressMonitor(), false);
				this.resultElements = new IJavaElement[] {unit};
				if (!Util.isExcluded(unit)
						&& unit.getParent().exists()) {
					for (int i = 0; i < this.resultElements.length; i++) {
						delta.changed(this.resultElements[i], IJavaElementDelta.F_CONTENT);
					}
					addDelta(delta);
				}
			} else {
				throw new JavaModelException(new JavaModelStatus(
					IJavaModelStatusConstants.NAME_COLLISION,
					Messages.bind(Messages.status_nameCollision, compilationUnitFile.getFullPath().toString())));
			}
		} else {
			try {
				String encoding = null;
				try {
					encoding = folder.getDefaultCharset(); // get folder encoding as file is not accessible
				}
				catch (CoreException ce) {
					// use no encoding
				}
				InputStream stream = new ByteArrayInputStream(encoding == null ? this.source.getBytes() : this.source.getBytes(encoding));
				createFile(folder, unit.getElementName(), stream, this.force);
				this.resultElements = new IJavaElement[] {unit};
				if (!Util.isExcluded(unit)
						&& unit.getParent().exists()) {
					for (int i = 0; i < this.resultElements.length; i++) {
						delta.added(this.resultElements[i]);
					}
					addDelta(delta);
				}
			} catch (IOException e) {
				throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
			}
		}
		worked(1);
	} finally {
		done();
	}
}
 
Example 4
Source File: QuickFixUtils.java    From CogniCrypt with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * This method inserts a certain import to a {@link ICompilationUnit}
 * 
 * @param unit ICompilationUnit of the source file
 * @param packagePath path of the annotation
 * @throws CoreException
 */
public static void insertJarImport(final ICompilationUnit unit, final String packagePath) throws CoreException {
	unit.createImport(packagePath, null, null);
	unit.save(null, true);
	PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().doSave(null);
}