1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.jdt.core.IClasspathEntry; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaModel; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.JavaModelException; 24 import org.eclipse.jdt.internal.core.util.Util; 25 26 public class SetVariablesOperation extends ChangeClasspathOperation { 27 28 String [] variableNames; 29 IPath[] variablePaths; 30 boolean updatePreferences; 31 32 35 public SetVariablesOperation(String [] variableNames, IPath[] variablePaths, boolean updatePreferences) { 36 super(new IJavaElement[] {JavaModelManager.getJavaModelManager().getJavaModel()}, !ResourcesPlugin.getWorkspace().isTreeLocked()); 37 this.variableNames = variableNames; 38 this.variablePaths = variablePaths; 39 this.updatePreferences = updatePreferences; 40 } 41 42 protected void executeOperation() throws JavaModelException { 43 checkCanceled(); 44 try { 45 beginTask("", 1); if (JavaModelManager.CP_RESOLVE_VERBOSE) 47 verbose_set_variables(); 48 49 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 50 if (manager.variablePutIfInitializingWithSameValue(this.variableNames, this.variablePaths)) 51 return; 52 53 int varLength = this.variableNames.length; 54 55 final HashMap affectedProjectClasspaths = new HashMap (5); 57 IJavaModel model = getJavaModel(); 58 59 int discardCount = 0; 61 for (int i = 0; i < varLength; i++){ 62 String variableName = this.variableNames[i]; 63 IPath oldPath = manager.variableGet(variableName); if (oldPath == JavaModelManager.VARIABLE_INITIALIZATION_IN_PROGRESS) { 65 oldPath = null; } 67 if (oldPath != null && oldPath.equals(this.variablePaths[i])){ 68 this.variableNames[i] = null; 69 discardCount++; 70 } 71 } 72 if (discardCount > 0){ 73 if (discardCount == varLength) return; 74 int changedLength = varLength - discardCount; 75 String [] changedVariableNames = new String [changedLength]; 76 IPath[] changedVariablePaths = new IPath[changedLength]; 77 for (int i = 0, index = 0; i < varLength; i++){ 78 if (this.variableNames[i] != null){ 79 changedVariableNames[index] = this.variableNames[i]; 80 changedVariablePaths[index] = this.variablePaths[i]; 81 index++; 82 } 83 } 84 this.variableNames = changedVariableNames; 85 this.variablePaths = changedVariablePaths; 86 varLength = changedLength; 87 } 88 89 if (isCanceled()) 90 return; 91 92 IJavaProject[] projects = model.getJavaProjects(); 93 nextProject : for (int i = 0, projectLength = projects.length; i < projectLength; i++){ 94 JavaProject project = (JavaProject) projects[i]; 95 96 IClasspathEntry[] classpath = project.getRawClasspath(); 98 for (int j = 0, cpLength = classpath.length; j < cpLength; j++){ 99 100 IClasspathEntry entry = classpath[j]; 101 for (int k = 0; k < varLength; k++){ 102 103 String variableName = this.variableNames[k]; 104 if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE){ 105 106 if (variableName.equals(entry.getPath().segment(0))){ 107 affectedProjectClasspaths.put(project, project.getResolvedClasspath()); 108 continue nextProject; 109 } 110 IPath sourcePath, sourceRootPath; 111 if (((sourcePath = entry.getSourceAttachmentPath()) != null && variableName.equals(sourcePath.segment(0))) 112 || ((sourceRootPath = entry.getSourceAttachmentRootPath()) != null && variableName.equals(sourceRootPath.segment(0)))) { 113 114 affectedProjectClasspaths.put(project, project.getResolvedClasspath()); 115 continue nextProject; 116 } 117 } 118 } 119 } 120 } 121 122 for (int i = 0; i < varLength; i++){ 124 manager.variablePut(this.variableNames[i], this.variablePaths[i]); 125 if (this.updatePreferences) 126 manager.variablePreferencesPut(this.variableNames[i], this.variablePaths[i]); 127 } 128 129 if (!affectedProjectClasspaths.isEmpty()) { 131 String [] dbgVariableNames = this.variableNames; 132 try { 133 Iterator projectsToUpdate = affectedProjectClasspaths.keySet().iterator(); 135 while (projectsToUpdate.hasNext()) { 136 137 if (this.progressMonitor != null && this.progressMonitor.isCanceled()) return; 138 139 JavaProject affectedProject = (JavaProject) projectsToUpdate.next(); 140 141 if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED) 143 verbose_update_project(dbgVariableNames, affectedProject); 144 affectedProject.getPerProjectInfo().resetResolvedClasspath(); 145 146 classpathChanged(affectedProject); 148 149 if (this.canChangeResources) { 150 affectedProject.getProject().touch(this.progressMonitor); 152 } 153 } 154 } catch (CoreException e) { 155 if (JavaModelManager.CP_RESOLVE_VERBOSE){ 156 verbose_failure(dbgVariableNames); 157 e.printStackTrace(); 158 } 159 if (e instanceof JavaModelException) { 160 throw (JavaModelException)e; 161 } else { 162 throw new JavaModelException(e); 163 } 164 } 165 } 166 } finally { 167 done(); 168 } 169 } 170 171 private void verbose_failure(String [] dbgVariableNames) { 172 Util.verbose( 173 "CPVariable SET - FAILED DUE TO EXCEPTION\n" + " variables: " + org.eclipse.jdt.internal.compiler.util.Util.toString(dbgVariableNames), System.err); 176 } 177 178 private void verbose_update_project(String [] dbgVariableNames, 179 JavaProject affectedProject) { 180 Util.verbose( 181 "CPVariable SET - updating affected project due to setting variables\n" + " project: " + affectedProject.getElementName() + '\n' + " variables: " + org.eclipse.jdt.internal.compiler.util.Util.toString(dbgVariableNames)); } 185 186 private void verbose_set_variables() { 187 Util.verbose( 188 "CPVariable SET - setting variables\n" + " variables: " + org.eclipse.jdt.internal.compiler.util.Util.toString(this.variableNames) + '\n' + " values: " + org.eclipse.jdt.internal.compiler.util.Util.toString(this.variablePaths)); } 192 193 } 194 | Popular Tags |