如何从Java调用Clojure宏?

前端之家收集整理的这篇文章主要介绍了如何从Java调用Clojure宏?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有从 Java调用Clojure宏?

这是我正在努力做的:

RT.var("clojure.core","require").invoke(Symbol.create("clojure.contrib.prxml"));
Var prxml = RT.var("clojure.contrib.prxml","prxml");
Var withOutStr = RT.var("clojure.core","with-out-str");
String stringXML = (String) withOutStr.invoke((prxml.invoke("[:Name \"Bob\"]")));

prxml默认写入* out *,这就是为什么我需要使用返回字符串的-out-str来包装宏.

我收到这个错误

[java] java.lang.IllegalArgumentException: Wrong number of args (1) passed to: core$with-out-str
 [java]     at clojure.lang.AFn.throwArity(AFn.java:437)
 [java]     at clojure.lang.RestFn.invoke(RestFn.java:412)
 [java]     at clojure.lang.Var.invoke(Var.java:365)
 [java]     at JavaClojure.xml.main(Unknown Source)

解决方法

你必须用OutStr自己滚动.
class YourClass {
    static final Var withBindings = RT.var("clojure.core","with-bindings*");
    static final Var list = RT.var("clojure.core","list*");
    static final Var out = RT.var("clojure.core","*out*");
    static final Var prxml = RT.var("clojure.contrib.prxml","prxml");

    static String withOutStr(IFn f,Object args...) {
        StringWriter wtr = new StringWriter();
        withBindings.applyTo(list.invoke(RT.map(out,wtr),f,args));
        return wtr.toString();
    }

    ...

    String stringXML = withOutStr(prxml,"[:Name \"Bob\"]");
}

猜你在找的Java相关文章