Shawn
07/19/2024, 2:13 AMval typeParameters: Array<Class<*>> by lazy {
(fieldType.type as? ParameterizedType)
?.actualTypeArguments
?.map { it as Class<*> }
?.toTypedArray()
?: emptyArray()
}
🅱️
val typeParameters: Array<Class<*>> by lazy {
when (val type = fieldType.type) {
is ParameterizedType -> type.actualTypeArguments
.map { it as Class<*> }
.toTypedArray()
else -> emptyArray()
}
}
(fieldType.type
is a property that has open or custom getter, so smart-casting can't be used with it)Shawn
07/19/2024, 2:17 AM.map { it as Class<*> }
or
.map(Class::class::cast)
Shawn
07/19/2024, 2:22 AM.filterIsInstance<Class<*>>()
might also work here, but could potentially cause a bug by quietly filtering out elements that aren't Class
instancesJoffrey
07/19/2024, 7:02 AM.toTypedArray()
Javier
07/19/2024, 11:12 AM.orEmpty()
with thatShawn
07/19/2024, 3:56 PMmap
and then force any user of said method to call .toTypedArray()
when they use it, or I could call it once here and cache the results.