Skip to content

Commit

Permalink
Only allow URLClassLoader to be modifiable if we can access addURL
Browse files Browse the repository at this point in the history
Under the latest build of Java 9 (9-ea+148), we can no longer call
.setAccessible on URLClassLoader.addURL() without --add-opens on the
java command line. So, we check to see if we can actually modify
URLClassLoader, and extend to DynamicClasspath appropriately.
  • Loading branch information
tobias committed Dec 9, 2016
1 parent 38fae3f commit 783bfe7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/dynapath/defaults.clj
Expand Up @@ -16,14 +16,18 @@
:classpath-urls #(seq (.getURLs ^URLClassLoader %))))

(when-not (extends? DynamicClasspath URLClassLoader)
(extend URLClassLoader
DynamicClasspath
(assoc base-url-classloader
:add-classpath-url (fn [cl url]
(-> URLClassLoader
(.getDeclaredMethod "addURL" (into-array Class [URL]))
(doto (.setAccessible true))
(.invoke cl (into-array URL [url]))))))
(let [addURL (try
(-> URLClassLoader
(.getDeclaredMethod "addURL" (into-array Class [URL]))
(doto (.setAccessible true)))
(catch Exception _))]
(extend URLClassLoader
DynamicClasspath
(assoc base-url-classloader
:can-add? (fn [_] (boolean addURL))
:add-classpath-url (fn [cl url]
(when addURL
(.invoke addURL cl (into-array URL [url])))))))

(extend DynamicClassLoader
DynamicClasspath
Expand Down

0 comments on commit 783bfe7

Please sign in to comment.