jeudi 2 avril 2020

Scala reflection for nested path strings

Given a string of com.company.util.Type[com.company.controller.Response], or com.company.util.Type[scala.collection.Seq[com.company.controller.Response]] how do you use reflection to instantiate com.company.controller.Response?

I could manually use regex or the sort to find the most nested [], but my next task is to try and retain as much information of how it's wrapped as possible, for instance: I would like to know if it's been wrapped in a Seq, or Option or Some.

Using

implicit val mirror = runTimeMirror(classLoader)
mirror.staticClass("com.company.controller.Response").toType

Gives me what I want, but as soon as you introduce nested class path's like above, it understandably throws a not found exception.





Aucun commentaire:

Enregistrer un commentaire