Scala fiddle here
import scala.reflect.ClassTag
import scala.language.existentials
class SomeClass[T <: SomeClass[T, R], R] extends EarlyInit[T, R] {}
trait EarlyInit[T <: SomeClass[T, R], R] {
self: SomeClass[T, R] =>
val clazz = getClass.getSuperclass
val potentialFields = clazz.getDeclaredClasses.toList
potentialFields.foreach {
field => {
// This correctly prints out fields of object members.
// but invokation throws an error every time.
println(s"${field.getName}")
}
}
}
class SomeThing extends SomeClass[SomeThing, Any] {
val stringTest = "test"
object name
object test
}
object SomeThing extends SomeThing {}
val x = SomeThing
How do I access and initialise object members(name
and test
) with reflection?
You can achieve this with scala reflection API:
You need to make
EarlyInit
a class in order to be able to get aTypeTag
evidence. Then you just search for all modules and access them (they would be initialized in the process)Update
You can also simplify
EarlyInit
a bit and get rid of type parameter. You actually can get aType
ofthis
directly from mirror: