I want to inject certain values onto the fields of my target that are annotated with a specific annotation.
I find these fields, I categorize them correctly but when I try to set them I get object is not an instance of declaring class
.
When I call my method I already know that the given field is instance of a subtype of Component
.
My code is this:
@Suppress("UNCHECKED_CAST")
private fun injectComponent(field: KProperty<*>) {
val componentType = field.returnType.jvmErasure as KClass<Component>
val value = owner.getComponent(componentType) ?: throw InjectionTargetNotFoundException(componentType)
field.isAccessible = true
field.call(value)
}
Also it is quite ugly to have that @Suppress("UNCHECKED_CAST")
. I just need it because owner.getComponent()
expects a KClass<T : Component>
.
And it returns the result I expect it to return.
Aucun commentaire:
Enregistrer un commentaire