1 11 package org.eclipse.jdt.launching; 12 13 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.jar.Attributes ; 20 import java.util.jar.JarFile ; 21 import java.util.jar.Manifest ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.debug.core.ILaunchConfiguration; 27 import org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry; 28 import org.eclipse.jdt.internal.launching.VariableClasspathEntry; 29 30 37 public class StandardSourcePathProvider extends StandardClasspathProvider { 38 39 42 public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException { 43 boolean useDefault = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH, true); 44 IRuntimeClasspathEntry[] entries = null; 45 if (useDefault) { 46 entries = super.computeUnresolvedClasspath(configuration); 48 } else { 49 entries = recoverRuntimePath(configuration, IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH); 51 } 52 return entries; 53 54 } 55 56 59 public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException { 60 List all = new UniqueList(entries.length); 61 for (int i = 0; i < entries.length; i++) { 62 switch (entries[i].getType()) { 63 case IRuntimeClasspathEntry.PROJECT: 64 all.add(entries[i]); 66 break; 67 case IRuntimeClasspathEntry.OTHER: 68 IRuntimeClasspathEntry2 entry = (IRuntimeClasspathEntry2)entries[i]; 69 String typeId = entry.getTypeId(); 70 IRuntimeClasspathEntry[] res = null; 71 if (typeId.equals(DefaultProjectClasspathEntry.TYPE_ID)) { 72 IRuntimeClasspathEntry[] children = entry.getRuntimeClasspathEntries(configuration); 74 res = JavaRuntime.resolveSourceLookupPath(children, configuration); 75 } else if (typeId.equals(VariableClasspathEntry.TYPE_ID)) { 76 res = JavaRuntime.resolveRuntimeClasspathEntry(entry, configuration); 78 } else { 79 res = JavaRuntime.resolveRuntimeClasspathEntry(entry, configuration); 80 } 81 if (res != null) { 82 for (int j = 0; j < res.length; j++) { 83 all.add(res[j]); 84 addManifestReferences(res[j], all); 85 } 86 } 87 break; 88 default: 89 IRuntimeClasspathEntry[] resolved =JavaRuntime.resolveRuntimeClasspathEntry(entries[i], configuration); 90 for (int j = 0; j < resolved.length; j++) { 91 all.add(resolved[j]); 92 addManifestReferences(resolved[j], all); 93 } 94 break; 95 } 96 } 97 return (IRuntimeClasspathEntry[])all.toArray(new IRuntimeClasspathEntry[all.size()]); 98 } 99 100 106 protected void addManifestReferences(IRuntimeClasspathEntry entry, List all) { 107 if (entry.getType() == IRuntimeClasspathEntry.ARCHIVE) { 108 String location = entry.getLocation(); 109 if (location != null) { 110 JarFile jar = null; 111 try { 112 jar = new JarFile (location, false); 113 Manifest manifest = jar.getManifest(); 114 if (manifest != null) { 115 Attributes mainAttributes = manifest.getMainAttributes(); 116 if (mainAttributes != null) { 117 String value = mainAttributes.getValue(Attributes.Name.CLASS_PATH); 118 if (value != null) { 119 String [] entries = value.split("\\s+"); IPath base = new Path(location); 121 base = base.removeLastSegments(1); 122 for (int i = 0; i < entries.length; i++) { 123 IPath path = base.append(entries[i]); 124 if (path.toFile().exists()) { 125 IRuntimeClasspathEntry ref = JavaRuntime.newArchiveRuntimeClasspathEntry(path); 126 if (!all.contains(ref)) { 127 all.add(ref); 128 } 129 } 130 } 131 } 132 } 133 } 134 } catch (IOException e) { 135 } finally { 136 if (jar != null) { 137 try { 138 jar.close(); 139 } catch (IOException e) { 140 } 141 } 142 } 143 } 144 } 145 } 146 147 151 class UniqueList extends ArrayList { 152 private static final long serialVersionUID = -7402160651027036270L; 153 HashSet set; 154 155 public UniqueList(int length) { 156 super(length); 157 set = new HashSet (length); 158 } 159 160 public void add(int index, Object element) { 161 if (set.add(element)) 162 super.add(index, element); 163 } 164 165 public boolean add(Object o) { 166 if (set.add(o)) 167 return super.add(o); 168 return false; 169 } 170 171 public boolean addAll(Collection c) { 172 if (set.addAll(c)) 173 return super.addAll(c); 174 return false; 175 } 176 177 public boolean addAll(int index, Collection c) { 178 if (set.addAll(c)) 179 return super.addAll(index, c); 180 return false; 181 } 182 183 public void clear() { 184 set.clear(); 185 super.clear(); 186 } 187 188 public boolean contains(Object elem) { 189 return set.contains(elem); 190 } 191 192 public void ensureCapacity(int minCapacity) { 193 super.ensureCapacity(minCapacity); 194 } 195 196 public Object remove(int index) { 197 Object object = super.remove(index); 198 set.remove(object); 199 return object; 200 } 201 202 protected void removeRange(int fromIndex, int toIndex) { 203 for (int index = fromIndex; index<=toIndex; index++) 204 remove(index); 205 } 206 207 public Object set(int index, Object element) { 208 set.remove(element); 209 if (set.add(element)) 210 return super.set(index, element); 211 return null; } 213 } 214 215 } 216 | Popular Tags |