views:

309

answers:

4

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

+2  A: 

getDeclaredField is a method of java.lang.Class.

You have to change foo.getDeclaredField("name") to foo.getClass.getDeclaredField("name") (or classOf[Foo].getDeclaredField("name")) to get the field.

You can get the type with getType method in class Field but it won't help you because it returns Class[_]. Given than you know that the type is a String you can always cast the value returned using field.get(foo).asInstanceOf[String]

german1981
I fixed the missing getClass, this was only an oversight.I wonder how to do the cast without knowing what it is beforehand. If I knew everything about the class I have to handle i wouldn't need reflection...Thanks for the suggestions!
soc
You can pattern match the object with something like `value match { case s: String => /* do something with a string */ case _ => /* do something else */ }`. Don't know if I understand what you want to do.
german1981
A: 

AFAIK, reflection always work with Object, and you have to cast the results yourself.

Daniel
Yes, I had misunderstood the question, now I have changed mi answer.
german1981
+4  A: 

As others have mentioned, the reflection methods return Object so you have to cast. You may be better using the method that the Scala compiler creates for field access rather than having to change the visibility of the private field. (I'm not even sure if the name private field is guaranteed to be the same as that of the accessor methods.)

val foo = new Foo
val method = foo.getClass.getDeclaredMethod("name")
val value = method.get(foo).asInstanceOf[String]
Ben Lings
A: 

It looks like that is not possible to do what I want with out trying all possible types by hand e. g.

o match {
    case I  if o.isInstanceOf[Int] => val i = I.asInstanceOf[Int] 
    case F  if o.isInstanceOf[Float] => val f = I.asInstanceOf[Float] 
    case S  if o.isInstanceOf[String] => val s = I.asInstanceOf[String] 
    case OF if o.getGenericType.toString == "scala.Option<java.lang.Float>" => val of = OF.asInstanceOf[Option[Float]]
    .
    .
    .

Well, let's hope there will be a language in the future where something like this works ...

soc