繁体   English   中英

将元数据附加到Clojure gen-class

[英]Attaching metadata to a Clojure gen-class

是否可以将元数据附加到Clojure gen-class?

我正在尝试实现一个使用库的服务器,该库需要将Java注释添加到类中。

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

是的,我在这里找到了一个很好的例子:

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

这里有一些内联代码,所以它不会在将来消失:

(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]])

要向其添加其他信息,因为我无法在其他任何地方找到它,因此也可以向构造函数添加注释。

您可以通过将元数据添加到构造函数对的第一个数组来向构造函数添加注释。 像这样:

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

我认为此时不可能。

Rich Hickey提到在此主题https://groups.google.com/group/clojure/browse_thread/thread/d2128e1505c0c117中添加注释支持,但据我所知,这仅适用于deftype / defrecord。 我当然错了。

这两个

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

(ns genclass.example)

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

没能为我编译。 从例外

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

听起来这是不可能的。

暂无
暂无

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

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