// Derived from http://stackoverflow.com/a/3054692/250076 public static File getRelativeFile(File targetFile, File baseFile) { try { targetFile = targetFile.getCanonicalFile(); baseFile = baseFile.getCanonicalFile(); } catch (IOException ignored) { } String pathSeparator = File.separator; String basePath = baseFile.getAbsolutePath(); String normalizedTargetPath = normalize(targetFile.getAbsolutePath(), pathSeparator); String normalizedBasePath = normalize(basePath, pathSeparator); String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator)); String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator)); StringBuilder common = new StringBuilder(); int commonIndex = 0; while (commonIndex < target.length && commonIndex < base.length && target[commonIndex].equals(base[commonIndex])) { common.append(target[commonIndex]).append(pathSeparator); commonIndex++; } if (commonIndex == 0) { throw new Error( "No common path element found for '" + normalizedTargetPath + "' and '" + normalizedBasePath + '\''); } boolean baseIsFile = true; if (baseFile.exists()) { baseIsFile = baseFile.isFile(); } else if (basePath.endsWith(pathSeparator)) { baseIsFile = false; } StringBuilder relative = new StringBuilder(); if (base.length != commonIndex) { int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex; for (int i = 0; i < numDirsUp; i++) { relative.append("..").append(pathSeparator); } } relative.append(normalizedTargetPath.substring(common.length())); return new File(relative.toString()); }
@Nullable static ClassLoader createPluginClassLoader( @NotNull File[] classPath, @NotNull ClassLoader[] parentLoaders, @NotNull IdeaPluginDescriptor pluginDescriptor) { if (pluginDescriptor.getUseIdeaClassLoader()) { try { final ClassLoader loader = PluginManagerCore.class.getClassLoader(); final Method addUrlMethod = getAddUrlMethod(loader); for (File aClassPath : classPath) { final File file = aClassPath.getCanonicalFile(); addUrlMethod.invoke(loader, file.toURI().toURL()); } return loader; } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } PluginId pluginId = pluginDescriptor.getPluginId(); File pluginRoot = pluginDescriptor.getPath(); // if (classPath.length == 0) return null; if (isUnitTestMode()) return null; try { final List<URL> urls = new ArrayList<URL>(classPath.length); for (File aClassPath : classPath) { final File file = aClassPath .getCanonicalFile(); // it is critical not to have "." and ".." in classpath // elements urls.add(file.toURI().toURL()); } return new PluginClassLoader( urls, parentLoaders, pluginId, pluginDescriptor.getVersion(), pluginRoot); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }