简体   繁体   English

将元数据附加到Clojure gen-class

[英]Attaching metadata to a Clojure gen-class

Is it possible to attach metadata to a Clojure gen-class? 是否可以将元数据附加到Clojure gen-class?

I am trying to implement a server that uses a library that requires Java annotations added to classes. 我正在尝试实现一个使用库的服务器,该库需要将Java注释添加到类中。

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. 从Chas Emerick等人,即将出版的书“Programming Clojure”(第9.7.3节)中,向gen-class方法添加注释很容易,但没有提到添加类级别的注释。

Yes it is, I found a great example here: 是的,我在这里找到了一个很好的例子:

https://github.com/clojure/clojure/blob/master/test/clojure/test_clojure/genclass/examples.clj 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]])

To add additional information to this because I can't find it documented anywhere else it's possible to add annotations to constructors as well. 要向其添加其他信息,因为我无法在其他任何地方找到它,因此也可以向构造函数添加注释。

You can add annotations to constructors by adding meta data to the first array of the constructor pair. 您可以通过将元数据添加到构造函数对的第一个数组来向构造函数添加注释。 Like this: 像这样:

(gen-class
  :name "FooClass"
  :init "init"
  :constructors {^{Inject {}} [Configuration] []}
  :state "state"
  :implements [FooInterface]
  :prefix "ref-")

I don't think it is possible at this point. 我认为此时不可能。

Rich Hickey mentions adding annotations support in this thread https://groups.google.com/group/clojure/browse_thread/thread/d2128e1505c0c117 but as far as I can see this is only for deftype / defrecord. Rich Hickey提到在此主题https://groups.google.com/group/clojure/browse_thread/thread/d2128e1505c0c117中添加注释支持,但据我所知,这仅适用于deftype / defrecord。 I could be wrong of course. 我当然错了。

Both of these 这两个

(ns genclass.example
  (:gen-class ^{:doc "example class"}))

and

(ns genclass.example)

(with-meta
  (gen-class
   :name genclass.example.ClassA
   :methods [[hello [] void]])
  {:doc "Example class"})      

fail to compile for me. 没能为我编译。 From the exception 从例外

Exception in thread "main" java.lang.IllegalArgumentException: Metadata can only be applied to IMetas (example.clj:4)`

It sounds like this is not possible. 听起来这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM