在我的 Clojure 代码中,我想生成一个包含静态方法(名为 staticMethod
)的类文件,稍后在 Java 程序的静态上下文中调用该方法.
我试过(Clojure):
(ns com.stackoverflow.clojure.testGenClass
(:gen-class
:name com.stackoverflow.clojure.TestGenClass
:prefix "java-"
:methods [
[#^{:static true} staticMethod [String String] String]
]))
(def ^:private pre "START: ")
(defn #^{:static true} java-staticMethod [this text post]
(str pre text post))
和(Java):
package com.stackoverflow.clojure;
public class TestGenClassTest {
private TestGenClassTest() {
}
public static void main(String[] args) {
TestGenClass.staticMethod("Static call from Java!", " :END");
}
}
关于 https://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html我阅读:
By adding metadata – via #^{:static true} – to a method declaration you can also define static methods.
无论我把 #^{:static true}
放在哪里,Java 编译器总是说:
Cannot make a static reference to the non-static method staticMethod(String, String) from the type TestGenClass
如何在 Clojure 中定义静态方法? #^{:static true}
和 ^:static
意思一样吗?这在哪里记录?
最佳答案
当 kotka 说要注释方法声明时,他“显然”指的是持有声明的整个vector:
:methods [^:static [staticMethod [String String] String] ]
不幸的是,这种简洁的措辞是 Clojure 文档的典型特征。
关于java - 如何使用 clojure 的 Gen 类生成静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26425098/