Skip to content

Commit ce42411

Browse files
Phil Brownbbatsov
authored andcommitted
Fix jdk source code lookup for boot projects
Adds available jdk sources to the ClassLoader used to resolve boot project resources by the 'info' op. This way, e.g. (cider-var-info "java.lang.String"), can return a "file" entry with an absolute url to the source, instead of just the relative resource path.
1 parent bd7b2bc commit ce42411

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/cider/nrepl/middleware/info.clj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
(str "file:" path)
2424
(str "file:" path dir-separator))]
2525
(new java.net.URL url)))
26-
paths)]
27-
(new java.net.URLClassLoader (into-array java.net.URL urls))))
26+
paths)
27+
jdk-sources (->> [#_"see '## Classpath' notes at `cider.nrepl.middleware.util.java`"
28+
["src.zip"]
29+
["lib" "tools.jar"]]
30+
(map (partial apply java/jdk-resource-url))
31+
(remove nil?))]
32+
(new java.net.URLClassLoader (into-array java.net.URL (concat urls jdk-sources)))))
2833

2934
(defn- resource-full-path [relative-path]
3035
(if (u/boot-project?)

src/cider/nrepl/middleware/util/java.clj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,21 @@
4444
(-> (io/file (System/getProperty "java.home"))
4545
(.getParentFile)))
4646

47+
(defn jdk-resource-url
48+
"Returns url to file at PATH-SEGMENTS relative to `jdk-root`."
49+
[& path-segments]
50+
(let [f (apply io/file jdk-root path-segments)]
51+
(when (.canRead f)
52+
(io/as-url f))))
53+
4754
(def jdk-sources
4855
"The JDK sources path. If available, this is added to the classpath. By
4956
convention, this is the file `src.zip` in the root of the JDK directory."
50-
(let [zip (io/file jdk-root "src.zip")]
51-
(when (.canRead zip)
52-
(add-classpath! (io/as-url zip)))))
57+
(some-> (jdk-resource-url "src.zip") add-classpath!))
5358

5459
(def jdk-tools
5560
"The JDK `tools.jar` path. If available, this is added to the classpath."
56-
(let [jar (io/file jdk-root "lib" "tools.jar")]
57-
(when (.canRead jar)
58-
(add-classpath! (io/as-url jar)))))
61+
(some-> (jdk-resource-url "lib" "tools.jar") add-classpath!))
5962

6063
;;; ## Javadoc URLs
6164
;; Relative Javadoc URLs can be constructed from class/member signatures.

test/clj/cider/nrepl/middleware/info_test.clj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[clojure.set :as set]
77
[clojure.string :as str]
88
[cider.nrepl.middleware.info :as info]
9+
[cider.nrepl.middleware.util.java :as java]
910
[cider.nrepl.middleware.util.meta :as m]
1011
[cider.nrepl.middleware.util.misc :as util]
1112
[cider.nrepl.test-session :as session]
@@ -70,8 +71,16 @@
7071
(let [tmp-jar-path "jar:file:fake/clojure.jar"]
7172
(try
7273
(System/setProperty "fake.class.path" tmp-jar-path)
73-
(is (= (-> (#'cider.nrepl.middleware.info/boot-class-loader) .getURLs last str)
74-
"file:fake/clojure.jar"))
74+
(is (some #{"file:fake/clojure.jar"}
75+
(->> (#'cider.nrepl.middleware.info/boot-class-loader) .getURLs (map str))))
76+
(finally
77+
(System/clearProperty "fake.class.path")))))
78+
(testing "include sources when avaliable"
79+
(when-let [src-url (java/jdk-resource-url "src.zip")]
80+
(try
81+
(System/setProperty "fake.class.path" tmp-dir-name)
82+
(is (some #{src-url}
83+
(.getURLs (#'cider.nrepl.middleware.info/boot-class-loader))))
7584
(finally
7685
(System/clearProperty "fake.class.path")))))))
7786

0 commit comments

Comments
 (0)