<s> package org . eclipse . jdt . internal . core ; import java . util . HashMap ; import java . util . Map ; import org . eclipse . core . runtime . IStatus ; import org . eclipse . jdt . core . * ; public abstract class MultiOperation extends JavaModelOperation { protected Map insertBeforeElements = new HashMap ( 1 ) ; protected Map newParents ; protected Map renamings ; protected String [ ] renamingsList = null ; protected MultiOperation ( IJavaElement [ ] elementsToProcess , boolean force ) { super ( elementsToProcess , force ) ; } protected MultiOperation ( IJavaElement [ ] elementsToProcess , IJavaElement [ ] parentElements , boolean force ) { super ( elementsToProcess , parentElements , force ) ; this . newParents = new HashMap ( elementsToProcess . length ) ; if ( elementsToProcess . length == parentElements . length ) { for ( int i = 0 ; i < elementsToProcess . length ; i ++ ) { this . newParents . put ( elementsToProcess [ i ] , parentElements [ i ] ) ; } } else { for ( int i = 0 ; i < elementsToProcess . length ; i ++ ) { this . newParents . put ( elementsToProcess [ i ] , parentElements [ 0 ] ) ; } } } protected void error ( int code , IJavaElement element ) throws JavaModelException { throw new JavaModelException ( new JavaModelStatus ( code , element ) ) ; } protected void executeOperation ( ) throws JavaModelException { processElements ( ) ; } protected IJavaElement getDestinationParent ( IJavaElement child ) { return ( IJavaElement ) this . newParents . get ( child ) ; } protected abstract String getMainTaskName ( ) ; protected String getNewNameFor ( IJavaElement element ) throws JavaModelException { String newName = null ; if ( this . renamings != null ) newName = ( String ) this . renamings . get ( element ) ; if ( newName == null && element instanceof IMethod && ( ( IMethod ) element ) . isConstructor ( ) ) newName = getDestinationParent ( element ) . getElementName ( ) ; return newName ; } private void initializeRenamings ( ) { if ( this . renamingsList != null && this . renamingsList . length == this . elementsToProcess . length ) { this . renamings = new HashMap ( this . renamingsList . length ) ; for ( int i = 0 ; i < this . renamingsList . length ; i ++ ) { if ( this . renamingsList [ i ] != null ) { this . renamings . put ( this . elementsToProcess [ i ] , this . renamingsList [ i ] ) ; } } } } protected boolean isMove ( ) { return false ; } protected boolean isRename ( ) { return false ; } protected abstract void processElement ( IJavaElement element ) throws JavaModelException ; protected void processElements ( ) throws JavaModelException { try { beginTask ( getMainTaskName ( ) , this . elementsToProcess . length ) ; IJavaModelStatus [ ] errors = new IJavaModelStatus [ 3 ] ; int errorsCounter = 0 ; for ( int i = 0 ; i < this . elementsToProcess . length ; i ++ ) { try { verify ( this . elementsToProcess [ i ] ) ; processElement ( this . elementsToProcess [ i ] ) ; } catch ( JavaModelException jme ) { if ( errorsCounter == errors . length ) { System . arraycopy ( errors , 0 , ( errors = new IJavaModelStatus [ errorsCounter * 2 ] ) , 0 , errorsCounter ) ; } errors [ errorsCounter ++ ] = jme . getJavaModelStatus ( ) ; } finally { worked ( 1 ) ; } } if ( errorsCounter == 1 ) { throw new JavaModelException ( errors [ 0 ] ) ; } else if ( errorsCounter > 1 ) { if ( errorsCounter != errors . length ) { System . arraycopy ( errors , 0 , ( errors = new IJavaModelStatus [ errorsCounter ] ) , 0 , errorsCounter ) ; } throw new JavaModelException ( JavaModelStatus . newMultiStatus ( errors ) ) ; } } finally { done ( ) ; } } public void setInsertBefore ( IJavaElement modifiedElement , IJavaElement newSibling ) { this . insertBeforeElements . put ( modifiedElement , newSibling ) ; } public void setRenamings ( String [ ] renamingsList ) { this . renamingsList = renamingsList ; initializeRenamings ( ) ; } protected abstract void verify ( IJavaElement element ) throws JavaModelException ; protected void verifyDestination ( IJavaElement element , IJavaElement destination ) throws JavaModelException { if ( destination == null || ! destination . exists ( ) ) error ( IJavaModelStatusConstants . ELEMENT_DOES_NOT_EXIST , destination ) ; int destType = destination . getElementType ( ) ; switch ( element . getElementType ( ) ) { case IJavaElement . PACKAGE_DECLARATION : case IJavaElement . IMPORT_DECLARATION : if ( destType != IJavaElement . COMPILATION_UNIT ) error ( IJavaModelStatusConstants . INVALID_DESTINATION , element ) ; break ; case IJavaElement . TYPE : if ( destType != IJavaElement . COMPILATION_UNIT && destType != IJavaElement . TYPE ) error ( IJavaModelStatusConstants . INVALID_DESTINATION , element ) ; break ; case IJavaElement . METHOD : case IJavaElement . FIELD : case IJavaElement . INITIALIZER : if ( destType != IJavaElement . TYPE || destination instanceof BinaryType ) error ( IJavaModelStatusConstants . INVALID_DESTINATION , element ) ; break ; case IJavaElement . COMPILATION_UNIT : if ( destType != IJavaElement . PACKAGE_FRAGMENT ) error ( IJavaModelStatusConstants . INVALID_DESTINATION , element ) ; else { CompilationUnit cu = ( CompilationUnit ) element ; if ( isMove ( ) && cu . isWorkingCopy ( ) && ! cu . isPrimary ( ) ) error ( IJavaModelStatusConstants . INVALID_ELEMENT_TYPES , element ) ; } break ; case IJavaElement . PACKAGE_FRAGMENT : IPackageFragment fragment = ( IPackageFragment ) element ; IJavaElement parent = fragment . getParent ( ) ; if ( parent . isReadOnly ( ) ) error ( IJavaModelStatusConstants . READ_ONLY , element ) ; else if ( destType != IJavaElement . PACKAGE_FRAGMENT_ROOT ) error ( IJavaModelStatusConstants . INVALID_DESTINATION , element ) ; break ; default : error ( IJavaModelStatusConstants . INVALID_ELEMENT_TYPES , element ) ; } } protected void verifyRenaming ( IJavaElement element ) throws JavaModelException { String newName = getNewNameFor ( element ) ; boolean isValid = true ; IJavaProject project = element . getJavaProject ( ) ; String sourceLevel = project . getOption ( JavaCore . COMPILER_SOURCE , true ) ; String complianceLevel = project . getOption ( JavaCore . COMPILER_COMPLIANCE , true ) ; switch ( element . getElementType ( ) ) { case IJavaElement . PACKAGE_FRAGMENT : if ( ( ( IPackageFragment ) element ) . isDefaultPackage ( ) ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . NAME_COLLISION , element ) ) ; } isValid = JavaConventions . validatePackageName ( newName , sourceLevel , complianceLevel ) . getSeverity ( ) != IStatus . ERROR ; break ; case IJavaElement . COMPILATION_UNIT : isValid = JavaConventions . validateCompilationUnitName ( newName , sourceLevel , complianceLevel ) . getSeverity ( ) != IStatus . ERROR ; break ; case IJavaElement . INITIALIZER : isValid = false ; break ; default : isValid = JavaConventions . validateIdentifier ( newName , sourceLevel , complianceLevel ) . getSeverity ( ) != IStatus . ERROR ; break ; } if ( ! isValid ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INVALID_NAME , element , newName ) ) ; } } protected void verifySibling ( IJavaElement element , IJavaElement destination ) throws JavaModelException { IJavaElement insertBeforeElement = ( IJavaElement ) this . insertBeforeElements . get ( element ) ; if ( insertBeforeElement != null ) { if ( ! insertBeforeElement . exists ( ) || ! insertBeforeElement . getParent ( ) . equals ( destination ) ) { error ( IJavaModelStatusConstants . INVALID_SIBLING , insertBeforeElement ) ; } } } } </s>