What I am trying to do is to make a function that would take a generic class and use a static method in it (sorry for Java language, I mean method of its companion object).<
I keep hitting this page when I forget how to do this and the answers are not hundred percent satisfactory for me. Here is how I do with reflection:
val thisClassCompanion = m.reflect(this).symbol.companion.asModule
val structural = m.reflectModule(thisClassCompanion)
.instance.asInstanceOf[{def doSth: Unit}]
You might need to verify that the class actually has a companion object or companion.asModule will throw a reflection exception is not a module
Updated: added another example for clarity:
object CompanionUtil {
import scala.reflect.runtime.{currentMirror => cm}
def companionOf[T, CT](implicit tag: TypeTag[T]): CT = {
Try[CT] {
val companionModule = tag.tpe.typeSymbol.companion.asModule
cm.reflectModule(companionModule).instance.asInstanceOf[CT]
}
}.getOrElse(throw new RuntimeException(s"Could not get companion object for type ${tag.tpe}"))
}