How to access a field's value via reflection (Scala 2.8)
Posted
by soc
on Stack Overflow
See other posts from Stack Overflow
or by soc
Published on 2010-04-17T10:39:41Z
Indexed on
2010/04/17
10:43 UTC
Read the original article
Hit count: 310
Consider the following code:
class Foo(var name: String = "bar")
Now i try to get the value and the correct type of it via reflection:
val foo = new Foo
val field = foo.getDeclaredField("name")
field.setAccessible(true)
//This is where it doesn't work
val value = field.get(????)
I tried things like field.get(foo), but that just returns an java.lang.Object but no String. Basically I need the correct type, because I want to invoke a method on it (e. g. toCharArray).
What is the suggested way to do that?
© Stack Overflow or respective owner