Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit 172a3d2

Browse files
committed
Fix classloader usage to support Java 9
The changes here include: * don't assume the system classloader is a URLClassLoader (it isn't under Java 9) * insert a classloader we control into the loader chain so we can have one to modify * use our own subclass of URLClassLoader that exposes .addURL, so we can modify it (we can't call .setAccessible on .addURL under Java 9) Fixes #525
1 parent 91e3012 commit 172a3d2

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Boot.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// vim: et:ts=4:sw=4
22

3+
import boot.bin.ParentClassLoader;
4+
35
import java.io.*;
46
import java.net.*;
57
import java.util.*;
@@ -9,14 +11,15 @@
911
import java.util.regex.Pattern;
1012
import java.lang.reflect.Method;
1113
import static java.nio.file.StandardCopyOption.*;
12-
14+
1315
@SuppressWarnings("unchecked")
1416
public class Boot {
1517

16-
public static final String initialVersion = "2.5.2";
17-
public static final File homedir = new File(System.getProperty("user.home"));
18-
public static final File workdir = new File(System.getProperty("user.dir"));
19-
18+
public static final String initialVersion = "2.5.2";
19+
public static final File homedir = new File(System.getProperty("user.home"));
20+
public static final File workdir = new File(System.getProperty("user.dir"));
21+
public static final ParentClassLoader loader = new ParentClassLoader(Boot.class.getClassLoader());
22+
2023
public static File
2124
mkFile(File parent, String... kids) throws Exception {
2225
File ret = parent;
@@ -225,13 +228,8 @@ public class Boot {
225228

226229
public static URLClassLoader
227230
loadJar(File jar) throws Exception {
228-
URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
229-
Class sc = URLClassLoader.class;
230-
Method cm = sc.getDeclaredMethod("addURL", URL.class);
231-
232-
cm.setAccessible(true);
233-
cm.invoke(cl, new Object[]{jar.toURI().toURL()});
234-
return cl; }
231+
loader.addURL(jar.toURI().toURL());
232+
return loader; }
235233

236234
public static void
237235
main(String[] args) throws Exception {
@@ -251,8 +249,9 @@ public class Boot {
251249
System.setProperty("BOOT_VERSION", initialVersion);
252250
System.err.println("Running for the first time, BOOT_VERSION not set: updating to latest."); }
253251

254-
URLClassLoader cl = loadJar(f);
255-
Class c = Class.forName("boot.App", true, cl);
256-
Method m = c.getMethod("main", String[].class);
252+
loadJar(f);
253+
tccl(loader);
254+
Class c = Class.forName("boot.App", true, loader);
255+
Method m = c.getMethod("main", String[].class);
257256

258257
m.invoke(null, new Object[]{a}); }}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package boot.bin;
2+
3+
import java.net.URL;
4+
import java.net.URLClassLoader;
5+
6+
// Exists solely so we have a type to seal to prevent user code from
7+
// altering our top-level CL.
8+
public class ParentClassLoader extends URLClassLoader {
9+
public ParentClassLoader(ClassLoader parent) {
10+
super(new URL[0], parent); }
11+
12+
public void addURL(URL url) {
13+
super.addURL(url); }}

0 commit comments

Comments
 (0)