Mercurial > lasercutter
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/test_clojure/annotations/java_6_and_later.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,73 @@ 1.4 +;; java 6 annotation tests 1.5 +(in-ns 'clojure.test-clojure.annotations) 1.6 + 1.7 +(import [java.lang.annotation Annotation Retention RetentionPolicy Target ElementType] 1.8 + [javax.xml.ws WebServiceRef WebServiceRefs]) 1.9 +(definterface Foo (foo [])) 1.10 + 1.11 +(deftype #^{Deprecated true 1.12 + Retention RetentionPolicy/RUNTIME 1.13 + javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] 1.14 + javax.xml.ws.soap.Addressing {:enabled false :required true} 1.15 + WebServiceRefs [(WebServiceRef {:name "fred" :type String}) 1.16 + (WebServiceRef {:name "ethel" :mappedName "lucy"})]} 1.17 + Bar [#^int a 1.18 + #^{:tag int 1.19 + Deprecated true 1.20 + Retention RetentionPolicy/RUNTIME 1.21 + javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] 1.22 + javax.xml.ws.soap.Addressing {:enabled false :required true} 1.23 + WebServiceRefs [(WebServiceRef {:name "fred" :type String}) 1.24 + (WebServiceRef {:name "ethel" :mappedName "lucy"})]} 1.25 + b] 1.26 + Foo (#^{Deprecated true 1.27 + Retention RetentionPolicy/RUNTIME 1.28 + javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"] 1.29 + javax.xml.ws.soap.Addressing {:enabled false :required true} 1.30 + WebServiceRefs [(WebServiceRef {:name "fred" :type String}) 1.31 + (WebServiceRef {:name "ethel" :mappedName "lucy"})]} 1.32 + foo [this] 42)) 1.33 + 1.34 +(defn annotation->map 1.35 + "Converts a Java annotation (which conceals data) 1.36 + into a map (which makes is usable). Not lazy. 1.37 + Works recursively. Returns non-annotations unscathed." 1.38 + [#^java.lang.annotation.Annotation o] 1.39 + (cond 1.40 + (instance? Annotation o) 1.41 + (let [type (.annotationType o) 1.42 + itfs (-> (into #{type} (supers type)) (disj java.lang.annotation.Annotation)) 1.43 + data-methods (into #{} (mapcat #(.getDeclaredMethods %) itfs))] 1.44 + (into 1.45 + {:annotationType (.annotationType o)} 1.46 + (map 1.47 + (fn [m] [(keyword (.getName m)) (annotation->map (.invoke m o nil))]) 1.48 + data-methods))) 1.49 + (or (sequential? o) (.isArray (class o))) 1.50 + (map annotation->map o) 1.51 + :else o)) 1.52 + 1.53 +(def expected-annotations 1.54 + #{{:annotationType java.lang.annotation.Retention, :value RetentionPolicy/RUNTIME} 1.55 + {:annotationType javax.xml.ws.WebServiceRefs, 1.56 + :value [{:annotationType javax.xml.ws.WebServiceRef, :name "fred", :mappedName "", :type java.lang.String, :wsdlLocation "", :value java.lang.Object} 1.57 + {:annotationType javax.xml.ws.WebServiceRef, :name "ethel", :mappedName "lucy", :type java.lang.Object, :wsdlLocation "", :value java.lang.Object}]} 1.58 + {:annotationType javax.xml.ws.soap.Addressing, :enabled false, :required true} 1.59 + {:annotationType javax.annotation.processing.SupportedOptions, :value ["foo" "bar" "baz"]} 1.60 + {:annotationType java.lang.Deprecated}}) 1.61 + 1.62 +(deftest test-annotations-on-type 1.63 + (is (= 1.64 + expected-annotations 1.65 + (into #{} (map annotation->map (.getAnnotations Bar)))))) 1.66 + 1.67 +(deftest test-annotations-on-field 1.68 + (is (= 1.69 + expected-annotations 1.70 + (into #{} (map annotation->map (.getAnnotations (.getField Bar "b"))))))) 1.71 + 1.72 +(deftest test-annotations-on-method 1.73 + (is (= 1.74 + expected-annotations 1.75 + (into #{} (map annotation->map (.getAnnotations (.getMethod Bar "foo" nil))))))) 1.76 +