[EDIT 2]
In a commentary on M Smith below, this does the same, but provides sorting by method name and returns methods:
(print-table (sort-by :name (filter :exception-types (:members (r/reflect "foo")))))
[/ EDIT 2]
[EDIT]
My initial answer was about Clojure 1.2, but that all changed with Clojure 1.3. Now this works without any dependency on Clojure contributors:
(require '[clojure.reflect :as r]) (use '[clojure.pprint :only [print-table]]) (print-table (:members (r/reflect "foo")))
This provides a much more decoupled approach, while the reflect
function provides all kinds of information about the passed argument (in this case a String
"foo"
) and the print-table
function, which accepts any general table-based data structure and prints it as such.
This is from this thread in the google group .
[/ EDIT]
I would use the show
function in the clojure.contrib.repl-utils
namespace, which will print all static and instance elements for an object (or object class). I demand it like this:
(require '[clojure.contrib.repl-utils :as ru])
Here is an example of using Joda Time:
(import 'org.joda.time.DateTime) (ru/show DateTime) (ru/show (DateTime.))
The first example demonstrates how you can simply pass a class to show
, and the second demonstrates that you can also pass an instance of the class.
This, of course, works for a variety of Clojure elements, which are the Java classes below. Here is an example of viewing all available methods for an instance of java.lang.String:
(ru/show "foo")
semperos Apr 28 '11 at 16:08 2011-04-28 16:08
source share