Last active
September 3, 2017 11:01
-
-
Save Centaur/9543576 to your computer and use it in GitHub Desktop.
get case class properties using reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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