1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IProjectDescription; 18 import org.eclipse.core.resources.IWorkspaceRoot; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.jdt.core.IClasspathEntry; 21 import org.eclipse.jdt.core.JavaModelException; 22 import org.eclipse.jdt.core.compiler.CharOperation; 23 import org.eclipse.jdt.internal.core.util.Util; 24 25 public class ProjectReferenceChange { 26 27 private JavaProject project; 28 private IClasspathEntry[] oldResolvedClasspath; 29 30 public ProjectReferenceChange(JavaProject project, IClasspathEntry[] oldResolvedClasspath) { 31 this.project = project; 32 this.oldResolvedClasspath = oldResolvedClasspath; 33 } 34 35 38 public void updateProjectReferencesIfNecessary() throws JavaModelException { 39 40 String [] oldRequired = this.oldResolvedClasspath == null ? CharOperation.NO_STRINGS : this.project.projectPrerequisites(this.oldResolvedClasspath); 41 IClasspathEntry[] newResolvedClasspath = this.project.getResolvedClasspath(); 42 String [] newRequired = this.project.projectPrerequisites(newResolvedClasspath); 43 try { 44 IProject projectResource = this.project.getProject(); 45 IProjectDescription description = projectResource.getDescription(); 46 47 IProject[] projectReferences = description.getDynamicReferences(); 48 49 HashSet oldReferences = new HashSet (projectReferences.length); 50 for (int i = 0; i < projectReferences.length; i++){ 51 String projectName = projectReferences[i].getName(); 52 oldReferences.add(projectName); 53 } 54 HashSet newReferences = (HashSet )oldReferences.clone(); 55 56 for (int i = 0; i < oldRequired.length; i++){ 57 String projectName = oldRequired[i]; 58 newReferences.remove(projectName); 59 } 60 for (int i = 0; i < newRequired.length; i++){ 61 String projectName = newRequired[i]; 62 newReferences.add(projectName); 63 } 64 65 Iterator iter; 66 int newSize = newReferences.size(); 67 68 checkIdentity: { 69 if (oldReferences.size() == newSize){ 70 iter = newReferences.iterator(); 71 while (iter.hasNext()){ 72 if (!oldReferences.contains(iter.next())){ 73 break checkIdentity; 74 } 75 } 76 return; 77 } 78 } 79 String [] requiredProjectNames = new String [newSize]; 80 int index = 0; 81 iter = newReferences.iterator(); 82 while (iter.hasNext()){ 83 requiredProjectNames[index++] = (String )iter.next(); 84 } 85 Util.sort(requiredProjectNames); 87 IProject[] requiredProjectArray = new IProject[newSize]; 88 IWorkspaceRoot wksRoot = projectResource.getWorkspace().getRoot(); 89 for (int i = 0; i < newSize; i++){ 90 requiredProjectArray[i] = wksRoot.getProject(requiredProjectNames[i]); 91 } 92 description.setDynamicReferences(requiredProjectArray); 93 projectResource.setDescription(description, null); 94 95 } catch(CoreException e){ 96 if (!ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(this.project.getElementName())) 97 throw new JavaModelException(e); 98 } 99 } 100 public String toString() { 101 return "ProjectRefenceChange: " + this.project.getElementName(); } 103 } 104 | Popular Tags |