-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathIsolatingClassLoader.java
More file actions
35 lines (31 loc) · 1.11 KB
/
Copy pathIsolatingClassLoader.java
File metadata and controls
35 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package jvmbootstraptest;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
/**
* Isolated {@link ClassLoader} that loads all non-JDK classes from class files instead of
* delegating.
*/
public class IsolatingClassLoader extends URLClassLoader {
public IsolatingClassLoader() {
super(discoverTestClassPath());
}
@Override
public Class<?> loadClass(final String className) throws ClassNotFoundException {
if (className.startsWith("java.")) {
return super.loadClass(className);
}
return findClass(className);
}
private static URL[] discoverTestClassPath() {
final String resourceName = IsolatingClassLoader.class.getName().replace(".", "/") + ".class";
final URL resource = Thread.currentThread().getContextClassLoader().getResource(resourceName);
final String testClassesURI = resource.toString().replace(resourceName, "");
System.out.println("Loading isolated classes from: " + testClassesURI);
try {
return new URL[] {new URI(testClassesURI).toURL()};
} catch (final Exception e) {
throw new IllegalStateException(e);
}
}
}