如果有以下类
`
public class ClojureTestBean {
private java.util.Map foo;
public void setFoo(Map foo) {
this.foo = foo;
}
public Map getFoo() {
return foo;
}
}
`
然后运行以下clojure
`
(def bean-instance (ClojureTestBean.))
(. bean-instance setFoo {"bar" "baz"})
(def bean-instance-as-map (from-java bean-instance))
(def new-bean-instance (to-java ClojureTestBean bean-instance-as-map))
`
最后一行抛出 {{java.lang.InstantiationException: java.util.Map}} 异常(完整的堆栈跟踪见下文)。看起来有一个 {{to-java}} 方法,当它接收到 {{APersistentMap}} 时,它会尝试创建参数类型的实例,在这种情况下是 {{Map}} 接口,显然是无法实例化的。
我疑惑为什么 {{APersistentMap}} 不能直接传入,因为它实现了 {{Map}} 接口?
`
编译器异常:java.lang.InstantiationException: java.util.Map,编译:(scratchpad.clj:13:24)
java.lang.InstantiationException: java.util.Map,编译:(scratchpad.clj:13:24)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3628)
at clojure.lang.Compiler$DefExpr.eval(Compiler.java:439)
at clojure.lang.Compiler.eval(Compiler.java:6787)
at clojure.lang.Compiler.load(Compiler.java:7227)
at user$eval1676.invoke(form-init2280240325964091253.clj:1)
at clojure.lang.Compiler.eval(Compiler.java:6782)
at clojure.lang.Compiler.eval(Compiler.java:6745)
at clojure.core$eval.invoke(core.clj:3081)
at clojure.main$repl$read_eval_print__7099$fn__7102.invoke(main.clj:240)
at clojure.main$repl$read_eval_print__7099.invoke(main.clj:240)
at clojure.main$repl$fn__7108.invoke(main.clj:258)
at clojure.main$repl.doInvoke(main.clj:258)
at clojure.lang.RestFn.invoke(RestFn.java:1523)
at clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__609.invoke(interruptible_eval.clj:53)
at clojure.lang.AFn.applyToHelper(AFn.java:152)
at clojure.lang.AFn.applyTo(AFn.java:144)
at clojure.core$apply.invoke(core.clj:630)
at clojure.core$with_bindings_STAR_.doInvoke(core.clj:1868)
at clojure.lang.RestFn.invoke(RestFn.java:425)
at clojure.tools.nrepl.middleware.interruptible_eval$evaluate.invoke(interruptible_eval.clj:51)
at clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__651$fn__654.invoke(interruptible_eval.clj:183)
at clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__644.invoke(interruptible_eval.clj:152)
at clojure.lang.AFn.run(AFn.java:22)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
原因:java.lang.InstantiationException: java.util.Map
at java.lang.Class.newInstance(Class.java:359)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:93)
at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:313)
at clojure.java.data$fn__247.invoke(data.clj:95)
at clojure.lang.MultiFn.invoke(MultiFn.java:233)
at clojure.java.data$make_setter_fn$fn__232.invoke(data.clj:55)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.AFn.applyTo(AFn.java:144)
at clojure.core$apply.invoke(core.clj:630)
at clojure.java.data$fn__247.invoke(data.clj:101)
at clojure.lang.MultiFn.invoke(MultiFn.java:233)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.AFn.applyTo(AFn.java:144)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3623)
`