macros - 使用 gen-class 的 clojure 宏不会创建注释

标签 macros clojure annotations gen-class

我正在尝试编写一个 clojure 宏,该宏将用于在编译时生成多个 Java 类。我发现当我在宏之外调用 gen-class 时,我可以向类添加注释。但是,当我尝试在宏中使用 gen-class 时,编译的类没有注释。

我把我的问题归结为这个例子:

(gen-class
  :name ^{Deprecated true} Test1
  :prefix Test1-
  :methods [[^{Deprecated true} getValue [] Integer]])

(defn Test1-getValue [] 42)

(defmacro create-test-class [name x]
  (let [prefix (str name "-")]
    `(do
      (gen-class
         :name ~(with-meta name {Deprecated true})
         :prefix ~(symbol prefix)
         :methods [[~(with-meta 'getValue {Deprecated true}) [] Integer]])
      (defn ~(symbol (str prefix "getValue")) [] ~x))))

(create-test-class Test2 56)

当我编译这个文件时,它会创建一个 Test1.class 和 Test2.class - 我用 Eclipse 进行检查,发现 Test1 具有类级和方法级 @Deprecated 注释,但 Test2.class 没有注释。当我使用 macroexpand 时,看起来我的 Test2.class 应该被注释:
user=> (set! *print-meta* true)
true
user=> (macroexpand '(create-test-class Test2 56))
(do (clojure.core/gen-class :name ^{java.lang.Deprecated true} Test2 :prefix Test2- :methods [[^{java.lang.Deprecated true} getValue [] java.lang.Integer]]) (user/defn Test2-getValue [] 56)) 

我在这里做错了什么?

最佳答案

梅克尔·布兰德迈耶在这里回答了这个问题:

https://groups.google.com/forum/#!topic/clojure/Ee1bVwcUT-c

“引用宏中的注释。(带有元名称`{Deprecated true})。注意反引号。”

这是工作宏:

(defmacro create-test-class [name x]
  (let [prefix (str name "-")]
    `(do
      (gen-class
         :name ~(with-meta name `{Deprecated true})
         :prefix ~(symbol prefix)
         :methods [[~(with-meta 'getValue `{Deprecated true}) [] Integer]])
      (defn ~(symbol (str prefix "getValue")) [] ~x))))

关于macros - 使用 gen-class 的 clojure 宏不会创建注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15123214/

相关文章:

c++ - 类似于函数的宏不接受嵌套的#define

macros - 在 Haxe 宏中读取元数据

java - 如何验证 hibernate validator 上的所有组?

java - 带 @annotation(...) 的 Spring AOP 在某些情况下似乎不起作用

c - 用 C 宏替换部分函数/变量名

macros - 预处理器宏在 Windows 上的 Fortran 代码中不起作用

clojure - 我怎样才能更地道地编写这个 Clojure 宏?

sqlite - 当我在 korma 中调用插入多行时,出现异常

clojure.core 取消引用和取消引用拼接

java - Hibernate 局部变量的自定义注释