<s> package org . eclipse . jdt . internal . core ; import java . util . HashMap ; import java . util . Iterator ; import org . eclipse . core . resources . ResourcesPlugin ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IPath ; import org . eclipse . jdt . core . IClasspathEntry ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IJavaModel ; import org . eclipse . jdt . core . IJavaProject ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . internal . core . util . Util ; public class SetVariablesOperation extends ChangeClasspathOperation { String [ ] variableNames ; IPath [ ] variablePaths ; boolean updatePreferences ; public SetVariablesOperation ( String [ ] variableNames , IPath [ ] variablePaths , boolean updatePreferences ) { super ( new IJavaElement [ ] { JavaModelManager . getJavaModelManager ( ) . getJavaModel ( ) } , ! ResourcesPlugin . getWorkspace ( ) . isTreeLocked ( ) ) ; this . variableNames = variableNames ; this . variablePaths = variablePaths ; this . updatePreferences = updatePreferences ; } protected void executeOperation ( ) throws JavaModelException { checkCanceled ( ) ; try { beginTask ( "" , 1 ) ; if ( JavaModelManager . CP_RESOLVE_VERBOSE ) verbose_set_variables ( ) ; JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; if ( manager . variablePutIfInitializingWithSameValue ( this . variableNames , this . variablePaths ) ) return ; int varLength = this . variableNames . length ; final HashMap affectedProjectClasspaths = new HashMap ( 5 ) ; IJavaModel model = getJavaModel ( ) ; int discardCount = 0 ; for ( int i = 0 ; i < varLength ; i ++ ) { String variableName = this . variableNames [ i ] ; IPath oldPath = manager . variableGet ( variableName ) ; if ( oldPath == JavaModelManager . VARIABLE_INITIALIZATION_IN_PROGRESS ) { oldPath = null ; } if ( oldPath != null && oldPath . equals ( this . variablePaths [ i ] ) ) { this . variableNames [ i ] = null ; discardCount ++ ; } } if ( discardCount > 0 ) { if ( discardCount == varLength ) return ; int changedLength = varLength - discardCount ; String [ ] changedVariableNames = new String [ changedLength ] ; IPath [ ] changedVariablePaths = new IPath [ changedLength ] ; for ( int i = 0 , index = 0 ; i < varLength ; i ++ ) { if ( this . variableNames [ i ] != null ) { changedVariableNames [ index ] = this . variableNames [ i ] ; changedVariablePaths [ index ] = this . variablePaths [ i ] ; index ++ ; } } this . variableNames = changedVariableNames ; this . variablePaths = changedVariablePaths ; varLength = changedLength ; } if ( isCanceled ( ) ) return ; IJavaProject [ ] projects = model . getJavaProjects ( ) ; nextProject : for ( int i = 0 , projectLength = projects . length ; i < projectLength ; i ++ ) { JavaProject project = ( JavaProject ) projects [ i ] ; IClasspathEntry [ ] classpath = project . getRawClasspath ( ) ; for ( int j = 0 , cpLength = classpath . length ; j < cpLength ; j ++ ) { IClasspathEntry entry = classpath [ j ] ; for ( int k = 0 ; k < varLength ; k ++ ) { String variableName = this . variableNames [ k ] ; if ( entry . getEntryKind ( ) == IClasspathEntry . CPE_VARIABLE ) { if ( variableName . equals ( entry . getPath ( ) . segment ( 0 ) ) ) { affectedProjectClasspaths . put ( project , project . getResolvedClasspath ( ) ) ; continue nextProject ; } IPath sourcePath , sourceRootPath ; if ( ( ( sourcePath = entry . getSourceAttachmentPath ( ) ) != null && variableName . equals ( sourcePath . segment ( 0 ) ) ) || ( ( sourceRootPath = entry . getSourceAttachmentRootPath ( ) ) != null && variableName . equals ( sourceRootPath . segment ( 0 ) ) ) ) { affectedProjectClasspaths . put ( project , project . getResolvedClasspath ( ) ) ; continue nextProject ; } } } } } for ( int i = 0 ; i < varLength ; i ++ ) { manager . variablePut ( this . variableNames [ i ] , this . variablePaths [ i ] ) ; if ( this . updatePreferences ) manager . variablePreferencesPut ( this . variableNames [ i ] , this . variablePaths [ i ] ) ; } if ( ! affectedProjectClasspaths . isEmpty ( ) ) { String [ ] dbgVariableNames = this . variableNames ; try { Iterator projectsToUpdate = affectedProjectClasspaths . keySet ( ) . iterator ( ) ; while ( projectsToUpdate . hasNext ( ) ) { if ( this . progressMonitor != null && this . progressMonitor . isCanceled ( ) ) return ; JavaProject affectedProject = ( JavaProject ) projectsToUpdate . next ( ) ; if ( JavaModelManager . CP_RESOLVE_VERBOSE_ADVANCED ) verbose_update_project ( dbgVariableNames , affectedProject ) ; ClasspathChange classpathChange = affectedProject . getPerProjectInfo ( ) . resetResolvedClasspath ( ) ; classpathChanged ( classpathChange , true ) ; if ( this . canChangeResources ) { affectedProject . getProject ( ) . touch ( this . progressMonitor ) ; } } } catch ( CoreException e ) { if ( JavaModelManager . CP_RESOLVE_VERBOSE || JavaModelManager . CP_RESOLVE_VERBOSE_FAILURE ) { verbose_failure ( dbgVariableNames ) ; e . printStackTrace ( ) ; } if ( e instanceof JavaModelException ) { throw ( JavaModelException ) e ; } else { throw new JavaModelException ( e ) ; } } } } finally { done ( ) ; } } private void verbose_failure ( String [ ] dbgVariableNames ) { Util . verbose ( "" + "tvariables: " + org . eclipse . jdt . internal . compiler . util . Util . toString ( dbgVariableNames ) , System . err ) ; } private void verbose_update_project ( String [ ] dbgVariableNames , JavaProject affectedProject ) { Util . verbose ( "" + "tproject: " + affectedProject . getElementName ( ) + '\n' + "tvariables: " + org . eclipse . jdt . internal . compiler . util . Util . toString ( dbgVariableNames ) ) ; } private void verbose_set_variables ( ) { Util . verbose ( "" + "tvariables: " + org . eclipse . jdt . internal . compiler . util . Util . toString ( this . variableNames ) + '\n' + "tvalues: " + org . eclipse . jdt . internal . compiler . util . Util . toString ( this . variablePaths ) ) ; } } </s>