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")
?
By using User.getClass
, you are referring to the class companion object that Scala by default creates for the case class, and not the case class itself. To get the class object of the case class, use classOf[User]
.
Alternatively, you could use Scala's reflection API to get the metadata of a case class, which gives you much more information:
import scala.reflect.runtime.universe._
def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
case m: MethodSymbol if m.isCaseAccessor => m
}.toList
Test in sbt console:
scala> case class User(name: String, age: Int)
defined class User
scala> classAccessors[User]
res0: List[reflect.runtime.universe.MethodSymbol] = List(value age, value name)
Starting Scala 2.13
, case classes
(which are an implementation of Product
) are now provided with a productElementNames method which returns an iterator over their field's names.
From an instance of the case class (let's say case class Person(name: String, age: Int)
), one can retrieve a List
of its fields:
Person("hello", 28).productElementNames.toList
// List[String] = List(name, age)
Following Andrey Tyukin solution, to get only the list of fields in Scala 2.12:
val fields: List[String] = classOf[Dummy].getDeclaredFields.map(_.getName).toList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With