Skip to content

Instantly share code, notes, and snippets.

@Centaur
Last active September 3, 2017 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Centaur/9543576 to your computer and use it in GitHub Desktop.
Save Centaur/9543576 to your computer and use it in GitHub Desktop.
get case class properties using reflection
import scala.reflect.runtime.{universe => ru}
import ru._
def getProperties[T: TypeTag]: Iterable[String] = {
val tpe = ru.typeTag[T].tpe
tpe.declarations.collect {
case m: MethodSymbol if m.isCaseAccessor => m.name.toString
}
}
case class User(name: String, age: Int) {
val other: Long = 0
}
assert(getProperties[User] == Iterable("name", "age"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment