如何在Clojure中实现Java接口

如何创建实现此接口的Clojure对象,然后从Java代码调用?

public interface Doer { public String doSomethin(String input); } Doer clojureDoer = ?; String output = clojureDoer.doSomethin(input); 

reify非常适合实现接口 – proxy是繁重的,旧的和慢的,所以应该尽可能避免。 实现看起来像:

 (reify Doer (doSomethin [this input] (...whatever...))) 

请注意,关于使用proxy的现有答案具有不正确的语法,如果您决定最终使用代理:proxy采用隐式this参数,而不是命名的第一个参数。

从Clojure 1.6开始 ,首选方法如下。 假设您在类路径上有Clojure 1.6 jar和以下clojure文件(或其编译的等效文件):

 (ns my.clojure.namespace (:import [my.java.package Doer])) (defn reify-doer "Some docstring about what this specific implementation of Doer does differently than the other ones. For example, this one does not actually do anything but print the given string to stdout." [] (reify Doer (doSomethin [this in] (println in)))) 

然后,从Java,您可以按如下方式访问它:

 package my.other.java.package.or.maybe.the.same.one; import my.java.package.Doer; import clojure.lang.IFn; import clojure.java.api.Clojure; public class ClojureDoerUser { // First, we need to instruct the JVM to compile/load our // Clojure namespace. This should, obviously, only be done once. static { IFn require = Clojure.var("clojure.core", "require"); require.invoke(Clojure.read("my.clojure.namespace")); // Clojure.var() does a somewhat expensive lookup; if we had more than // one Clojure namespace to load, so as a general rule its result should // always be saved into a variable. // The call to Clojure.read is necessary because require expects a Clojure // Symbol, for which there is no more direct official Clojure API. } // We can now lookup the function we want from our Clojure namespace. private static IFn doerFactory = Clojure.var("my.clojure.namespace", "reify-doer"); // Optionally, we can wrap the doerFactory IFn into a Java wrapper, // to isolate the rest of the code from our Clojure dependency. // And from the need to typecast, as IFn.invoke() returns Object. public static Doer createDoer() { return (Doer) doerFactory.invoke(); } public static void main(String[] args) { Doer doer = (Doer) doerFactory.invoke(); doer.doSomethin("hello, world"); } } 

有代理

请参阅proxy宏。 Clojure Docs有一些例子。 它也包含在Java Interop页面上。

 (proxy [Doer] [] (doSomethin [input] (str input " went through proxy"))) 

proxy返回实现Doer的对象。 现在,要使用Java访问它,您必须使用gen-class来使您的Clojure代码可以从Java调用。 它在“从java调用clojure”问题的答案中得到了解答。

与gen-class

 (ns doer-clj (:gen-class :name DoerClj :implements [Doer] :methods [[doSomethin [String] String]])) (defn -doSomethin [_ input] (str input " went through Clojure")) 

现在将它保存为doer_clj.cljmkdir classes ,并通过调用REPL (require 'doer-clj) (compile 'doer-clj) doer_clj.clj (require 'doer-clj) (compile 'doer-clj)编译它(require 'doer-clj) (compile 'doer-clj) 。 你应该找到准备在classes目录中从Java中使用的DoerClj.class

有关这个问题的更一般性看法,当您需要某种Java-interop时,此图表可能非常有用:

https://github.com/cemerick/clojure-type-selection-flowchart

如果在界面中定义了doSomethin()则不应在:methods提及它。 引自http://clojuredocs.org/clojure_core/clojure.core/gen-class :

 :methods [ [name [param-types] return-type], ...] The generated class automatically defines all of the non-private methods of its superclasses/interfaces. This parameter can be used to specify the signatures of additional methods of the generated class. Static methods can be specified with ^{:static true} in the signature's metadata. Do not repeat superclass/interface signatures here.