<s> package org . eclipse . jdt . internal . core ; import java . util . HashSet ; import java . util . Iterator ; import org . eclipse . core . resources . IProject ; import org . eclipse . core . resources . IProjectDescription ; import org . eclipse . core . resources . IResource ; import org . eclipse . core . resources . IWorkspace ; import org . eclipse . core . resources . IWorkspaceRoot ; import org . eclipse . core . resources . IWorkspaceRunnable ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IProgressMonitor ; import org . eclipse . core . runtime . jobs . ISchedulingRule ; import org . eclipse . jdt . core . IClasspathEntry ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . internal . core . util . Util ; public class ProjectReferenceChange { private JavaProject project ; private IClasspathEntry [ ] oldResolvedClasspath ; public ProjectReferenceChange ( JavaProject project , IClasspathEntry [ ] oldResolvedClasspath ) { this . project = project ; this . oldResolvedClasspath = oldResolvedClasspath ; } public void updateProjectReferencesIfNecessary ( ) throws JavaModelException { String [ ] oldRequired = this . oldResolvedClasspath == null ? CharOperation . NO_STRINGS : this . project . projectPrerequisites ( this . oldResolvedClasspath ) ; IClasspathEntry [ ] newResolvedClasspath = this . project . getResolvedClasspath ( ) ; String [ ] newRequired = this . project . projectPrerequisites ( newResolvedClasspath ) ; final IProject projectResource = this . project . getProject ( ) ; try { IProject [ ] projectReferences = projectResource . getDescription ( ) . getDynamicReferences ( ) ; HashSet oldReferences = new HashSet ( projectReferences . length ) ; for ( int i = 0 ; i < projectReferences . length ; i ++ ) { String projectName = projectReferences [ i ] . getName ( ) ; oldReferences . add ( projectName ) ; } HashSet newReferences = ( HashSet ) oldReferences . clone ( ) ; for ( int i = 0 ; i < oldRequired . length ; i ++ ) { String projectName = oldRequired [ i ] ; newReferences . remove ( projectName ) ; } for ( int i = 0 ; i < newRequired . length ; i ++ ) { String projectName = newRequired [ i ] ; newReferences . add ( projectName ) ; } Iterator iter ; int newSize = newReferences . size ( ) ; checkIdentity : { if ( oldReferences . size ( ) == newSize ) { iter = newReferences . iterator ( ) ; while ( iter . hasNext ( ) ) { if ( ! oldReferences . contains ( iter . next ( ) ) ) { break checkIdentity ; } } return ; } } String [ ] requiredProjectNames = new String [ newSize ] ; int index = 0 ; iter = newReferences . iterator ( ) ; while ( iter . hasNext ( ) ) { requiredProjectNames [ index ++ ] = ( String ) iter . next ( ) ; } Util . sort ( requiredProjectNames ) ; final IProject [ ] requiredProjectArray = new IProject [ newSize ] ; IWorkspaceRoot wksRoot = projectResource . getWorkspace ( ) . getRoot ( ) ; for ( int i = 0 ; i < newSize ; i ++ ) { requiredProjectArray [ i ] = wksRoot . getProject ( requiredProjectNames [ i ] ) ; } IWorkspace workspace = projectResource . getWorkspace ( ) ; ISchedulingRule rule = workspace . getRuleFactory ( ) . modifyRule ( projectResource ) ; IWorkspaceRunnable runnable = new IWorkspaceRunnable ( ) { public void run ( IProgressMonitor monitor ) throws CoreException { IProjectDescription description = projectResource . getDescription ( ) ; description . setDynamicReferences ( requiredProjectArray ) ; projectResource . setDescription ( description , IResource . AVOID_NATURE_CONFIG , null ) ; } } ; workspace . run ( runnable , rule , IWorkspace . AVOID_UPDATE , null ) ; } catch ( CoreException e ) { if ( ! ExternalJavaProject . EXTERNAL_PROJECT_NAME . equals ( this . project . getElementName ( ) ) ) throw new JavaModelException ( e ) ; } } public String toString ( ) { return "" + this . project . getElementName ( ) ; } } </s>