问题调用clojure函数,该功能从Java中获取地图作为参数
我将Maven与几个模块一起使用,一个在Java,另一个在Clojure中。我正在调用Java的Clojure函数,并希望通过哈希图作为参数传递并返回hashmap。 (我在Clojure项目上运行了Lein Uberjar和Lein Pom,以使其与Maven一起使用。我可以使用简单类型的clojure功能来工作,例如String,因此Maven设置确实可以工作。)
当我在我时会遇到以下错误。运行一些呼叫Java代码的Java单元测试:
java.lang.ClassCastException: class clojure.lang.LazySeq cannot be cast to class java.util.Map (clojure.lang.LazySeq is in unnamed module o
f loader 'app'; java.util.Map is in module java.base of loader 'bootstrap')
如何使此功能工作?这是从Java调用Clojure方法的正确方法吗? 如果哈希图将POJO对象作为一个值而不是字符串,该怎么办?
我的Java代码:
import interop.Core;
public class BillingCalc {
static Map<String, String> nonEmptyItems(Map<String, String> items) {
return Core.non_empty_seats(new HashMap<String, String>());
}
}
我的Clojure代码:
(ns interop.core
(:gen-class
:name interop.Core
:methods [^{:static true} [apply_vat_to_netto [java.math.BigDecimal java.math.BigDecimal] java.math.BigDecimal]
^{:static true} [non_empty_seats [java.util.Map] java.util.Map]]) )
(defn -filter-empty-seats
"filter out those with empty seats"
[seats]
(filter (fn [[_ v]] (pos? (:booked-items v))) seats))
(defn -non_empty_seats
[java-seats]
(-filter-empty-seats (into {} java-seats)))
I'm using maven with several modules, one in java, another in clojure. I'm calling a clojure function from java and want to pass in a HashMap as a parameter and return a HashMap.
(I ran lein uberjar and lein pom on the clojure project to make it work with maven. I can get things to work for clojure function with simple types e.g. String, so the maven setup does work.)
I am getting the following error when I run some java unit tests calling the java code:
java.lang.ClassCastException: class clojure.lang.LazySeq cannot be cast to class java.util.Map (clojure.lang.LazySeq is in unnamed module o
f loader 'app'; java.util.Map is in module java.base of loader 'bootstrap')
How can I get this to work? Is this the proper way to call clojure methods from java?
What about if the HashMap had a POJO object as a value rather than a String?
My java code:
import interop.Core;
public class BillingCalc {
static Map<String, String> nonEmptyItems(Map<String, String> items) {
return Core.non_empty_seats(new HashMap<String, String>());
}
}
My clojure code:
(ns interop.core
(:gen-class
:name interop.Core
:methods [^{:static true} [apply_vat_to_netto [java.math.BigDecimal java.math.BigDecimal] java.math.BigDecimal]
^{:static true} [non_empty_seats [java.util.Map] java.util.Map]]) )
(defn -filter-empty-seats
"filter out those with empty seats"
[seats]
(filter (fn [[_ v]] (pos? (:booked-items v))) seats))
(defn -non_empty_seats
[java-seats]
(-filter-empty-seats (into {} java-seats)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您的错误是由
中的此定义引起的:gen-class
:[non_empty_seats [java.util.map] java.util.map]]
for < a href =“ https://clojurecs.org/clojure.core/gen-class” rel =“ nofollow noreferrer“>:gen-class :
:方法[[name [param-types] return-type],...]
返回值的预期类型为
java。 util.map
,但是filter
in-filter-empty-seats
返回的实例clojure.lang.lazyseq
。您应该重写-non_empty_seats
这样:I guess that your error is caused by this definition in
:gen-class
:[non_empty_seats [java.util.Map] java.util.Map]]
From docs for :gen-class:
:methods [ [name [param-types] return-type], ...]
The expected type of returned value is
java.util.Map
, butfilter
in-filter-empty-seats
returns instance ofclojure.lang.LazySeq
. You should rewrite-non_empty_seats
like this: