diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-02-06 05:39:24 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-02-06 05:39:24 +0000 |
commit | b7f44fa102c405f9220f271ac89b518ed2e916e2 (patch) | |
tree | 810d1963fc3b79d266ca379d01c62ebd98a172a8 | |
parent | bb759a177e9185741b2dc5e217061298b007cea6 (diff) |
added bean, which creates a live read-only map of an object's javabeans properties
-rw-r--r-- | src/boot.clj | 25 |
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 )) |