I have been able to get a List of attributes for a case class using scala with Reflection.
import scala.reflect.runtime.universe._
def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
case m: MethodSymbol if m.isCaseAccessor => m
}.toList
case class Z(a1: String, b1: String, id: Integer)
val z = classAccessors[Z]
val res1 = z.map(x => if(x.equals("value id")) "xxx" else x)
However, the
.equalsdoes not work, but gives no error -> so I am missing something and I can't google it. Must be something basic.
.replacedoes not work, how would that go?.How do I just get a normal List for processing? I note a
List[Object]. Did not find anything either in that regard. Mmm. Can someone give an example of not using Reflection?
xhas typeMethodSymbol. Soif(x.equals("value id")) "xxx" else xhas typeAny(akaObject) because this is the minimal supertype ofStringandMethodSymbol.And
x.equals("value id")is alwaysfalsebecauseMethodSymbolandStringare different types.Try
or
Please notice that Java
.equalsis like Scala==, Java==is like Scalaeq.