java - 如何使用 clojure 的 Gen 类生成静态方法?

标签 java clojure static static-methods gen-class

在我的 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/

相关文章:

java - BufferedReader 读取一个为 null 的值

java - 保存临时数组列表以在适配器中使用

swing - Clojure 找不到方法 JComboBox.addItemListener

clojure - 为什么字符集看起来是有序的?

clojure - 使变量常量用于 Clojure 中的 case 语句

Java单例-空静态变量(无法解释)

c++ - 如何在不同的 dll 中强制销毁静态对象的顺序?

python - Django 静态文件再次不起作用

java - 分离的 API 和实现项目结构

Java 在构造函数中设置私有(private)字段