Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Clojure中引用基本Java类型?_Java_Clojure_Java Interop - Fatal编程技术网

如何在Clojure中引用基本Java类型?

如何在Clojure中引用基本Java类型?,java,clojure,java-interop,Java,Clojure,Java Interop,我想使用反射从Clojure获取Java对象的方法。其中一种参数类型是Java原语,我不知道如何从Clojure引用它们 例如,说我想得到。我最近的猜测是 (.getDeclaredMethod String "valueOf" (into-array [Boolean])) 但是这失败了,因为Boolean不是原语类型本身,而是装箱版本。我尝试了boolean,但它引用了一个内置的Clojure函数,bool是未定义的 如何在Clojure中引用原语Java类型?您可以通过其装箱等价物的ty

我想使用反射从Clojure获取Java对象的方法。其中一种参数类型是Java原语,我不知道如何从Clojure引用它们

例如,说我想得到。我最近的猜测是

(.getDeclaredMethod String "valueOf" (into-array [Boolean]))
但是这失败了,因为
Boolean
不是原语类型本身,而是装箱版本。我尝试了
boolean
,但它引用了一个内置的Clojure函数,
bool
是未定义的


如何在Clojure中引用原语Java类型?

您可以通过其装箱等价物的
type
属性来引用原语类型。例如:

user=> (.getDeclaredMethod String "valueOf" (into-array [Boolean/TYPE]))
#<Method public static java.lang.String java.lang.String.valueOf(boolean)>
user=>(.getDeclaredMethod字符串“valueOf”(放入数组[Boolean/TYPE]))
#

请注意,如果您想为一组原语找到Java类对象,可以使用以下技巧:

; An instance of the java.lang.Class<XXXX[]> (e.g. java.lang.Class<Byte[]>). 
(def ^:private  class-boolean-array (.getClass (boolean-array   0)))
(def ^:private  class-byte-array    (.getClass (byte-array      0)))
(def ^:private  class-char-array    (.getClass (char-array      0)))
(def ^:private  class-double-array  (.getClass (double-array    0)))
(def ^:private  class-float-array   (.getClass (float-array     0)))
(def ^:private  class-int-array     (.getClass (int-array       0)))
(def ^:private  class-long-array    (.getClass (long-array      0)))
(def ^:private  class-object-array  (.getClass (object-array    0)))
(def ^:private  class-short-array   (.getClass (short-array     0)))
(defn boolean-array?
  "Returns true is the arg is a boolean array, else false."
  [arg]
  (= class-boolean-array (.getClass arg)))