Skip to content
Browse files

Added support for converting Enums from java and fixed other bugs. Te…

…sts pass now for simple to and from java conversion
  • Loading branch information...
1 parent 716c070 commit 7698f8135c1580eaa5be8e7ea7034f74f25faa6a @cosmin committed Sep 9, 2010
24 src/main/clojure/offbytwo/beanhelpers.clj
@@ -17,7 +17,7 @@
(defn- make-getter-fn [method]
(fn [instance]
- (.invoke method instance nil)))
+ (from-java (.invoke method instance nil))))
(defn- make-setter-fn [method]
(fn [instance value]
@@ -26,9 +26,9 @@
(defn- add-getter-fn [the-map prop-descriptor]
(let [name (.getName prop-descriptor)
method (.getReadMethod prop-descriptor)]
- (if (is-getter method)
- (assoc the-map (keyword name) (make-getter-fn method)))))
+ (if (and (is-getter method) (not (= "class" name)))
+ (assoc the-map (keyword name) (make-getter-fn method))
+ the-map)))
(defn- add-setter-fn [the-map prop-descriptor]
(let [name (.getName prop-descriptor)
@@ -37,7 +37,6 @@
(assoc the-map (keyword name) (make-setter-fn method))
(defmethod to-java [Enum String] [enum value]
(.invoke (.getDeclaredMethod enum "valueOf" (into-array [String])) nil (into-array [value])))
@@ -53,22 +52,19 @@
(defmethod to-java :default [_ value] value)
(doseq [clazz [String Character Byte Short Integer Long Float Double Boolean]]
(derive clazz ::reference-type))
(defmethod from-java ::reference-type [value] value)
-(defmethod from-java Iterable [instance]
- (for [each (seq instance)] (from-java each)))
+(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))
(defmethod from-java Object [instance]
(let [clazz (.getClass instance)
- getter-map (reduce add-getter-fn {} (get-property-descriptors clazz))]
- (reduce add-getter-fn {} (get-property-descriptors clazz))))
-(defmethod from-java java.util.Map [instance]
- (into {} instance))
+ getter-map (reduce add-getter-fn {} (get-property-descriptors clazz))]
+ (into {} (for [[key getter-fn] (seq getter-map)] [key (getter-fn instance)]))))
(prefer-method from-java java.util.Map Iterable)
(prefer-method from-java Iterable Object)
3 src/test/clojure/offbytwo/beanhelpers/beanhelperstest.clj
@@ -19,8 +19,7 @@
(deftest java-to-clojure
(let [address (new Address "123 Main St" "Dallas" State/TX "75432")
- person (Person. "Bob" 30 address)
- person-info (from-java person)]
+ person (from-java (Person. "Bob" 30 address))]
(is (= "Bob" (:name person)))
(is (= 30 (:age person)))
(is (= "123 Main St" (:line1 (:address person))))

0 comments on commit 7698f81

Please sign in to comment.
Something went wrong with that request. Please try again.