18

I'm learning scala and can't find out how to do this:

I'm doing a mapper between scala objects and google appengine entities, so if i have a class like this:

class Student {
    var id:Long
    var name:String
}

I need to create an instance of that class, in java i would get the Field by it's name and then do field.set(object, value) but I can't find how to do so in scala.

I can't use java reflection since the fields of Student are seen as private and field.set throws an error because of that.

Thanks

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
Damian
  • 5,471
  • 11
  • 56
  • 89

1 Answers1

42

Scala turns "var" into a private field, one getter and one setter. So in order to get/set a var by identifying it using a String, you need to use Java reflection to find the getter/setter methods. Below is a code snippet that does that. Please note this code runs under Scala 2.8.0 and handlings of duplicated method names and errors are nonexistent.

class Student {
  var id: Long = _
  var name: String = _
}

implicit def reflector(ref: AnyRef) = new {
  def getV(name: String): Any = ref.getClass.getMethods.find(_.getName == name).get.invoke(ref)
  def setV(name: String, value: Any): Unit = ref.getClass.getMethods.find(_.getName == name + "_$eq").get.invoke(ref, value.asInstanceOf[AnyRef])
}

val s = new Student
s.setV("name", "Walter")
println(s.getV("name"))  // prints "Walter"
s.setV("id", 1234)
println(s.getV("id"))    // prints "1234"
Walter Chang
  • 11,547
  • 2
  • 47
  • 36
  • Thanks! that was really helpfull, I took a part of your code and worked fine in Scala 2.7.6 – Damian Oct 19 '09 at 17:52
  • You can also directly fetch the method by name if you use getClass.getDeclaredMethod if you know in advance the method parameter types! – Vincenzo Maggio Oct 17 '12 at 07:02
  • Why is it better than setting field.setAccessible(true) ? – Yael Jul 05 '17 at 13:52
  • You can make this work with javabeans by changing the `find` method definition. E.g., for the `getV` method: `find(_.getName == s"get${lowerCamelToUpperCamel(name)}"` and for the setter `find(_.getName == s"set${lowerCamelToUpperCamel(name)}"`. You would of course also have to have a `lowerCamelToUpperCamel` function defined, e.g,. using Guava: `def lowerCamelToUpperCamel(str : String) { CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, str)}`. – RyanQuey Sep 07 '20 at 21:48