Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2009 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.internal.core;
 
 import java.util.Map;
 
 import  org.eclipse.core.runtime.IPath;
This operation copies/moves a collection of elements from their current container to a new container, optionally renaming the elements.

Notes:

  • If there is already an element with the same name in the new container, the operation either overwrites or aborts, depending on the collision policy setting. The default setting is abort.
  • When constructors are copied to a type, the constructors are automatically renamed to the name of the destination type.
  • When main types are renamed (move within the same parent), the compilation unit and constructors are automatically renamed
  • The collection of elements being copied must all share the same type of container (for example, must all be type members).
  • The elements are inserted in the new container in the order given.
  • The elements can be positioned in the new container - see #setInsertBefore. By default, the elements are inserted based on the default positions as specified in the creation operation for that element type.
  • This operation can be used to copy and rename elements within the same container.
  • This operation only copies elements contained within compilation units.
 
 public class CopyElementsOperation extends MultiOperation implements SuffixConstants {
 
 
 	private Map sources = new HashMap();
When executed, this operation will copy the given elements to the given containers. The elements and destination containers must be in the correct order. If there is > 1 destination, the number of destinations must be the same as the number of elements being copied/moved/renamed.
 
 public CopyElementsOperation(IJavaElement[] elementsToCopyIJavaElement[] destContainersboolean force) {
 	super(elementsToCopydestContainersforce);
 }
When executed, this operation will copy the given elements to the given container.
 
 public CopyElementsOperation(IJavaElement[] elementsToCopyIJavaElement destContainerboolean force) {
 	this(elementsToCopynew IJavaElement[]{destContainer}, force);
 }
Returns the String to use as the main task name for progress monitoring.
 
 protected String getMainTaskName() {
 }
Returns the nested operation to use for processing this element
 
 	try {
 		IJavaElement dest = getDestinationParent(element);
 		switch (element.getElementType()) {
 				IImportDeclaration importDeclaration = (IImportDeclarationelement;
				return new CreateImportOperation(element.getElementName(), (ICompilationUnitdestimportDeclaration.getFlags());
				if (isRenamingMainType(elementdest)) {
					IPath path = element.getPath();
					String extension = path.getFileExtension();
					return new RenameResourceElementsOperation(new IJavaElement[] {dest}, new IJavaElement[] {dest.getParent()}, new String[]{getNewNameFor(element) + '.' + extension}, this.);
else {
					String source = getSourceFor(element);
					String lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(sourceelement.getJavaProject());
					return new CreateTypeOperation(destsource + lineSeparatorthis.);
				}
				String source = getSourceFor(element);
				String lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(sourceelement.getJavaProject());
				return new CreateMethodOperation((ITypedestsource + lineSeparatorthis.);
				source = getSourceFor(element);
				lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(sourceelement.getJavaProject());
				return new CreateFieldOperation((ITypedestsource + lineSeparatorthis.);
				source = getSourceFor(element);
				lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(sourceelement.getJavaProject());
				return new CreateInitializerOperation((ITypedestsource + lineSeparator);
			default :
				return null;
		}
catch (JavaModelException npe) {
		return null;
	}
Returns the cached source for this element or compute it if not already cached.
private String getSourceFor(IJavaElement elementthrows JavaModelException {
	String source = (Stringthis..get(element);
	if (source == null && element instanceof IMember) {
		source = ((IMember)element).getSource();
		this..put(elementsource);
	}
	return source;
Returns true if this element is the main type of its compilation unit.
protected boolean isRenamingMainType(IJavaElement elementIJavaElement destthrows JavaModelException {
	if ((isRename() || getNewNameFor(element) != null)
		String typeName = dest.getElementName();
		typeName = org.eclipse.jdt.internal.core.util.Util.getNameWithoutJavaLikeExtension(typeName);
		return element.getElementName().equals(typeName) && element.getParent().equals(dest);
	}
	return false;
Copy/move the element from the source to destination, renaming the elements as specified, honoring the collision policy.

Throws:
JavaModelException if the operation is unable to be completed
protected void processElement(IJavaElement elementthrows JavaModelException {
	boolean createElementInCUOperation =op instanceof CreateElementInCUOperation;
	if (op == null) {
		return;
	}
	if (createElementInCUOperation) {
		IJavaElement sibling = (IJavaElementthis..get(element);
		if (sibling != null) {
else
			if (isRename()) {
				IJavaElement anchor = resolveRenameAnchor(element);
				if (anchor != null) {
					((CreateElementInCUOperationop).setRelativePosition(anchor.); // insert after so that the anchor is found before when deleted below
				}
			}
		String newName = getNewNameFor(element);
		if (newName != null) {
		}
	}
	JavaElement destination = (JavaElementgetDestinationParent(element);
	ICompilationUnit unitdestination.getCompilationUnit();
	if (!unit.isWorkingCopy()) {
		unit.close();
	}
	if (createElementInCUOperation && isMove() && !isRenamingMainType(elementdestination)) {
		JavaModelOperation deleteOp = new DeleteElementsOperation(new IJavaElement[] { element }, this.);
	}
Returns the anchor used for positioning in the destination for the element being renamed. For renaming, if no anchor has explicitly been provided, the element is anchored in the same position.
	IParent parent = (IParentelement.getParent();
	IJavaElement[] children = parent.getChildren();
	for (int i = 0; i < children.lengthi++) {
		IJavaElement child = children[i];
		if (child.equals(element)) {
			return child;
		}
	}
	return null;
Possible failures:
  • NO_ELEMENTS_TO_PROCESS - no elements supplied to the operation
  • INDEX_OUT_OF_BOUNDS - the number of renamings supplied to the operation does not match the number of elements that were supplied.
protected IJavaModelStatus verify() {
	IJavaModelStatus status = super.verify();
	if (!status.isOK()) {
		return status;
	}
	if (this. != null && this..length != this..length) {
	}

See also:
MultiOperation Possible failure codes:
  • ELEMENT_DOES_NOT_EXIST - element or its specified destination is is null or does not exist. If a null element is supplied, no element is provided in the status, otherwise, the non-existant element is supplied in the status.
  • INVALID_ELEMENT_TYPES - element is not contained within a compilation unit. This operation only operates on elements contained within compilation units.
  • READ_ONLY - element is read only.
  • INVALID_DESTINATION - The destination parent specified for element is of an incompatible type. The destination for a package declaration or import declaration must be a compilation unit; the destination for a type must be a type or compilation unit; the destination for any type member (other than a type) must be a type. When this error occurs, the element provided in the operation status is the element.
  • INVALID_NAME - the new name for element does not have valid syntax. In this case the element and name are provided in the status.
protected void verify(IJavaElement elementthrows JavaModelException {
	if (element == null || !element.exists())
	if (element.getElementType() < .)
	if (element.isReadOnly())
	verifyDestination(elementdest);
	verifySibling(elementdest);
	if (this. != null) {
	}
New to GrepCode? Check out our FAQ X