Created
October 5, 2012 11:56
-
-
Save wjlroe/3839414 to your computer and use it in GitHub Desktop.
Utility function for getting types of Java methods from Clojure (for debugging)
This file contains 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
(defn method-spy | |
"Print out reflection info about a method." | |
[classname methodname] | |
(let [c (Class/forName classname) | |
all-methods (.getDeclaredMethods c) | |
matching-methods (filter #(= methodname (.getName %)) all-methods) | |
pr-col (fn [name info] (println (format "%24s: %s" name info)))] | |
(doseq [m matching-methods] | |
(println (.toGenericString m)) | |
(pr-col "ReturnType" (.getReturnType m)) | |
(pr-col "GenericReturnType" (.getGenericReturnType m)) | |
(let [p-types (.getParameterTypes m) | |
gp-types (.getGenericParameterTypes m) | |
types (map vector p-types gp-types) | |
x-types (.getExceptionTypes m) | |
gx-types (.getGenericExceptionTypes m) | |
exceptions (map vector x-types gx-types)] | |
(doseq [type types] | |
(pr-col "ParamterType" (first type)) | |
(pr-col "GenericParamterType" (second type))) | |
(doseq [exception exceptions] | |
(pr-col "ExceptionType" (first exception)) | |
(pr-col "GenericExceptionType" (second exception))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: