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字段_Clojure - Fatal编程技术网

无法访问clojure中的java字段

无法访问clojure中的java字段,clojure,Clojure,我遇到了一个问题,我可以看到Java类/对象的字段,但实际上无法访问它们。我可以通过两种方式看到这些字段。使用以下代码 =>(require '[clojure.reflect :as r]) =>(use '[clojure.pprint :only [print-table]]) =>(print-table (sort-by :name (:members (r/reflect myClass)))) 而且,通过创建对象的实例。假设这些字段是一个int

我遇到了一个问题,我可以看到Java类/对象的字段,但实际上无法访问它们。我可以通过两种方式看到这些字段。使用以下代码

=>(require '[clojure.reflect :as r])    
=>(use '[clojure.pprint :only [print-table]])    
=>(print-table (sort-by :name (:members (r/reflect  myClass))))
而且,通过创建对象的实例。假设这些字段是一个int表示的a、一个字符串表示的word和一个字符串ArrayList表示的mylist

=>myObj
#<myClass 1 hello [world]>
有人知道发生了什么事吗


作为对Nicolas Modrzyk回答的回应,我运行(.-MyField myObject)并获取 IllegalArgumentException未找到匹配字段:类myClass clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:308)的myField

此外,这些字段不是私有的。我面前有Java源代码。

的正确表示法略有不同:

   (.-fieldName instance)
下面是java类文件及其私有字段路径的完整示例:


缩略图的评论是正确的。我不知道Java的默认访问权限是包。从那以后,我通过反射进入了这些领域。谢谢大家的帮助。

它们可能是私人领域。你不能从clojure访问它们,就像你不能从java访问它们一样。如果这个问题至少包含了来自打印调用表的部分输出,那么我们就可以知道实际字段名是什么是公共字段,这将非常有帮助?如果不是,Java中的默认访问权限是package。您的clojure代码可能正在另一个版本中运行,因此无法访问。请向我们展示Java代码。
-
不是必需的,但推荐使用。
   (.-fieldName instance)
    (require '[clojure.reflect :as r])   
    (use '[clojure.pprint :only [print-table]])    

    (import '[java.io File])
    (def f (File. "test.txt"))

    ; access a public static field
    (. File separator)
    ; "/"

    (print-table 
      (sort-by :name (:members (r/reflect  File))))

    (.-path f)
    ; java.lang.IllegalArgumentException: 
    ; No matching field found: path for class java.io.File

    (def field (.getDeclaredField File "path"))
    ; you need the below if the field is private
    (.setAccessible field true)

    ; get the value
    (.get field f)
    ; "test.txt"