Search code examples
clojureannotationsgen-class

Attaching metadata to a Clojure gen-class


Is it possible to attach metadata to a Clojure gen-class?

I am trying to implement a server that uses a library that requires Java annotations added to classes.

From Chas Emerick's, et al., forthcoming book "Programming Clojure" (section 9.7.3), adding annotations to gen-class methods is easy, but there is no mention of adding class-level annotations.


Solution

  • Yes it is, I found a great example here:

    https://github.com/clojure/clojure/blob/master/test/clojure/test_clojure/genclass/examples.clj

    Here's some code inlined so it doesn't disappear in the future:

    (gen-class :name ^{Deprecated {}
                       SuppressWarnings ["Warning1"] ; discarded
                       java.lang.annotation.Target []}
                     clojure.test_clojure.genclass.examples.ExampleAnnotationClass
               :prefix "annot-"
               :methods [[^{Deprecated {}
                            Override {}} ;discarded
                          foo [^{java.lang.annotation.Retention java.lang.annotation.RetentionPolicy/SOURCE
                                 java.lang.annotation.Target    [java.lang.annotation.ElementType/TYPE
                                                                 java.lang.annotation.ElementType/PARAMETER]}
                               String] void]])