首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将元数据附加到Clojure gen-class

将元数据附加到Clojure gen-class
EN

Stack Overflow用户
提问于 2011-10-09 20:22:24
回答 3查看 3.8K关注 0票数 6

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

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-20 06:52:41

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

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

这里有一些内联的代码,这样它在将来就不会消失了:

代码语言:javascript
复制
(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]])
票数 24
EN

Stack Overflow用户

发布于 2011-10-09 23:37:40

我认为这在这一点上是不可能的。

Rich Hickey提到在这个线程https://groups.google.com/group/clojure/browse_thread/thread/d2128e1505c0c117中添加注释支持,但据我所知,这只适用于deftype / defrecord。当然,我也可能错了。

这两个

代码语言:javascript
复制
(ns genclass.example
  (:gen-class ^{:doc "example class"}))

代码语言:javascript
复制
(ns genclass.example)

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

无法为我编译。从异常中

代码语言:javascript
复制
Exception in thread "main" java.lang.IllegalArgumentException: Metadata can only be applied to IMetas (example.clj:4)`

听起来这是不可能的。

票数 1
EN

Stack Overflow用户

发布于 2016-06-09 12:32:17

为了添加额外的信息,因为我在其他地方找不到文档,所以也可以向构造函数添加注释。

您可以通过向构造函数对的第一个数组添加元数据来向构造函数添加注释。如下所示:

代码语言:javascript
复制
(gen-class
  :name "FooClass"
  :init "init"
  :constructors {^{Inject {}} [Configuration] []}
  :state "state"
  :implements [FooInterface]
  :prefix "ref-")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7703467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档