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