1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.File ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.debug.core.ILaunchConfiguration; 25 import org.eclipse.jdt.core.IAccessRule; 26 import org.eclipse.jdt.core.IClasspathAttribute; 27 import org.eclipse.jdt.core.IClasspathEntry; 28 import org.eclipse.jdt.core.IJavaProject; 29 import org.eclipse.jdt.core.JavaCore; 30 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 31 import org.eclipse.jdt.launching.IRuntimeClasspathEntryResolver2; 32 import org.eclipse.jdt.launching.IVMInstall; 33 import org.eclipse.jdt.launching.JavaRuntime; 34 import org.eclipse.jdt.launching.LibraryLocation; 35 36 39 public class JRERuntimeClasspathEntryResolver implements IRuntimeClasspathEntryResolver2 { 40 41 private static IAccessRule[] EMPTY_RULES = new IAccessRule[0]; 42 43 46 public IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, ILaunchConfiguration configuration) throws CoreException { 47 IVMInstall jre = null; 48 if (entry.getType() == IRuntimeClasspathEntry.CONTAINER && entry.getPath().segmentCount() > 1) { 49 jre = JREContainerInitializer.resolveVM(entry.getPath()); 51 } else { 52 jre = JavaRuntime.computeVMInstall(configuration); 54 } 55 if (jre == null) { 56 return new IRuntimeClasspathEntry[0]; 58 } 59 return resolveLibraryLocations(jre, entry.getClasspathProperty()); 60 } 61 62 65 public IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, IJavaProject project) throws CoreException { 66 IVMInstall jre = null; 67 if (entry.getType() == IRuntimeClasspathEntry.CONTAINER && entry.getPath().segmentCount() > 1) { 68 jre = JREContainerInitializer.resolveVM(entry.getPath()); 70 } else { 71 jre = JavaRuntime.getVMInstall(project); 73 } 74 if (jre == null) { 75 return new IRuntimeClasspathEntry[0]; 77 } 78 return resolveLibraryLocations(jre, entry.getClasspathProperty()); 79 } 80 81 84 protected IRuntimeClasspathEntry[] resolveLibraryLocations(IVMInstall vm, int kind) { 85 LibraryLocation[] libs = vm.getLibraryLocations(); 86 LibraryLocation[] defaultLibs = vm.getVMInstallType().getDefaultLibraryLocations(vm.getInstallLocation()); 87 boolean overrideJavadoc = false; 88 if (libs == null) { 89 libs = defaultLibs; 91 overrideJavadoc = true; 92 } else if (!isSameArchives(libs, defaultLibs)) { 93 kind = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES; 95 } 96 if (kind == IRuntimeClasspathEntry.BOOTSTRAP_CLASSES) { 97 File vmInstallLocation= vm.getInstallLocation(); 98 if (vmInstallLocation != null) { 99 LibraryInfo libraryInfo= LaunchingPlugin.getLibraryInfo(vmInstallLocation.getAbsolutePath()); 100 if (libraryInfo != null) { 101 String [] extensionDirsArray = libraryInfo.getExtensionDirs(); 104 Set extensionDirsSet = new HashSet (); 105 for (int i = 0; i < extensionDirsArray.length; i++) { 106 extensionDirsSet.add(extensionDirsArray[i]); 107 } 108 List resolvedEntries = new ArrayList (libs.length); 109 for (int i = 0; i < libs.length; i++) { 110 LibraryLocation location = libs[i]; 111 IPath libraryPath = location.getSystemLibraryPath(); 112 String dir = libraryPath.toFile().getParent(); 113 if (!extensionDirsSet.contains(dir)) { 115 resolvedEntries.add(resolveLibraryLocation(vm, location, kind, overrideJavadoc)); 116 } 117 } 118 return (IRuntimeClasspathEntry[]) resolvedEntries.toArray(new IRuntimeClasspathEntry[resolvedEntries.size()]); 119 } 120 } 121 } 122 List resolvedEntries = new ArrayList (libs.length); 123 for (int i = 0; i < libs.length; i++) { 124 IPath systemLibraryPath = libs[i].getSystemLibraryPath(); 125 if (systemLibraryPath.toFile().exists()) { 126 resolvedEntries.add(resolveLibraryLocation(vm, libs[i], kind, overrideJavadoc)); 127 } 128 } 129 return (IRuntimeClasspathEntry[]) resolvedEntries.toArray(new IRuntimeClasspathEntry[resolvedEntries.size()]); 130 } 131 132 141 public static boolean isSameArchives(LibraryLocation[] libs, LibraryLocation[] defaultLibs) { 142 if (libs.length != defaultLibs.length) { 143 return false; 144 } 145 IPath dpath = null, lpath = null; 146 for (int i = 0; i < defaultLibs.length; i++) { 147 dpath = defaultLibs[i].getSystemLibraryPath(); 148 lpath = libs[i].getSystemLibraryPath(); 149 if(Platform.getOS().equals(Platform.OS_WIN32)) { 150 if (!dpath.removeTrailingSeparator().toOSString().equalsIgnoreCase(lpath.removeTrailingSeparator().toOSString())) { 152 return false; 153 } 154 } else if (!dpath.equals(lpath)) { 155 return false; 156 } 157 } 158 return true; 159 } 160 161 164 public IVMInstall resolveVMInstall(IClasspathEntry entry) { 165 switch (entry.getEntryKind()) { 166 case IClasspathEntry.CPE_VARIABLE: 167 if (entry.getPath().segment(0).equals(JavaRuntime.JRELIB_VARIABLE)) { 168 return JavaRuntime.getDefaultVMInstall(); 169 } 170 break; 171 case IClasspathEntry.CPE_CONTAINER: 172 if (entry.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) { 173 return JREContainerInitializer.resolveVM(entry.getPath()); 174 } 175 break; 176 default: 177 break; 178 } 179 return null; 180 } 181 182 185 public boolean isVMInstallReference(IClasspathEntry entry) { 186 switch (entry.getEntryKind()) { 187 case IClasspathEntry.CPE_VARIABLE: 188 if (entry.getPath().segment(0).equals(JavaRuntime.JRELIB_VARIABLE)) { 189 return true; 190 } 191 break; 192 case IClasspathEntry.CPE_CONTAINER: 193 if (entry.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) { 194 return true; 195 } 196 break; 197 default: 198 break; 199 } 200 return false; 201 } 202 203 212 private IRuntimeClasspathEntry resolveLibraryLocation(IVMInstall vm, LibraryLocation location, int kind, boolean overrideJavaDoc) { 213 IPath libraryPath = location.getSystemLibraryPath(); 214 URL javadocLocation = location.getJavadocLocation(); 215 if (overrideJavaDoc && javadocLocation == null) { 216 javadocLocation = vm.getJavadocLocation(); 217 } 218 IClasspathAttribute[] attributes = null; 219 if (javadocLocation == null) { 220 attributes = new IClasspathAttribute[0]; 221 } else { 222 attributes = new IClasspathAttribute[]{JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation.toExternalForm())}; 223 } 224 IClasspathEntry cpe = JavaCore.newLibraryEntry(libraryPath, location.getSystemLibraryPath(), location.getPackageRootPath(), EMPTY_RULES, attributes, false); 225 IRuntimeClasspathEntry resolved = new RuntimeClasspathEntry(cpe); 226 resolved.setClasspathProperty(kind); 227 IPath sourcePath = location.getSystemLibrarySourcePath(); 228 if (sourcePath != null && !sourcePath.isEmpty()) { 229 resolved.setSourceAttachmentPath(sourcePath); 230 resolved.setSourceAttachmentRootPath(location.getPackageRootPath()); 231 } 232 return resolved; 233 } 234 235 } 236 | Popular Tags |