clojure - 无法使用 ns 中另一个命名空间的类 :gen-class

标签 clojure clojure-java-interop

我在 sre.plan.dsl.constraint 命名空间中有一个名为 ConstraintLookupdefrecord。 我想在位于 sre.plan.compiler 命名空间的 gen-class 方法中使用其生成的类:

(ns sre.plan.compiler
  (:require
    [sre.plan.dsl.constraint :as constraint])
  (:import (sre.plan.dsl.constraint ConstraintLookup))
  (:gen-class
    :name sre.plan.Compiler
    :methods [^:static [makeConstraintLookupFromTargetsAndBounds 
                         [Iterable Iterable] ConstraintLookup]]))

我正在使用 nebula-clojure 插件和 Gradle 进行 AOT 编译。编译器遇到 ns 声明时会发出错误:

> Task :sre:compileClojure
Exception in thread "main" java.lang.ClassNotFoundException: java.lang.ConstraintLookup, compiling:(sre/plan/compiler.clj:1:1)

类似地,当在方法声明中使用完全限定的 sre.plan.dsl.constraint.Constraint 时,我得到:

Exception in thread "main" java.lang.ClassNotFoundException: sre.plan.dsl.constraint.ConstraintLookup, compiling:(sre/plan/compiler.clj:1:1)

这里有什么问题吗?我迷路了。

更新:

引用的 ns 如下所示:

(ns sre.plan.dsl.constraint
  (:require [clojure.set :refer :all]
            [clojure.algo.generic.functor :refer :all]))

(defrecord ConstraintLookup [free bound])

更新:

在我看来,无论如何,在 gen-class 中你都必须使用完全限定的类名。但是我仍然不明白为什么具有完全限定名称的版本不起作用。

最佳答案

ns 宏内的 :gen-class 指令很可能无法引用作为 :require 的副作用生成的类> 以相同的形式。 ns 宏发出的代码在调用任何 require 之前调用 gen-class。因此,当调用 gen-class 时,所需的命名空间尚未编译。 gen-class 在生成 defrecord 中的任何类之前调用​​。

可以看到ns的行为in the source code以及在 repl 中使用 macroexpand:

(clojure.pprint/pprint (macroexpand '(ns sre.plan.compiler
  (:require
    [sre.plan.dsl.constraint :as constraint])
  (:import (sre.plan.dsl.constraint ConstraintLookup))
  (:gen-class
    :name sre.plan.Compiler
    :methods [^:static [makeConstraintLookupFromTargetsAndBounds 
                         [Iterable Iterable] ConstraintLookup]]))))
;; (do
;;  (clojure.core/in-ns 'sre.plan.compiler)
;;  (clojure.core/with-loading-context
;;    (clojure.core/gen-class
;;     :name
;;     "sre.plan.compiler"
;;     :impl-ns
;;     sre.plan.compiler
;;     :main
;;     true
;;     :name
;;     sre.plan.Compiler
;;     :methods
;;     [[makeConstraintLookupFromTargetsAndBounds
;;       [Iterable Iterable]
;;       ConstraintLookup]])
;;    (clojure.core/refer 'clojure.core)
;;    (clojure.core/require '[sre.plan.dsl.constraint :as constraint])
;;    (clojure.core/import '(sre.plan.dsl.constraint ConstraintLookup)))
;;   (if
;;    (.equals 'sre.plan.compiler 'clojure.core)
;;    nil
;;    (do
;;     (clojure.core/dosync
;;      (clojure.core/commute
;;       @#'clojure.core/*loaded-libs*
;;       clojure.core/conj
;;       'sre.plan.compiler))
;;     nil)))

要解决此问题,我们可以在 ns 之后调用 gen-class。例如:

(ns sre.plan.compiler
  (:require
    [sre.plan.dsl.constraint :as constraint])
  (:import (sre.plan.dsl.constraint ConstraintLookup)))

(gen-class
 :impl-ns
 sre.plan.compiler
 :main
 true
 :name
 sre.plan.Compiler
 :methods
 [[makeConstraintLookupFromTargetsAndBounds
   [Iterable Iterable]
   sre.plan.dsl.constraint.ConstraintLookup]])

关于clojure - 无法使用 ns 中另一个命名空间的类 :gen-class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899142/

相关文章:

java - 将 Clojure 夹在 Java 与 Leiningen 之间

arrays - 将包含可能为空的原语数组的嵌套数据结构与 clojure.data/diff 进行比较

Clojure 映射-最长

ubuntu - 在 ubuntu 服务器上使用插孔的实时音频流

dictionary - 在 Clojure 中如何创建 "add id to map"函数?

clojure - 处理子 lambda 失败

math - 为什么 Clojure 返回 1N 而不是 1?

clojure - 是否可以将 Clojure 的案例形式与 Java 枚举一起使用?

clojure gen 类可变参数构造函数

clojure - 实用 Clojure : macros and Java interop