Skip to content
Browse files

Make BigInteger handling compatible with Clojure 1.2

  • Loading branch information...
1 parent e149d4a commit 92937d1eff36c847581ec391deebe5ba6b029fc5 @seancorfield seancorfield committed Feb 13, 2012
Showing with 35 additions and 1 deletion.
  1. +11 −0 src/main/clojure/clojure/java/data.clj
  2. +24 −1 src/test/clojure/clojure/java/test_data.clj
View
11 src/main/clojure/clojure/java/data.clj
@@ -109,10 +109,21 @@
(list* 'do body))
(catch ClassNotFoundException _#)))
+(defmacro ^{:private true} when-not-available
+ [sym & body]
+ (try
+ (when-not (resolve sym)
+ (list* 'do body))
+ (catch ClassNotFoundException _#)))
+
(when-available
biginteger
(defmethod to-java [BigInteger Object] [_ value] (biginteger value)))
+(when-not-available
+ biginteger
+ (defmethod to-java [BigInteger Object] [_ value] (bigint value)))
+
;; common from-java definitions
(defmethod from-java :default [^Object instance]
View
25 src/test/clojure/clojure/java/test_data.clj
@@ -51,9 +51,32 @@
(is (thrown-with-msg? Exception #"invoked :info"
(to-java Person {:name "Bob" :foobar "Baz"}))))))
+;; feature testing macro, based on suggestion from Chas Emerick:
+(defmacro ^{:private true} when-available
+ [sym & body]
+ (try
+ (when (resolve sym)
+ (list* 'do body))
+ (catch ClassNotFoundException _#)))
+
+(defmacro ^{:private true} when-not-available
+ [sym & body]
+ (try
+ (when-not (resolve sym)
+ (list* 'do body))
+ (catch ClassNotFoundException _#)))
+
+(when-available
+ biginteger
+ (defn- to-BigInteger [v] (biginteger v)))
+
+(when-not-available
+ biginteger
+ (defn- to-BigInteger [v] (bigint v)))
+
(deftest java-to-clojure
(let [address (new Address "123 Main St" "Dallas" State/TX "75432")
- person (from-java (Person. "Bob" (biginteger 30) address))]
+ person (from-java (Person. "Bob" (to-BigInteger 30) address))]
(is (= "Bob" (:name person)))
(is (= 30 (:age person)))
(is (= "123 Main St" (:line1 (:address person))))

0 comments on commit 92937d1

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