Scala-类声明的字段和访问修饰符 [英] Scala -- class declared fields and access modifiers

查看:141
本文介绍了Scala-类声明的字段和访问修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Scala中尝试了Manifests之类的方法,当通过getDeclaredFields方法访问对象时,我很难找到一种使用对象字段的方法...

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...

这里是一个例子:

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),它显示了同样的例外.抛出的异常是:

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)

我已经尽了最大的努力,但目前我还没有发现任何新的想法.根据我的发现,类Woah中的val"x"和"y"被声明为private final,因为它们是在构造函数之外使用的.所以我也尝试了这个:

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))
  }
}

不幸的是,引发了相同的异常.有什么办法可以公开两个val?还是有可能通过其他途径实现相同的目标?我只是对访问类的字段的值的集合感兴趣.

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.

预先感谢您的帮助!

推荐答案

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

这篇关于Scala-类声明的字段和访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆