Created
February 16, 2012 21:47
-
-
Save mattyoho/1848119 to your computer and use it in GitHub Desktop.
Reflect on an object's methods in Clojure
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
(use '[clojure.reflect :only [reflect]]) | |
(use '[clojure.string :only [join]]) | |
(defn inspect [obj] | |
"nicer output for reflecting on an object's methods" | |
(let [reflection (reflect obj) | |
members (sort-by :name (:members reflection))] | |
(println "Class:" (.getClass obj)) | |
(println "Bases:" (:bases reflection)) | |
(println "---------------------\nConstructors:") | |
(doseq [constructor (filter #(instance? clojure.reflect.Constructor %) members)] | |
(println (:name constructor) "(" (join ", " (:parameter-types constructor)) ")")) | |
(println "---------------------\nMethods:") | |
(doseq [method (filter #(instance? clojure.reflect.Method %) members)] | |
(println (:name method) "(" (join ", " (:parameter-types method)) ") ;=>" (:return-type method))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment