是否可以将元数据附加到Clojure gen-class?
我正在尝试实现一个服务器,该服务器使用库,该库需要在类中添加Java注释。
在Chas Emerick等人即将出版的"Programming Clojure“一书(第9.7.3节)中,向gen-class方法添加注释很容易,但没有提到添加类级别的注释。
发布于 2013-04-20 06:52:41
是的,我在这里找到了一个很好的例子:
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]])发布于 2011-10-09 23:37:40
我认为这在这一点上是不可能的。
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)`听起来这是不可能的。
发布于 2016-06-09 12:32:17
为了添加额外的信息,因为我在其他地方找不到文档,所以也可以向构造函数添加注释。
您可以通过向构造函数对的第一个数组添加元数据来向构造函数添加注释。如下所示:
(gen-class
:name "FooClass"
:init "init"
:constructors {^{Inject {}} [Configuration] []}
:state "state"
:implements [FooInterface]
:prefix "ref-")https://stackoverflow.com/questions/7703467
复制相似问题