Sasha Shpota
01/04/2021, 9:41 AMorg.jetbrains.kotlin.psi.KtProperty
, form which I need to get annotations. So far, I found a way to do it like this:
property.annotationEntries.any {
it.typeReference
?.typeElement
?.safeAs<KtUserType>()
?.referencedName == "MyAnnotation"
}
But referencedName
doesn't return a fully qualified name (MyAnnotation
instead off com.example.MyAnnotation
)
Question: How to check if a KtProperty
is annotated with a particular annotation?Ilmir Usmanov [JB]
01/12/2021, 10:49 AMfun KtProperty.hasMyAnnotation(bindingContext: BindingContext): Boolean {
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as? PropertyDescriptor ?: return false
return descriptor.annotations.hasAnnotation(FqName("com.example.MyAnnotation"))
}
Sasha Shpota
01/12/2021, 12:33 PMSyntheticResolveExtension.generateSyntheticProperties
and there, I can iterate over property descriptors. The only issue I have with this, is that it doesn't count annotations declared on properties of data classes. For those, I have to check annotations on constructor parameters.Ilmir Usmanov [JB]
01/12/2021, 6:26 PMSasha Shpota
01/12/2021, 6:36 PM