alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (java_5.clj)

This example Java source code file (java_5.clj) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

annotation, bar, class/forname, converts, deprecated, elementtype, foo, java, object, retention, retentionpolicy/runtime, returns, target, works

The java_5.clj Java example source code

;; java 5 annotation tests
(in-ns 'clojure.test-clojure.annotations)

(import [java.lang.annotation Annotation Retention RetentionPolicy Target ElementType])
(definterface Foo (foo []))

(deftype #^{Deprecated true
            Retention RetentionPolicy/RUNTIME}
  Bar [#^int a
       #^{:tag int
          Deprecated true
          Retention RetentionPolicy/RUNTIME} b]
  Foo (#^{Deprecated true
          Retention RetentionPolicy/RUNTIME}
       foo [this] 42))

(defn annotation->map
  "Converts a Java annotation (which conceals data)
   into a map (which makes is usable). Not lazy.
   Works recursively. Returns non-annotations unscathed."
  [#^java.lang.annotation.Annotation o]
  (cond
   (instance? Annotation o)
   (let [type (.annotationType o)
         itfs (-> (into #{type} (supers type)) (disj java.lang.annotation.Annotation))
         data-methods (into #{} (mapcat #(.getDeclaredMethods %) itfs))]
     (into
      {:annotationType (.annotationType o)}
      (map
       (fn [m] [(keyword (.getName m)) (annotation->map (.invoke m o nil))])
       data-methods)))
   (or (sequential? o) (.isArray (class o)))
   (map annotation->map o)
     :else o))

(def expected-annotations
  #{{:annotationType java.lang.annotation.Retention, :value RetentionPolicy/RUNTIME}
    {:annotationType java.lang.Deprecated}})

(deftest test-annotations-on-type
  (is (=
       expected-annotations
       (into #{} (map annotation->map (.getAnnotations Bar))))))

(deftest test-annotations-on-field
  (is (=
       expected-annotations
       (into #{} (map annotation->map (.getAnnotations (.getField Bar "b")))))))

(deftest test-annotations-on-method
  (is (=
       expected-annotations
       (into #{} (map annotation->map (.getAnnotations (.getMethod Bar "foo" nil)))))))

(gen-class :name foo.Bar
           :extends clojure.lang.Box
           :constructors {^{Deprecated true} [Object] [Object]}
           :init init
           :prefix "foo")

(defn foo-init [obj]
  [[obj] nil])

(deftest test-annotations-on-constructor
  (is (some #(instance? Deprecated %)
            (for [ctor (.getConstructors (Class/forName "foo.Bar"))
                  annotation (.getAnnotations ctor)]
              annotation))))

Other Java examples (source code examples)

Here is a short list of links related to this Java java_5.clj source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.