nadi
02/14/2024, 7:43 PM@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FUNCTION])
@Retention(AnnotationRetention.RUNTIME)
annotation class RequiredExternalUserAuth<T>(
val contextReader: KClass<ContextReader<T>>
)
i want to be able to know what T is in runtime. is it possible?David Kubecka
02/15/2024, 8:24 AMnadi
02/15/2024, 8:24 AMDavid Kubecka
02/15/2024, 8:28 AMfun Any.getGenericSupertypesArgumentClasses(): List<Class<*>> {
val superType = this::class.java.genericSuperclass as ParameterizedType
return superType.actualTypeArguments.map { Class.forName(it.typeName) }
}
For annotations you might have to modify it a bit.nadi
02/15/2024, 8:56 AMnadi
02/15/2024, 8:57 AMnadi
02/15/2024, 8:57 AMDavid Kubecka
02/15/2024, 8:59 AMnadi
02/15/2024, 8:59 AMDavid Kubecka
02/15/2024, 9:02 AMnadi
02/15/2024, 9:04 AMnadi
02/15/2024, 9:25 AMDavid Kubecka
02/15/2024, 12:54 PM/**
* If the receiver is a subclass of a generic superclass with type arguments (e.g. `Receiver : Map<Int, String>`)
* then return the type argument Classes (e.g. `[Int, String]`). Otherwise an exception is thrown.
*/
In other words, this relis on an old Java technique (hack): https://www.baeldung.com/java-generic-type-find-class-runtime#3-using-typetoken