Make classpath return classpath elements from all parent classloaders
Since a classloader delegates to its parent if it fails to provide a class, the
classpath reported by java.classpath should include the classpath of parent
classloaders.
Add unit tests for non-empty sequence of java.io.File objects as return
of classpath function.
Signed-off-by: Stuart Sierra <mail@stuartsierra.com>
Hugo Duncan authored 12 years ago
Stuart Sierra committed 12 years ago
12 | 12 |
;; remove this notice, or any other, from this software.
|
13 | 13 |
|
14 | 14 |
|
15 | |
(ns
|
16 | |
^{:author "Stuart Sierra",
|
|
15 |
(ns
|
|
16 |
^{:author "Stuart Sierra"
|
17 | 17 |
:doc "Utilities for dealing with the JVM's classpath"}
|
18 | 18 |
clojure.java.classpath
|
19 | 19 |
(:require [clojure.java.io :as io])
|
|
45 | 45 |
(.split (System/getProperty "java.class.path")
|
46 | 46 |
(System/getProperty "path.separator"))))
|
47 | 47 |
|
48 | |
(defn clojure-classpath
|
49 | |
"Returns a sequence of File paths from Clojure's base classloader."
|
50 | |
[]
|
51 | |
(let [loader (clojure.lang.RT/baseLoader)]
|
52 | |
(when (instance? URLClassLoader loader)
|
53 | |
(map #(File. (.getPath ^URL %)) (.getURLs ^URLClassLoader loader)))))
|
|
48 |
(defn loader-classpath
|
|
49 |
"Returns a sequence of File paths from a classloader."
|
|
50 |
[loader]
|
|
51 |
(when (instance? java.net.URLClassLoader loader)
|
|
52 |
(map
|
|
53 |
#(java.io.File. (.getPath ^java.net.URL %))
|
|
54 |
(.getURLs ^java.net.URLClassLoader loader))))
|
54 | 55 |
|
55 | 56 |
(defn classpath
|
56 | |
"Returns a sequence of File objects of the elements on the
|
57 | |
classpath."
|
58 | |
[]
|
59 | |
(distinct (concat (clojure-classpath) (system-classpath))))
|
|
57 |
"Returns a sequence of File objects of the elements on the classpath."
|
|
58 |
([classloader]
|
|
59 |
(distinct
|
|
60 |
(mapcat
|
|
61 |
loader-classpath
|
|
62 |
(take-while
|
|
63 |
identity
|
|
64 |
(iterate #(.getParent %) classloader)))))
|
|
65 |
([] (classpath (clojure.lang.RT/baseLoader))))
|
60 | 66 |
|
61 | 67 |
(defn classpath-directories
|
62 | 68 |
"Returns a sequence of File objects for the directories on classpath."
|
|
0 |
(ns clojure.java.classpath-test
|
|
1 |
(:require
|
|
2 |
[clojure.java.classpath :as classpath]
|
|
3 |
[clojure.test :as test]))
|
|
4 |
|
|
5 |
(test/deftest classpath-test
|
|
6 |
(test/is (every? #(instance? java.io.File %) (classpath/classpath))
|
|
7 |
"returns File objects")
|
|
8 |
(test/is (seq (classpath/classpath))
|
|
9 |
"returns a non empty sequence"))
|