I used to use user.clj for the following, but that doesn't always work
right (did it have something to do with *print-length* not being bound
for clojure.lang.Script? I'm don't quite remember...) So anyway, I
have a little shell script that runs clojure for me with the right
classpath and, when launching a REPL, with ~/.clojurerc.clj on its
command line. In .clojurerc.clj, I have:
(import '(java.io LineNumberReader InputStreamReader PushbackReader)
'(java.lang.reflect Modifier Method Constructor)
'(clojure.lang RT))
(set! *print-length* 103)
(defn show
([x] (show x nil))
([x i]
(let [c (if (class? x) x (class x))
items (sort
(for [m (concat (.getFields c)
(.getMethods c)
(.getConstructors c))]
(let [static? (bit-and Modifier/STATIC
(.getModifiers m))
method? (instance? Method m)
ctor? (instance? Constructor m)
text (if ctor?
(str "(" (apply str (interpose ", "
(.getParameterTypes m))) ")")
(str
(if (pos? static?) "static ")
(.getName m) " : "
(if method?
(str (.getReturnType m) " ("
(count (.getParameterTypes m)) ")")
(str (.getType m)))))]
[(- static?) method? text (str m) m])))]
(if i
(last (nth items i))
(do (println "=== " c " ===")
(doseq [[e i] (map list items (iterate inc 0))]
(printf "[%2d] %s\n" i (nth e 2))))))))
*print-length* is documented elsewhere.
show works like this:
user=> (show Object) ; give it a class
=== java.lang.Object ===
[ 0] ()
[ 1] equals : boolean (1)
[ 2] getClass : class java.lang.Class (0)
[ 3] hashCode : int (0)
[ 4] notify : void (0)
[ 5] notifyAll : void (0)
[ 6] toString : class java.lang.String (0)
[ 7] wait : void (0)
[ 8] wait : void (1)
[ 9] wait : void (2)
nil
user=> (show Object 1) ; a class and a method number to see details
#<Method public boolean java.lang.Object.equals(java.lang.Object)>
user=> (show {}) ; or give it an instance
=== clojure.lang.PersistentHashMap ===
[ 0] static EMPTY : class clojure.lang.PersistentHashMap
[ 1] static applyToHelper : class java.lang.Object (2)
[ 2] static create : class clojure.lang.PersistentHashMap (1)
[ 3] static create : class clojure.lang.PersistentHashMap (1)
[ 4] static create : class clojure.lang.PersistentHashMap (1)
[ 5] static create : class clojure.lang.PersistentHashMap (2)
[ 6] static create : interface clojure.lang.IPersistentMap (1)
[ 7] (interface clojure.lang.IPersistentMap, int, interface
clojure.lang.PersistentHashMap$INode)
[ 8] applyTo : class java.lang.Object (1)
[ 9] assoc : interface clojure.lang.Associative (2)
...
It's like interactive javadoc, without all those distracting English
descriptions of things. :-)
--Chouser
Oh, no, those are two independent settings. 'show' is not helped much
the the constrained *print-length* since it does its own looping and
only over concrete things like method lists -- no infinite seqs there.
No, the *print-length* is set for the whole REPL context to save
myself from pain when I accidentally evaluate the equivalent of
(iterate inc 0) at the prompt.
--Chouser
Ah, ok - I thought the *print-length* mod was related to the 'show' function.
- Bill