Factor Clojure code setting many different fields in a Java object using a parameter map bound to a var or local

StackOverflow https://stackoverflow.com/questions/23110132

문제

I would like to set a group of fields in a Java object from Clojure without using reflection at runtime.

This solution (copied from one of the solutions) is close to what I am after:

(defmacro set-all! [obj m]
    `(do ~@(map (fn [e] `(set! (. ~obj ~(key e)) ~(val e))) m) ~obj))

(def a (java.awt.Point.))
(set-all! a {x 300 y 100})

This works fine but I want the macro to be able to process a map of fields and values passed in as a var or as a local binding (i.e. not passed directly to the macro as above). The fields should be represented as keywords so the following should work:

(def a (java.awt.Point.))
(def m {:x 300 :y 100})
(set-all! a m)

I can't figure out how to do this using set! and the special dot form within a macro (or any solution that works as above without using reflection at runtime).

도움이 되었습니까?

해결책

For this, I would do compile-time reflection coupled with polymorphism.

(defprotocol FieldSettable (set-field! [this k v]))

(defmacro extend-field-setter [klass] 
  (let [obj (with-meta (gensym "obj_") {:tag klass})
        fields (map #(symbol (.getName ^java.lang.reflect.Field %)) 
                    (-> klass str (java.lang.Class/forName) .getFields))
        setter (fn [fld] 
                 `(fn [~obj v#] (set! (. ~obj ~fld) v#) ~obj))] 
    `(let [m# ~(into {} (map (juxt keyword setter) fields))] 
       (extend ~klass 
         FieldSettable 
         {:set-field! (fn [~obj k# v#] ((m# k#) ~obj v#))}))))

This allows you to extend field setters per class.

(extend-field-setter java.awt.Point)
(extend-field-setter java.awt.Rectangle)

Now set-field! works on either and can be used with reduce-kv on a map.

(def pt (java.awt.Point.))
(def rect (java.awt.Rectangle.))

(def m {:x 1, :y 2})

(reduce-kv set-field! pt m) 
;=> #<Point java.awt.Point[x=1,y=2]>

(reduce-kv set-field! rect m) 
;=> #<Rectangle java.awt.Rectangle[x=1,y=2,width=0,height=0]>

Where in the rect example the width and height fields were left unaltered since not specified in the map.

다른 팁

Ok, this works. I'm not sure if it is faster than reflection and I'd like to see it done more elegantly if anyone has any suggestions.

(defmacro set-fn-2 [f]
    `(fn [o# v#] (set! (. o# ~f) v#)))

(defmacro set-fn-1 [f]
    `(list 'set-fn-2 (symbol ~f)))

(defn set-fn-0 [f]
    (eval (set-fn-1 (symbol (name f)))))

(def set-fn (memoize set-fn-0))

(def a (java.awt.Point.))

(def val-map {:x 1 :y 2})

(defn set-all! [o m]
    (doseq [k (keys m)] ((set-fn k) o (m k))))

(set-all! a val-map)

a
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top