Created
March 25, 2013 19:21
-
-
Save gtrak/5239837 to your computer and use it in GitHub Desktop.
clojure reflections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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