1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.core.resources.ResourcesPlugin; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.jdt.core.IClasspathContainer; 17 import org.eclipse.jdt.core.IClasspathEntry; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.internal.core.util.Util; 22 23 public class SetContainerOperation extends ChangeClasspathOperation { 24 25 IPath containerPath; 26 IJavaProject[] affectedProjects; 27 IClasspathContainer[] respectiveContainers; 28 29 32 public SetContainerOperation(IPath containerPath, IJavaProject[] affectedProjects, IClasspathContainer[] respectiveContainers) { 33 super(new IJavaElement[] {JavaModelManager.getJavaModelManager().getJavaModel()}, !ResourcesPlugin.getWorkspace().isTreeLocked()); 34 this.containerPath = containerPath; 35 this.affectedProjects = affectedProjects; 36 this.respectiveContainers = respectiveContainers; 37 } 38 39 protected void executeOperation() throws JavaModelException { 40 checkCanceled(); 41 try { 42 beginTask("", 1); if (JavaModelManager.CP_RESOLVE_VERBOSE) 44 verbose_set_container(); 45 if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED) 46 verbose_set_container_invocation_trace(); 47 48 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 49 if (manager.containerPutIfInitializingWithSameEntries(this.containerPath, this.affectedProjects, this.respectiveContainers)) 50 return; 51 52 final int projectLength = this.affectedProjects.length; 53 final IJavaProject[] modifiedProjects; 54 System.arraycopy(this.affectedProjects, 0, modifiedProjects = new IJavaProject[projectLength], 0, projectLength); 55 final IClasspathEntry[][] oldResolvedPaths = new IClasspathEntry[projectLength][]; 56 57 int remaining = 0; 59 for (int i = 0; i < projectLength; i++){ 60 if (isCanceled()) 61 return; 62 JavaProject affectedProject = (JavaProject) this.affectedProjects[i]; 63 IClasspathContainer newContainer = this.respectiveContainers[i]; 64 if (newContainer == null) newContainer = JavaModelManager.CONTAINER_INITIALIZATION_IN_PROGRESS; boolean found = false; 66 if (JavaProject.hasJavaNature(affectedProject.getProject())){ 67 IClasspathEntry[] rawClasspath = affectedProject.getRawClasspath(); 68 for (int j = 0, cpLength = rawClasspath.length; j <cpLength; j++) { 69 IClasspathEntry entry = rawClasspath[j]; 70 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().equals(this.containerPath)){ 71 found = true; 72 break; 73 } 74 } 75 } 76 if (!found) { 77 modifiedProjects[i] = null; manager.containerPut(affectedProject, this.containerPath, newContainer); 79 continue; 80 } 81 IClasspathContainer oldContainer = manager.containerGet(affectedProject, this.containerPath); 82 if (oldContainer == JavaModelManager.CONTAINER_INITIALIZATION_IN_PROGRESS) { 83 oldContainer = null; 84 } 85 if ((oldContainer != null && oldContainer.equals(this.respectiveContainers[i])) 86 || (oldContainer == this.respectiveContainers[i])) { 87 modifiedProjects[i] = null; continue; 89 } 90 remaining++; 91 oldResolvedPaths[i] = affectedProject.getResolvedClasspath(); 92 manager.containerPut(affectedProject, this.containerPath, newContainer); 93 } 94 95 if (remaining == 0) return; 96 97 try { 99 for(int i = 0; i < projectLength; i++){ 100 if (isCanceled()) 101 return; 102 103 JavaProject affectedProject = (JavaProject)modifiedProjects[i]; 104 if (affectedProject == null) continue; if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED) 106 verbose_update_project(affectedProject); 107 108 affectedProject.getPerProjectInfo().resetResolvedClasspath(); 110 111 classpathChanged(affectedProject); 113 114 if (this.canChangeResources) { 115 try { 117 affectedProject.getProject().touch(this.progressMonitor); 118 } catch (CoreException e) { 119 if (!ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(affectedProject.getElementName())) 121 throw e; 122 } 123 } 124 } 125 } catch(CoreException e) { 126 if (JavaModelManager.CP_RESOLVE_VERBOSE) 127 verbose_failure(e); 128 if (e instanceof JavaModelException) { 129 throw (JavaModelException)e; 130 } else { 131 throw new JavaModelException(e); 132 } 133 } finally { 134 for (int i = 0; i < projectLength; i++) { 135 if (this.respectiveContainers[i] == null) { 136 manager.containerPut(this.affectedProjects[i], this.containerPath, null); } 138 } 139 } 140 } finally { 141 done(); 142 } 143 } 144 145 private void verbose_failure(CoreException e) { 146 Util.verbose( 147 "CPContainer SET - FAILED DUE TO EXCEPTION\n" + " container path: " + this.containerPath, System.err); 150 e.printStackTrace(); 151 } 152 153 private void verbose_update_project(JavaProject affectedProject) { 154 Util.verbose( 155 "CPContainer SET - updating affected project due to setting container\n" + " project: " + affectedProject.getElementName() + '\n' + " container path: " + this.containerPath); } 159 160 private void verbose_set_container() { 161 Util.verbose( 162 "CPContainer SET - setting container\n" + " container path: " + this.containerPath + '\n' + " projects: {" + org.eclipse.jdt.internal.compiler.util.Util.toString( 166 this.affectedProjects, 167 new org.eclipse.jdt.internal.compiler.util.Util.Displayable(){ 168 public String displayString(Object o) { return ((IJavaProject) o).getElementName(); } 169 }) + 170 "}\n values: {\n" + org.eclipse.jdt.internal.compiler.util.Util.toString( 172 this.respectiveContainers, 173 new org.eclipse.jdt.internal.compiler.util.Util.Displayable(){ 174 public String displayString(Object o) { 175 StringBuffer buffer = new StringBuffer (" "); if (o == null) { 177 buffer.append("<null>"); return buffer.toString(); 179 } 180 IClasspathContainer container = (IClasspathContainer) o; 181 buffer.append(container.getDescription()); 182 buffer.append(" {\n"); IClasspathEntry[] entries = container.getClasspathEntries(); 184 if (entries != null){ 185 for (int i = 0; i < entries.length; i++){ 186 buffer.append(" "); buffer.append(entries[i]); 188 buffer.append('\n'); 189 } 190 } 191 buffer.append(" }"); return buffer.toString(); 193 } 194 }) + 195 "\n }"); } 197 198 private void verbose_set_container_invocation_trace() { 199 Util.verbose( 200 "CPContainer SET - setting container\n" + " invocation stack trace:"); new Exception ("<Fake exception>").printStackTrace(System.out); } 204 205 } 206 | Popular Tags |