I need to get only field names of case class. I'm not interested in its values.
I thought getClass.getDeclaredFields.map(_.getName)
would return a list of field names.
scala> case class User(id: Int, name: String)
defined class User
scala> User.getClass.getDeclaredFields
res14: Array[java.lang.reflect.Field] = Array(public static final User$ User$.MODULE$)
scala> User.getClass.getDeclaredFields.toList
res15: List[java.lang.reflect.Field] = List(public static final User$ User$.MODULE$)
scala> val user = User(1, "dude")
user: User = User(1,dude)
scala> user.getClass.getDeclaredFields.toList
res16: List[java.lang.reflect.Field] = List(private final int User.id, private final java.lang.String User.name)
What is this User$.MODULE$? What's that?
Method getDeclaredFields works fine when you have an instance of a case class, but I don't want to create an instance in order to get only fields.
Why this isn't true:
User.getClass.getDeclaredFields.map(_.getName) == List("id", "name")
?