Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import scala.reflect.runtime.universe.{NoSymbol, TermName}
  2. import scala.reflect.{classTag, ClassTag}
  3.  
  4. object ReflectionUtils {
  5.  
  6. def defaultValues[T: ClassTag]: Map[String, Any] = {
  7. import scala.reflect.runtime.currentMirror
  8.  
  9. val classSymbol = currentMirror.classSymbol(classTag[T].runtimeClass)
  10. val moduleMirror = currentMirror.reflectModule(classSymbol.companion.asModule)
  11. val instanceMirror = currentMirror.reflect(moduleMirror.instance)
  12. val instanceTypeSignature = instanceMirror.symbol.typeSignature
  13. val applyMirror = instanceTypeSignature.member(TermName("apply")).asMethod
  14.  
  15. applyMirror.paramLists.flatten.zipWithIndex
  16. .map {
  17. case (arg, index) =>
  18. arg.name.toString -> instanceTypeSignature.member(TermName(s"apply$$default$$${index + 1}"))
  19. }
  20. .filterNot(_._2 == NoSymbol)
  21. .map { case (name, value) => name -> instanceMirror.reflectMethod(value.asMethod)() }
  22. .toMap
  23. }
  24.  
  25. def instantiate[T: ClassTag](values: Map[String, Any]): T = {
  26. import scala.reflect.runtime.currentMirror
  27.  
  28. val classSymbol = currentMirror.classSymbol(classTag[T].runtimeClass)
  29. val moduleMirror = currentMirror.reflectModule(classSymbol.companion.asModule)
  30. val instanceMirror = currentMirror.reflect(moduleMirror.instance)
  31. val instanceTypeSignature = instanceMirror.symbol.typeSignature
  32. val applyMirror = instanceTypeSignature.member(TermName("apply")).asMethod
  33.  
  34. val args = applyMirror.paramLists.flatten.zipWithIndex.map {
  35. case (arg, index) =>
  36. val name = arg.name.toString
  37.  
  38. if (values.contains(name))
  39. values(name)
  40. else
  41. instanceTypeSignature.member(TermName(s"apply$$default$$${index + 1}")) match {
  42. case NoSymbol =>
  43. throw new Exception(s"Missing value for argument $name")
  44. case value: Any =>
  45. instanceMirror.reflectMethod(value.asMethod)()
  46. }
  47. }
  48.  
  49. instanceMirror.reflectMethod(applyMirror)(args: _*).asInstanceOf[T]
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement