Skip to content

Instantly share code, notes, and snippets.

@gtrak
Created March 25, 2013 19:21
Show Gist options
  • Save gtrak/5239837 to your computer and use it in GitHub Desktop.
Save gtrak/5239837 to your computer and use it in GitHub Desktop.
clojure reflections
(ns reflection
;;(:import nothing)
)
(defn class-loader
"could be any classloader"
[]
(.getContextClassLoader (Thread/currentThread)))
(defn get-class
[clazz]
(-> (Class/forName (str clazz) (boolean true) (class-loader))))
(defn rnew
([clazz] (.newInstance clazz))
;; a little more complicated, getConstructors and param-types and such
([clazz params]))
;; (rnew (get-class "java.util.List")) ;; instantiationException
(let [l (rnew (get-class "java.util.ArrayList"))]
(.addAll l (range 10))
(println l)
(println (seq l)))
;; #<ArrayList [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>
;; (0 1 2 3 4 5 6 7 8 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment