Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala -- class declared fields and access modifiers

Tags:

field

scala

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!

like image 591
jwinder Avatar asked Jul 20 '11 02:07

jwinder


People also ask

Does Scala have access modifiers?

Access Modifiers in scala are used to define the access field of members of packages, classes or objects in scala. For using an access modifier, you must include its keyword in the definition of members of package, class or object. These modifiers will restrict accesses to the members to specific regions of code.

What is the default access modifier in Scala?

public, private and protected are the three access modifiers used in Scala. Include the keywords private/protected/public in the definition of class, object or package to enforce the corresponding scope. If none of these keywords are used, then the access modifier is public by default.

What is the default access level to fields in Scala?

The default access level (when no modifier is specified) corresponds to Java's public access level. Unlike in Java, in Scala members that are protected are not accessible from other unrelated classes, traits or objects in the same package.

What is private [] in Scala?

In Scala,private[this] is object private,which makes sure that any other object of same class is unable to access private[this] members. Example.


1 Answers

class Woah(val x: String, val y: String) {
  def printParams = classOf[Woah].getDeclaredFields foreach { field =>
    field.setAccessible(true)
    println(field.get(this))
  }
}
like image 74
Don Roby Avatar answered Nov 20 '22 00:11

Don Roby