summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-02-06 05:39:24 +0000
committerRich Hickey <richhickey@gmail.com>2008-02-06 05:39:24 +0000
commitb7f44fa102c405f9220f271ac89b518ed2e916e2 (patch)
tree810d1963fc3b79d266ca379d01c62ebd98a172a8 /src
parentbb759a177e9185741b2dc5e217061298b007cea6 (diff)
added bean, which creates a live read-only map of an object's javabeans properties
Diffstat (limited to 'src')
-rw-r--r--src/boot.clj25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/boot.clj b/src/boot.clj
index ea9842df..ad3d540e 100644
--- a/src/boot.clj
+++ b/src/boot.clj
@@ -1192,6 +1192,30 @@
(. (the-var defmacro*) (setMacro))
+(defn bean [#^Object x]
+ (let [c (. x (getClass))
+ pmap (reduce (fn [m #^java.beans.PropertyDescriptor pd]
+ (let [name (. pd (getName))
+ method (. pd (getReadMethod))]
+ (if (and method (zero? (alength (. method (getParameterTypes)))))
+ (assoc m (keyword name) (fn [] (. method (invoke x nil))))
+ m)))
+ {}
+ (seq (.. java.beans.Introspector
+ (getBeanInfo c)
+ (getPropertyDescriptors))))
+ v (fn [k] ((pmap k)))]
+ (implement [clojure.lang.IPersistentMap]
+ (containsKey [k] (contains? pmap k))
+ (entryAt [k] (when (contains? pmap k) (new clojure.lang.MapEntry k (v k))))
+ (valAt ([k] (v k))
+ ([k default] (if (contains? pmap k) (v k) default)))
+ (count [] (count pmap))
+ (seq [] ((fn this [pseq]
+ (when pseq
+ (lazy-cons (new clojure.lang.MapEntry (first pseq) (v (first pseq)))
+ (this (rest pseq))))) (keys pmap))))))
+
(export
'( load-file load
list cons conj defn
@@ -1257,5 +1281,6 @@
nthrest
string? symbol? map? seq? vector?
let* fn* defn* defmacro*
+ bean
))