我在sre.plan.dsl.constraint名称空间中有一个名为ConstraintLookup的defrecord。我想在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进行编译。编译器在遇到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中,无论如何都必须使用完全限定的类名。然而,我仍然不明白为什么带有完全限定名的版本不起作用。
发布于 2017-08-29 10:00:20
ns宏内的:gen-class指令很可能不会引用作为:require的副作用以相同形式生成的类。由ns宏发出的代码在调用任何require之前调用gen-class。因此,在调用gen-class时,所需的命名空间尚未编译。在从defrecord生成任何类之前,都会调用gen-class。
ns的行为可以在in the source code中看到,也可以在使用macroexpand的repl中看到
(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]])https://stackoverflow.com/questions/45899142
复制相似问题