将元数据附加到 Clojure gen-class

发布于 2024-12-09 01:31:52 字数 175 浏览 0 评论 0原文

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

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

从 Chas Emerick 等人即将出版的书“Programming Clojure”(第 9.7.3 节)中,向 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江挽川 2024-12-16 01:31:52

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

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

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]])
隱形的亼 2024-12-16 01:31:52

我认为目前这是不可能的。

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)`

这似乎是不可能的。

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. 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.

我爱人 2024-12-16 01:31:52

要为此添加附加信息,因为我在其他地方找不到它的记录,也可以向构造函数添加注释。

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

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

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-")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文