Codebase list java-classpath-clojure / f1b9bfc
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
2 changed file(s) with 28 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
1212 ;; remove this notice, or any other, from this software.
1313
1414
15 (ns
16 ^{:author "Stuart Sierra",
15 (ns
16 ^{:author "Stuart Sierra"
1717 :doc "Utilities for dealing with the JVM's classpath"}
1818 clojure.java.classpath
1919 (:require [clojure.java.io :as io])
4545 (.split (System/getProperty "java.class.path")
4646 (System/getProperty "path.separator"))))
4747
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))))
5455
5556 (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))))
6066
6167 (defn classpath-directories
6268 "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"))