Scala——类声明的字段和访问修饰符

发布于 2024-11-25 07:25:32 字数 2384 浏览 2 评论 0原文

我一直在 Scala 中尝试清单等,并且我很难找到一种在通过 getDeclaredFields 方法访问时使用对象字段的方法...

这是一个示例:

class Woah(val x: String, val y: String)

val w = new Woah("w_x", "w_y")
classOf[Woah].getDeclaredFields foreach (field => println(field.get(w))

我尝试了很多变体,例如创建一个Woah 类内部的方法执行与第三行代码相同的操作,但将 field.get(w) 替换为 field.get(this),并且它显示相同的异常。抛出的异常是:

java.lang.IllegalAccessException: Class Fun$Woah$$anonfun$1 can not access a member of class Fun$Woah with modifiers "private final"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.reflect.Field.doSecurityCheck(Field.java:960)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:896)
    at java.lang.reflect.Field.get(Field.java:358)
    at Fun$Woah$$anonfun$1.apply(Fun.scala:17)
    at Fun$Woah$$anonfun$1.apply(Fun.scala:17)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
    at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:35)
    at Fun$Woah.<init>(Fun.scala:17)
    at Fun$.main(Fun.scala:20)
    at Fun.main(Fun.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:81)
    at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:86)
    at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:81)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:86)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:83)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

我已经进行了尽可能多的搜索,目前没有找到任何新的想法。据我所知,Woah 类中的值“x”和“y”被声明为私有final,因为它们在构造函数之外使用。所以我也尝试了这个:

class Woah(val x: String, val y: String) {
  def printParams = classOf[Woah].getDeclaredFields foreach { field =>
    println(field.get(this))
  }
}

不幸的是抛出了同样的异常。有什么办法让这两个值公开吗?或者是否有可能通过其他途径实现同样的目标?我只是对访问类字段值的集合感兴趣。

预先感谢您的帮助!

I have been experimenting with Manifests and such in Scala, and I am having a very hard finding a way to use an object's fields when accessed via the getDeclaredFields method...

Here is an example:

class Woah(val x: String, val y: String)

val w = new Woah("w_x", "w_y")
classOf[Woah].getDeclaredFields foreach (field => println(field.get(w))

I have tried many variations, such as creating a method inside of the class Woah that performs that same action as the third line of code, but replace field.get(w) with field.get(this), and it shows the same exception. The exception thrown is:

java.lang.IllegalAccessException: Class Fun$Woah$anonfun$1 can not access a member of class Fun$Woah with modifiers "private final"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.reflect.Field.doSecurityCheck(Field.java:960)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:896)
    at java.lang.reflect.Field.get(Field.java:358)
    at Fun$Woah$anonfun$1.apply(Fun.scala:17)
    at Fun$Woah$anonfun$1.apply(Fun.scala:17)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
    at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:35)
    at Fun$Woah.<init>(Fun.scala:17)
    at Fun$.main(Fun.scala:20)
    at Fun.main(Fun.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.util.ScalaClassLoader$anonfun$run$1.apply(ScalaClassLoader.scala:81)
    at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:86)
    at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:81)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:86)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:83)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

I have done as much searching as I can, and I am not finding any new ideas at the moment. From what I could find, the vals "x" and "y" in the class Woah are declared as private final because they are used outside of the constructor. So I also tried this:

class Woah(val x: String, val y: String) {
  def printParams = classOf[Woah].getDeclaredFields foreach { field =>
    println(field.get(this))
  }
}

Unfortunately the same exception is thrown. Is there any way to have the two vals be public? Or is it possible to accomplish the same goal through another avenue? I am simply interested in accessing a collection of a class's fields' values.

Thank you in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海不蓝 2024-12-02 07:25:32
class Woah(val x: String, val y: String) {
  def printParams = classOf[Woah].getDeclaredFields foreach { field =>
    field.setAccessible(true)
    println(field.get(this))
  }
}
class Woah(val x: String, val y: String) {
  def printParams = classOf[Woah].getDeclaredFields foreach { field =>
    field.setAccessible(true)
    println(field.get(this))
  }
}
我们的影子 2024-12-02 07:25:32

Scala 中的所有 JVM 字段都是私有的。在 Scala 中,只能通过 getter 来访问它们,因此它们与无参数方法之间没有区别。

All JVM fields in Scala are private. Access to them is made only through getters, in Scala, so that there's no distinction between them and a parameterless method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文