Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Broke out xml functions into separate namespace and reorganized code …

…a bit
  • Loading branch information...
commit 6c53b2b5f3dc73cb890638a0e9486835f4f16939 1 parent 1062aa7
@cosmin authored
View
1  README
@@ -12,6 +12,7 @@ Helpers for dealing with Java beans in Clojure
Example:
(require 'offbytwo.beanhelpers)
+(require 'offbytwo.beanhelpers.xml) ; if you are converting to from XML data types, like XMLGregorianCalendar
(beanhelpers/to-java YourJavaClass clojure-propert-map)
(beanhelpers/from-java javaValue)
View
65 src/main/clojure/offbytwo/beanhelpers.clj
@@ -1,28 +1,21 @@
(ns offbytwo.beanhelpers)
(defmulti to-java (fn [destination-type value] [destination-type (class value)]))
+(defmethod to-java :default [_ value] value)
(defmulti from-java class)
(defn- get-property-descriptors [clazz]
(.getPropertyDescriptors (java.beans.Introspector/getBeanInfo clazz)))
+;; getters
+
(defn- is-getter [method]
(and method (= 0 (alength (. method (getParameterTypes))))))
-(defn- is-setter [method]
- (and method (= 1 (alength (. method (getParameterTypes))))))
-
-(defn- get-setter-type [method]
- (get (.getParameterTypes method) 0))
-
(defn- make-getter-fn [method]
(fn [instance]
(from-java (.invoke method instance nil))))
-(defn- make-setter-fn [method]
- (fn [instance value]
- (.invoke method instance (into-array [(to-java (get-setter-type method) value)]))))
-
(defn- add-getter-fn [the-map prop-descriptor]
(let [name (.getName prop-descriptor)
method (.getReadMethod prop-descriptor)]
@@ -30,6 +23,18 @@
(assoc the-map (keyword name) (make-getter-fn method))
the-map)))
+;; setters
+
+(defn- is-setter [method]
+ (and method (= 1 (alength (. method (getParameterTypes))))))
+
+(defn- get-setter-type [method]
+ (get (.getParameterTypes method) 0))
+
+(defn- make-setter-fn [method]
+ (fn [instance value]
+ (.invoke method instance (into-array [(to-java (get-setter-type method) value)]))))
+
(defn- add-setter-fn [the-map prop-descriptor]
(let [name (.getName prop-descriptor)
method (.getWriteMethod prop-descriptor)]
@@ -37,6 +42,8 @@
(assoc the-map (keyword name) (make-setter-fn method))
the-map)))
+;; to java
+
(defmethod to-java [Enum String] [enum value]
(.invoke (.getDeclaredMethod enum "valueOf" (into-array [String])) nil (into-array [value])))
@@ -50,16 +57,7 @@
(apply setter [instance value]))))
instance))
-(defmethod to-java :default [_ value] value)
-
-(doseq [clazz [String Character Byte Short Integer Long Float Double Boolean BigInteger BigDecimal]]
- (derive clazz ::do-not-convert))
-
-(defmethod from-java ::do-not-convert [value] value)
-(defmethod from-java Iterable [instance] (for [each (seq instance)] (from-java each)))
-(defmethod from-java java.util.Map [instance] (into {} instance))
-(defmethod from-java nil [_] nil)
-(defmethod from-java Enum [enum] (str enum))
+;; from java
(defmethod from-java Object [instance]
(try
@@ -68,23 +66,14 @@
(into {} (for [[key getter-fn] (seq getter-map)] [key (getter-fn instance)])))
(catch Exception e (println "Error trying to convert " instance e))))
-(defmethod from-java javax.xml.datatype.XMLGregorianCalendar [obj]
- (let [date {:year (.getYear obj)
- :month (.getMonth obj)
- :day (.getDay obj)}
- time {:hour (.getHour obj)
- :minute (.getMinute obj)
- :second (.getSecond obj)}
- tz {:timezone (.getTimezone obj)}
- is-undefined? #(= javax.xml.datatype.DatatypeConstants/FIELD_UNDEFINED %1)]
- (conj {}
- (if-not (is-undefined? (:year date))
- date)
- (if-not (is-undefined? (:hour time))
- time)
- (if-not (is-undefined? (:timezone tz))
- tz))))
+(doseq [clazz [String Character Byte Short Integer Long Float Double Boolean BigInteger BigDecimal]]
+ (derive clazz ::do-not-convert))
-(prefer-method from-java java.util.Map Iterable)
-(prefer-method from-java Iterable Object)
+(defmethod from-java ::do-not-convert [value] value)
(prefer-method from-java ::do-not-convert Object)
+(defmethod from-java Iterable [instance] (for [each (seq instance)] (from-java each)))
+(prefer-method from-java Iterable Object)
+(defmethod from-java java.util.Map [instance] (into {} instance))
+(prefer-method from-java java.util.Map Iterable)
+(defmethod from-java nil [_] nil)
+(defmethod from-java Enum [enum] (str enum))
View
32 src/main/clojure/offbytwo/beanhelpers/xml.clj
@@ -0,0 +1,32 @@
+(ns offbytwo.beanhelpers.xml
+ (:use offbytwo.beanhelpers))
+
+(defmethod to-java [javax.xml.datatype.XMLGregorianCalendar clojure.lang.APersistentMap] [clazz props]
+ "Create an XMLGregorianCalendar object given the following keys :year :month :day :hour :minute :second :timezone"
+ (let [instance (.newInstance clazz)
+ undefined javax.xml.datatype.DatatypeConstants/FIELD_UNDEFINED
+ getu #(get %1 %2 undefined)]
+ (doto instance
+ (.setYear (getu props :year))
+ (.setMonth (getu props :month))
+ (.setDay (getu props :day))
+ (.setHour (getu props :hour))
+ (.setMinute (getu props :minute))
+ (.setTimezone (getu props :timezone)))))
+
+(defmethod from-java javax.xml.datatype.XMLGregorianCalendar [obj]
+ (let [date {:year (.getYear obj)
+ :month (.getMonth obj)
+ :day (.getDay obj)}
+ time {:hour (.getHour obj)
+ :minute (.getMinute obj)
+ :second (.getSecond obj)}
+ tz {:timezone (.getTimezone obj)}
+ is-undefined? #(= javax.xml.datatype.DatatypeConstants/FIELD_UNDEFINED %1)]
+ (conj {}
+ (if-not (is-undefined? (:year date))
+ date)
+ (if-not (is-undefined? (:hour time))
+ time)
+ (if-not (is-undefined? (:timezone tz))
+ tz))))
Please sign in to comment.
Something went wrong with that request. Please try again.