【发布时间】:2014-04-08 16:12:56
【问题描述】:
我有一个想在 Java 中使用的 Clojure 库,但我无法生成正确的类。我想生成一个有效的类:
public class TokenStore {
public static TokenStore Sns(Map<String, String> services) { /* ... */ }
public String addToken(String token, String username, String service) { /* ... */ }
public String findToken(String username, String service) { /* ... */ }
public String sendNotification(String username, String service, String message) { /* ... */ }
}
这是我在 Clojure 中尝试做的事情:
(ns my.lib.token-java
(:require [my.lib.token :refer :all])
(:gen-class
:name my.lib.TokenStore
:methods [#^{:static true} [Sns [java.util.Map] TokenStore]]))
(defprotocol TokenStore
(addToken [this token username service])
(findToken [this username service])
(sendNotification [this username service message]))
(defn -Sns [services]
(let [ts (token-store (apply concat (for [[k v] (into {} coll)] [(keyword k) v]))]
(reify TokenStore
(addToken [this token username service]
(add-token this token username service))
(findToken [this username service]
(find-token this username service))
(sendNotification [this username service message]
(send-notification this username service message)))))
但是当我尝试编译它时,我得到:
: jmglov@alhana; lein jar
Compiling orron-iii.push-java
Exception in thread "main" java.lang.ClassNotFoundException: java.lang.TokenStore, compiling:(orron_iii/push_java.clj:1:1)
谁能给我一个简单的例子来说明如何做到这一点?
【问题讨论】:
-
尝试将 :methods 部分中的 TokenStore 更改为 my.lib.TokenStore。
-
不,这没有帮助。
-
是的,您的协议和类具有相同名称这一事实可能会导致冲突,因为协议会导致生成自己的接口。我的建议是使用您想要的方法签名在 Java 中定义 TokenStore 接口,然后在 Clojure 中创建一个工厂类来生成该接口的实现。但你的方式也行得通。