annotate src/clojure/test_clojure/annotations/java_6_and_later.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;; java 6 annotation tests
rlm@10 2 (in-ns 'clojure.test-clojure.annotations)
rlm@10 3
rlm@10 4 (import [java.lang.annotation Annotation Retention RetentionPolicy Target ElementType]
rlm@10 5 [javax.xml.ws WebServiceRef WebServiceRefs])
rlm@10 6 (definterface Foo (foo []))
rlm@10 7
rlm@10 8 (deftype #^{Deprecated true
rlm@10 9 Retention RetentionPolicy/RUNTIME
rlm@10 10 javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"]
rlm@10 11 javax.xml.ws.soap.Addressing {:enabled false :required true}
rlm@10 12 WebServiceRefs [(WebServiceRef {:name "fred" :type String})
rlm@10 13 (WebServiceRef {:name "ethel" :mappedName "lucy"})]}
rlm@10 14 Bar [#^int a
rlm@10 15 #^{:tag int
rlm@10 16 Deprecated true
rlm@10 17 Retention RetentionPolicy/RUNTIME
rlm@10 18 javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"]
rlm@10 19 javax.xml.ws.soap.Addressing {:enabled false :required true}
rlm@10 20 WebServiceRefs [(WebServiceRef {:name "fred" :type String})
rlm@10 21 (WebServiceRef {:name "ethel" :mappedName "lucy"})]}
rlm@10 22 b]
rlm@10 23 Foo (#^{Deprecated true
rlm@10 24 Retention RetentionPolicy/RUNTIME
rlm@10 25 javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"]
rlm@10 26 javax.xml.ws.soap.Addressing {:enabled false :required true}
rlm@10 27 WebServiceRefs [(WebServiceRef {:name "fred" :type String})
rlm@10 28 (WebServiceRef {:name "ethel" :mappedName "lucy"})]}
rlm@10 29 foo [this] 42))
rlm@10 30
rlm@10 31 (defn annotation->map
rlm@10 32 "Converts a Java annotation (which conceals data)
rlm@10 33 into a map (which makes is usable). Not lazy.
rlm@10 34 Works recursively. Returns non-annotations unscathed."
rlm@10 35 [#^java.lang.annotation.Annotation o]
rlm@10 36 (cond
rlm@10 37 (instance? Annotation o)
rlm@10 38 (let [type (.annotationType o)
rlm@10 39 itfs (-> (into #{type} (supers type)) (disj java.lang.annotation.Annotation))
rlm@10 40 data-methods (into #{} (mapcat #(.getDeclaredMethods %) itfs))]
rlm@10 41 (into
rlm@10 42 {:annotationType (.annotationType o)}
rlm@10 43 (map
rlm@10 44 (fn [m] [(keyword (.getName m)) (annotation->map (.invoke m o nil))])
rlm@10 45 data-methods)))
rlm@10 46 (or (sequential? o) (.isArray (class o)))
rlm@10 47 (map annotation->map o)
rlm@10 48 :else o))
rlm@10 49
rlm@10 50 (def expected-annotations
rlm@10 51 #{{:annotationType java.lang.annotation.Retention, :value RetentionPolicy/RUNTIME}
rlm@10 52 {:annotationType javax.xml.ws.WebServiceRefs,
rlm@10 53 :value [{:annotationType javax.xml.ws.WebServiceRef, :name "fred", :mappedName "", :type java.lang.String, :wsdlLocation "", :value java.lang.Object}
rlm@10 54 {:annotationType javax.xml.ws.WebServiceRef, :name "ethel", :mappedName "lucy", :type java.lang.Object, :wsdlLocation "", :value java.lang.Object}]}
rlm@10 55 {:annotationType javax.xml.ws.soap.Addressing, :enabled false, :required true}
rlm@10 56 {:annotationType javax.annotation.processing.SupportedOptions, :value ["foo" "bar" "baz"]}
rlm@10 57 {:annotationType java.lang.Deprecated}})
rlm@10 58
rlm@10 59 (deftest test-annotations-on-type
rlm@10 60 (is (=
rlm@10 61 expected-annotations
rlm@10 62 (into #{} (map annotation->map (.getAnnotations Bar))))))
rlm@10 63
rlm@10 64 (deftest test-annotations-on-field
rlm@10 65 (is (=
rlm@10 66 expected-annotations
rlm@10 67 (into #{} (map annotation->map (.getAnnotations (.getField Bar "b")))))))
rlm@10 68
rlm@10 69 (deftest test-annotations-on-method
rlm@10 70 (is (=
rlm@10 71 expected-annotations
rlm@10 72 (into #{} (map annotation->map (.getAnnotations (.getMethod Bar "foo" nil)))))))
rlm@10 73