How can I get the methods of a Java class from Clojure?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
[EDIT 2]
Per M Smith’s comment below, this accomplishes the same but provides sorting by method name and only returns methods:
[/EDIT 2]
[EDIT]
My original answer refers to Clojure 1.2, but things have changed with Clojure 1.3. This works without any reliance on Clojure’s contrib libraries now:
This provides a much more decoupled approach, with the
reflectfunction providing all kinds of information on the argument passed in (in this case, aString"foo") and theprint-tablefunction taking any generic tabular data structure and pretty printing it as such.This is originally from this thread on the Google Group.
[/EDIT]
I’d use the
showfunction in theclojure.contrib.repl-utilsnamespace, which will print all static and instance members for an object (or class of an object). I require it like so:Here’s an example using Joda Time:
The first example demonstrates how you can simply pass a class to
show, while the second demonstrates that you can pass an instance of the class as well.This of course works for lots of Clojure items that are Java classes underneath. Here’s an example of seeing all methods available to an instance of java.lang.String: