问题调用clojure函数,该功能从Java中获取地图作为参数

发布于 2025-02-13 20:01:01 字数 1339 浏览 1 评论 0原文

我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2025-02-20 20:01:01

我猜您的错误是由中的此定义引起的: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这样:

(defn -non_empty_seats
  [java-seats]
  (into {} (-filter-empty-seats java-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, but filter in -filter-empty-seats returns instance of clojure.lang.LazySeq. You should rewrite -non_empty_seats like this:

(defn -non_empty_seats
  [java-seats]
  (into {} (-filter-empty-seats java-seats)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文