Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove laziness in Slamhound's "get-available-classes" #184

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/refactor_nrepl/ns/slam/hound/search.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@
[#^File f #^File loc]
(let [lp (.getPath loc)]
(try
(map class-or-ns-name
(filter class-file?
(map #(.getName #^JarEntry %)
(enumeration-seq (.entries (JarFile. f))))))
(into ()
(comp
(map #(.getName #^JarEntry %))
(filter class-file?)
(map class-or-ns-name))
(enumeration-seq (.entries (JarFile. f))))
(catch Exception e [])))) ; fail gracefully if jar is unreadable

(defmethod path-class-files :dir
;; Dispatch directories and files (excluding jars) recursively.
[#^File d #^File loc]
(let [fs (.listFiles d (proxy [FilenameFilter] []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line was actually asking for trouble..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup.

(accept [d n] (not (jar? (file n))))))]
(reduce concat (for [f fs] (path-class-files f loc)))))
(into () (mapcat #(path-class-files % loc)) fs)))

(defmethod path-class-files :class
;; Build class info using file path relative to parent classpath entry
Expand All @@ -99,26 +101,28 @@
[(class-or-ns-name fpr)])
[])))

(defn scan-paths
"Takes one or more classpath strings, scans each classpath entry location, and
returns a list of all class file paths found, each relative to its parent
directory or jar on the classpath."
([cp]
(if cp
(let [entries (enumeration-seq
(StringTokenizer. cp File/pathSeparator))
locs (mapcat expand-wildcard entries)]
(mapcat #(path-class-files % %) locs))
())))

(defn- get-available-classes
[]
(->> (mapcat scan-paths (concat (map #(System/getProperty %) ["sun.boot.class.path"
"java.ext.dirs"
"java.class.path"])
(map #(.getName %) (cp/classpath-jarfiles))))
(remove clojure-fn-file?)
(map symbol)))
(defn path-entries-seq
"Split a string on the 'path separator', i.e. ':'. Used for splitting multiple
classpath entries."
[path-str]
(enumeration-seq
(StringTokenizer. path-str File/pathSeparator)))

(defn all-classpath-entries []
(into (map #(System/getProperty %) ["sun.boot.class.path"
"java.ext.dirs"
"java.class.path"])
(map #(.getName %) (cp/classpath-jarfiles))))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied this logic over, although it seems strange to me that this is really the way to get the full classpath.


(defn- get-available-classes []
(into ()
(comp (mapcat path-entries-seq)
(mapcat expand-wildcard)
(mapcat #(path-class-files % %))
(remove clojure-fn-file?)
(distinct)
(map symbol))
(all-classpath-entries)))

(def available-classes
(get-available-classes))
Expand Down