private static ClassLoader getParentClassLoader() { return getClassLoader().getParent(); }
/** * Creates a classloader for testing purposes. The classloader loads the classes from the test jars. * * @param testJars * jar files to be loaded by this classloader. For all packages and classes contained in these jars this * classloader takes precedence over the parent classloader. * @param packages * additional packages for which this classloader should take precedence over the parent classloader */ public TestClassLoader(Collection<URL> testJars, Collection<String> packages, String... excludePackages) { super(getURLs(testJars), getParentClassLoader()); this.packages = Collections.newSetFromMap(new ConcurrentHashMap<>()); this.packages.addAll(packages); this.packages.addAll(new JarScanner().addJar(testJars).ignore(excludePackages).scanPackages()); this.blacklist = Collections.newSetFromMap(new ConcurrentHashMap<>()); this.blacklist.addAll(Arrays.asList(excludePackages)); }
/** * Creates the classpath from the collection of test URLs and the system classpath. The testjars listed on the * beginning of the classpath and therefore take precedence in the class loading order. * * @param testJars * the testjars to be prepended to the current classpath * * @return array of all the test jars and the system classpath */ private static URL[] getURLs(Collection<URL> testJars) { final List<URL> result = new ArrayList<>(); result.addAll(testJars); result.addAll(getParentJars()); return result.toArray(new URL[result.size()]); } }
private static Collection<URL> getParentJars() { return Arrays.asList(((URLClassLoader)getClassLoader()).getURLs()); }