1 11 package org.eclipse.jdt.internal.launching; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.debug.core.ILaunchConfiguration; 24 import org.eclipse.jdt.core.ClasspathContainerInitializer; 25 import org.eclipse.jdt.core.IClasspathContainer; 26 import org.eclipse.jdt.core.IClasspathEntry; 27 import org.eclipse.jdt.core.IJavaProject; 28 import org.eclipse.jdt.core.JavaCore; 29 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 30 import org.eclipse.jdt.launching.IRuntimeContainerComparator; 31 import org.eclipse.jdt.launching.JavaRuntime; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 35 38 public class DefaultProjectClasspathEntry extends AbstractRuntimeClasspathEntry { 39 40 public static final String TYPE_ID = "org.eclipse.jdt.launching.classpathentry.defaultClasspath"; 42 46 private boolean fExportedEntriesOnly = false; 47 48 51 public DefaultProjectClasspathEntry() { 52 } 53 54 59 public DefaultProjectClasspathEntry(IJavaProject project) { 60 setJavaProject(project); 61 } 62 63 66 protected void buildMemento(Document document, Element memento) throws CoreException { 67 memento.setAttribute("project", getJavaProject().getElementName()); memento.setAttribute("exportedEntriesOnly", Boolean.toString(fExportedEntriesOnly)); } 70 71 74 public void initializeFrom(Element memento) throws CoreException { 75 String name = memento.getAttribute("project"); if (name == null) { 77 abort(LaunchingMessages.DefaultProjectClasspathEntry_3, null); 78 } 79 IJavaProject project = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(name)); 80 setJavaProject(project); 81 name = memento.getAttribute("exportedEntriesOnly"); if (name == null) { 83 fExportedEntriesOnly = false; 84 } else { 85 fExportedEntriesOnly = Boolean.valueOf(name).booleanValue(); 86 } 87 } 88 91 public String getTypeId() { 92 return TYPE_ID; 93 } 94 97 public int getType() { 98 return OTHER; 99 } 100 101 protected IProject getProject() { 102 return getJavaProject().getProject(); 103 } 104 105 108 public String getLocation() { 109 return getProject().getLocation().toOSString(); 110 } 111 112 115 public IPath getPath() { 116 return getProject().getFullPath(); 117 } 118 119 122 public IResource getResource() { 123 return getProject(); 124 } 125 126 129 public IRuntimeClasspathEntry[] getRuntimeClasspathEntries(ILaunchConfiguration configuration) throws CoreException { 130 IClasspathEntry entry = JavaCore.newProjectEntry(getJavaProject().getProject().getFullPath()); 131 List classpathEntries = new ArrayList (5); 132 List expanding = new ArrayList (5); 133 expandProject(entry, classpathEntries, expanding); 134 IRuntimeClasspathEntry[] runtimeEntries = new IRuntimeClasspathEntry[classpathEntries.size()]; 135 for (int i = 0; i < runtimeEntries.length; i++) { 136 Object e = classpathEntries.get(i); 137 if (e instanceof IClasspathEntry) { 138 IClasspathEntry cpe = (IClasspathEntry)e; 139 runtimeEntries[i] = new RuntimeClasspathEntry(cpe); 140 } else { 141 runtimeEntries[i] = (IRuntimeClasspathEntry)e; 142 } 143 } 144 List ordered = new ArrayList (runtimeEntries.length); 146 for (int i = 0; i < runtimeEntries.length; i++) { 147 if (runtimeEntries[i].getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) { 148 ordered.add(runtimeEntries[i]); 149 } 150 } 151 return (IRuntimeClasspathEntry[]) ordered.toArray(new IRuntimeClasspathEntry[ordered.size()]); 152 } 153 154 165 private void expandProject(IClasspathEntry projectEntry, List expandedPath, List expanding) throws CoreException { 166 expanding.add(projectEntry); 167 IPath projectPath = projectEntry.getPath(); 170 IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(projectPath.lastSegment()); 171 if (res == null) { 172 expandedPath.add(projectEntry); 174 return; 175 } 176 IJavaProject project = (IJavaProject)JavaCore.create(res); 177 if (project == null || !project.getProject().isOpen() || !project.exists()) { 178 expandedPath.add(projectEntry); 180 return; 181 } 182 183 IClasspathEntry[] buildPath = project.getRawClasspath(); 184 List unexpandedPath = new ArrayList (buildPath.length); 185 boolean projectAdded = false; 186 for (int i = 0; i < buildPath.length; i++) { 187 IClasspathEntry classpathEntry = buildPath[i]; 188 if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 189 if (!projectAdded) { 190 projectAdded = true; 191 unexpandedPath.add(projectEntry); 192 } 193 } else { 194 if (classpathEntry.isExported()) { 196 unexpandedPath.add(classpathEntry); 197 } else if (!isExportedEntriesOnly() || project.equals(getJavaProject())) { 198 unexpandedPath.add(classpathEntry); 200 } 201 } 202 } 203 Iterator iter = unexpandedPath.iterator(); 206 while (iter.hasNext()) { 207 IClasspathEntry entry = (IClasspathEntry)iter.next(); 208 if (entry == projectEntry) { 209 expandedPath.add(entry); 210 } else { 211 switch (entry.getEntryKind()) { 212 case IClasspathEntry.CPE_PROJECT: 213 if (!expanding.contains(entry)) { 214 expandProject(entry, expandedPath, expanding); 215 } 216 break; 217 case IClasspathEntry.CPE_CONTAINER: 218 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); 219 int property = -1; 220 if (container != null) { 221 switch (container.getKind()) { 222 case IClasspathContainer.K_APPLICATION: 223 property = IRuntimeClasspathEntry.USER_CLASSES; 224 break; 225 case IClasspathContainer.K_DEFAULT_SYSTEM: 226 property = IRuntimeClasspathEntry.STANDARD_CLASSES; 227 break; 228 case IClasspathContainer.K_SYSTEM: 229 property = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES; 230 break; 231 } 232 IRuntimeClasspathEntry r = JavaRuntime.newRuntimeContainerClasspathEntry(entry.getPath(), property, project); 233 boolean duplicate = false; 235 ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(r.getPath().segment(0)); 236 for (int i = 0; i < expandedPath.size(); i++) { 237 Object o = expandedPath.get(i); 238 if (o instanceof IRuntimeClasspathEntry) { 239 IRuntimeClasspathEntry re = (IRuntimeClasspathEntry)o; 240 if (re.getType() == IRuntimeClasspathEntry.CONTAINER) { 241 if (container instanceof IRuntimeContainerComparator) { 242 duplicate = ((IRuntimeContainerComparator)container).isDuplicate(re.getPath()); 243 } else { 244 ClasspathContainerInitializer initializer2 = JavaCore.getClasspathContainerInitializer(re.getPath().segment(0)); 245 Object id1 = null; 246 Object id2 = null; 247 if (initializer == null) { 248 id1 = r.getPath().segment(0); 249 } else { 250 id1 = initializer.getComparisonID(r.getPath(), project); 251 } 252 if (initializer2 == null) { 253 id2 = re.getPath().segment(0); 254 } else { 255 IJavaProject context = re.getJavaProject(); 256 if (context == null) { 257 context = project; 258 } 259 id2 = initializer2.getComparisonID(re.getPath(), context); 260 } 261 if (id1 == null) { 262 duplicate = id2 == null; 263 } else { 264 duplicate = id1.equals(id2); 265 } 266 } 267 if (duplicate) { 268 break; 269 } 270 } 271 } 272 } 273 if (!duplicate) { 274 expandedPath.add(r); 275 } 276 } 277 break; 278 case IClasspathEntry.CPE_VARIABLE: 279 if (entry.getPath().segment(0).equals(JavaRuntime.JRELIB_VARIABLE)) { 280 IRuntimeClasspathEntry r = JavaRuntime.newVariableRuntimeClasspathEntry(entry.getPath()); 281 r.setSourceAttachmentPath(entry.getSourceAttachmentPath()); 282 r.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath()); 283 r.setClasspathProperty(IRuntimeClasspathEntry.STANDARD_CLASSES); 284 if (!expandedPath.contains(r)) { 285 expandedPath.add(r); 286 } 287 break; 288 } 289 default: 291 if (!expandedPath.contains(entry)) { 292 expandedPath.add(entry); 293 } 294 break; 295 } 296 } 297 } 298 return; 299 } 300 303 public boolean isComposite() { 304 return true; 305 } 306 309 public String getName() { 310 if (isExportedEntriesOnly()) { 311 return MessageFormat.format(LaunchingMessages.DefaultProjectClasspathEntry_2, new String [] {getJavaProject().getElementName()}); 312 } 313 return MessageFormat.format(LaunchingMessages.DefaultProjectClasspathEntry_4, new String [] {getJavaProject().getElementName()}); 314 } 315 318 public boolean equals(Object obj) { 319 if (obj instanceof DefaultProjectClasspathEntry) { 320 DefaultProjectClasspathEntry entry = (DefaultProjectClasspathEntry) obj; 321 return entry.getJavaProject().equals(getJavaProject()) && 322 entry.isExportedEntriesOnly() == isExportedEntriesOnly(); 323 } 324 return false; 325 } 326 329 public int hashCode() { 330 return getJavaProject().hashCode(); 331 } 332 333 340 public void setExportedEntriesOnly(boolean exportedOnly) { 341 fExportedEntriesOnly = exportedOnly; 342 } 343 344 351 public boolean isExportedEntriesOnly() { 352 return fExportedEntriesOnly; 353 } 354 } 355 | Popular Tags |