<s> package org . eclipse . jdt . internal . core ; import java . util . HashMap ; import java . util . Map ; import org . eclipse . core . runtime . IPath ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IImportDeclaration ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IJavaModelStatus ; import org . eclipse . jdt . core . IJavaModelStatusConstants ; import org . eclipse . jdt . core . IMember ; import org . eclipse . jdt . core . IParent ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . internal . compiler . util . SuffixConstants ; import org . eclipse . jdt . internal . core . util . Messages ; public class CopyElementsOperation extends MultiOperation implements SuffixConstants { private Map sources = new HashMap ( ) ; public CopyElementsOperation ( IJavaElement [ ] elementsToCopy , IJavaElement [ ] destContainers , boolean force ) { super ( elementsToCopy , destContainers , force ) ; } public CopyElementsOperation ( IJavaElement [ ] elementsToCopy , IJavaElement destContainer , boolean force ) { this ( elementsToCopy , new IJavaElement [ ] { destContainer } , force ) ; } protected String getMainTaskName ( ) { return Messages . operation_copyElementProgress ; } protected JavaModelOperation getNestedOperation ( IJavaElement element ) { try { IJavaElement dest = getDestinationParent ( element ) ; switch ( element . getElementType ( ) ) { case IJavaElement . PACKAGE_DECLARATION : return new CreatePackageDeclarationOperation ( element . getElementName ( ) , ( ICompilationUnit ) dest ) ; case IJavaElement . IMPORT_DECLARATION : IImportDeclaration importDeclaration = ( IImportDeclaration ) element ; return new CreateImportOperation ( element . getElementName ( ) , ( ICompilationUnit ) dest , importDeclaration . getFlags ( ) ) ; case IJavaElement . TYPE : if ( isRenamingMainType ( element , dest ) ) { IPath path = element . getPath ( ) ; String extension = path . getFileExtension ( ) ; return new RenameResourceElementsOperation ( new IJavaElement [ ] { dest } , new IJavaElement [ ] { dest . getParent ( ) } , new String [ ] { getNewNameFor ( element ) + '.' + extension } , this . force ) ; } else { String source = getSourceFor ( element ) ; String lineSeparator = org . eclipse . jdt . internal . core . util . Util . getLineSeparator ( source , element . getJavaProject ( ) ) ; return new CreateTypeOperation ( dest , source + lineSeparator , this . force ) ; } case IJavaElement . METHOD : String source = getSourceFor ( element ) ; String lineSeparator = org . eclipse . jdt . internal . core . util . Util . getLineSeparator ( source , element . getJavaProject ( ) ) ; return new CreateMethodOperation ( ( IType ) dest , source + lineSeparator , this . force ) ; case IJavaElement . FIELD : source = getSourceFor ( element ) ; lineSeparator = org . eclipse . jdt . internal . core . util . Util . getLineSeparator ( source , element . getJavaProject ( ) ) ; return new CreateFieldOperation ( ( IType ) dest , source + lineSeparator , this . force ) ; case IJavaElement . INITIALIZER : source = getSourceFor ( element ) ; lineSeparator = org . eclipse . jdt . internal . core . util . Util . getLineSeparator ( source , element . getJavaProject ( ) ) ; return new CreateInitializerOperation ( ( IType ) dest , source + lineSeparator ) ; default : return null ; } } catch ( JavaModelException npe ) { return null ; } } private String getSourceFor ( IJavaElement element ) throws JavaModelException { String source = ( String ) this . sources . get ( element ) ; if ( source == null && element instanceof IMember ) { source = ( ( IMember ) element ) . getSource ( ) ; this . sources . put ( element , source ) ; } return source ; } protected boolean isRenamingMainType ( IJavaElement element , IJavaElement dest ) throws JavaModelException { if ( ( isRename ( ) || getNewNameFor ( element ) != null ) && dest . getElementType ( ) == IJavaElement . COMPILATION_UNIT ) { 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 ; } protected void processElement ( IJavaElement element ) throws JavaModelException { JavaModelOperation op = getNestedOperation ( element ) ; boolean createElementInCUOperation = op instanceof CreateElementInCUOperation ; if ( op == null ) { return ; } if ( createElementInCUOperation ) { IJavaElement sibling = ( IJavaElement ) this . insertBeforeElements . get ( element ) ; if ( sibling != null ) { ( ( CreateElementInCUOperation ) op ) . setRelativePosition ( sibling , CreateElementInCUOperation . INSERT_BEFORE ) ; } else if ( isRename ( ) ) { IJavaElement anchor = resolveRenameAnchor ( element ) ; if ( anchor != null ) { ( ( CreateElementInCUOperation ) op ) . setRelativePosition ( anchor , CreateElementInCUOperation . INSERT_AFTER ) ; } } String newName = getNewNameFor ( element ) ; if ( newName != null ) { ( ( CreateElementInCUOperation ) op ) . setAlteredName ( newName ) ; } } executeNestedOperation ( op , 1 ) ; JavaElement destination = ( JavaElement ) getDestinationParent ( element ) ; ICompilationUnit unit = destination . getCompilationUnit ( ) ; if ( ! unit . isWorkingCopy ( ) ) { unit . close ( ) ; } if ( createElementInCUOperation && isMove ( ) && ! isRenamingMainType ( element , destination ) ) { JavaModelOperation deleteOp = new DeleteElementsOperation ( new IJavaElement [ ] { element } , this . force ) ; executeNestedOperation ( deleteOp , 1 ) ; } } private IJavaElement resolveRenameAnchor ( IJavaElement element ) throws JavaModelException { IParent parent = ( IParent ) element . getParent ( ) ; IJavaElement [ ] children = parent . getChildren ( ) ; for ( int i = 0 ; i < children . length ; i ++ ) { IJavaElement child = children [ i ] ; if ( child . equals ( element ) ) { return child ; } } return null ; } protected IJavaModelStatus verify ( ) { IJavaModelStatus status = super . verify ( ) ; if ( ! status . isOK ( ) ) { return status ; } if ( this . renamingsList != null && this . renamingsList . length != this . elementsToProcess . length ) { return new JavaModelStatus ( IJavaModelStatusConstants . INDEX_OUT_OF_BOUNDS ) ; } return JavaModelStatus . VERIFIED_OK ; } protected void verify ( IJavaElement element ) throws JavaModelException { if ( element == null || ! element . exists ( ) ) error ( IJavaModelStatusConstants . ELEMENT_DOES_NOT_EXIST , element ) ; if ( element . getElementType ( ) < IJavaElement . TYPE ) error ( IJavaModelStatusConstants . INVALID_ELEMENT_TYPES , element ) ; if ( element . isReadOnly ( ) ) error ( IJavaModelStatusConstants . READ_ONLY , element ) ; IJavaElement dest = getDestinationParent ( element ) ; verifyDestination ( element , dest ) ; verifySibling ( element , dest ) ; if ( this . renamingsList != null ) { verifyRenaming ( element ) ; } } } </s>