How to access a field's value via reflection (Scala 2.8)

前端 未结 5 1975
北海茫月
北海茫月 2021-02-08 00:18

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:

<
相关标签:
5条回答
  • 2021-02-08 00:31
    foo.getClass.getDeclaredField("name").getString(foo)
    

    should work if you want to avoid asInstanceOf. get* is available for various types

    0 讨论(0)
  • 2021-02-08 00:40

    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]
    
    0 讨论(0)
  • 2021-02-08 00:41

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

    0 讨论(0)
  • 2021-02-08 00:43

    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]

    0 讨论(0)
  • 2021-02-08 00:43

    This is how one can get list of fieldnames and its value of a case class:
    First, using reflection, get fields info as follows -

    val TUPLE2_OF_FIELDNAME_TO_GETTERS = typeOf[<CLASS>].members
    .filter(!_.isMethod)
    .map(x => (x.name.toString, classOf[<CLASS>].getDeclaredMethod(x.name.toString.trim)))
    

    How to use it?

    getFieldNameAndValue(obj: <CLASS>): Seq[(String, String)] {
      var output = Seq[(String, String)]()
     for(fieldToGetter <- TUPLE2_OF_FIELDNAME_TO_GETTERS) {
          val fieldNameAsString = fieldToGetter._1
          val getter = fieldToGetter._2
          val fieldValue = getter.invoke(obj).toString
          output += (fieldName, fieldValue)
        }
    }
    
    0 讨论(0)
提交回复
热议问题