Mercurial > lasercutter
diff src/clojure/test_clojure/annotations/java_5.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_5.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,54 @@ 1.4 +;; java 5 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 +(definterface Foo (foo [])) 1.9 + 1.10 +(deftype #^{Deprecated true 1.11 + Retention RetentionPolicy/RUNTIME} 1.12 + Bar [#^int a 1.13 + #^{:tag int 1.14 + Deprecated true 1.15 + Retention RetentionPolicy/RUNTIME} b] 1.16 + Foo (#^{Deprecated true 1.17 + Retention RetentionPolicy/RUNTIME} 1.18 + foo [this] 42)) 1.19 + 1.20 +(defn annotation->map 1.21 + "Converts a Java annotation (which conceals data) 1.22 + into a map (which makes is usable). Not lazy. 1.23 + Works recursively. Returns non-annotations unscathed." 1.24 + [#^java.lang.annotation.Annotation o] 1.25 + (cond 1.26 + (instance? Annotation o) 1.27 + (let [type (.annotationType o) 1.28 + itfs (-> (into #{type} (supers type)) (disj java.lang.annotation.Annotation)) 1.29 + data-methods (into #{} (mapcat #(.getDeclaredMethods %) itfs))] 1.30 + (into 1.31 + {:annotationType (.annotationType o)} 1.32 + (map 1.33 + (fn [m] [(keyword (.getName m)) (annotation->map (.invoke m o nil))]) 1.34 + data-methods))) 1.35 + (or (sequential? o) (.isArray (class o))) 1.36 + (map annotation->map o) 1.37 + :else o)) 1.38 + 1.39 +(def expected-annotations 1.40 + #{{:annotationType java.lang.annotation.Retention, :value RetentionPolicy/RUNTIME} 1.41 + {:annotationType java.lang.Deprecated}}) 1.42 + 1.43 +(deftest test-annotations-on-type 1.44 + (is (= 1.45 + expected-annotations 1.46 + (into #{} (map annotation->map (.getAnnotations Bar)))))) 1.47 + 1.48 +(deftest test-annotations-on-field 1.49 + (is (= 1.50 + expected-annotations 1.51 + (into #{} (map annotation->map (.getAnnotations (.getField Bar "b"))))))) 1.52 + 1.53 +(deftest test-annotations-on-method 1.54 + (is (= 1.55 + expected-annotations 1.56 + (into #{} (map annotation->map (.getAnnotations (.getMethod Bar "foo" nil))))))) 1.57 +