Hi, I am working on annotation processor. I try to get the annotations that data class constructor parameter has (example see slug
has @Input(Parameter::class)
). But I cannot find the way to do it.
I have data class and annotations:
@Retention(AnnotationRetention.RUNTIME) // try this with .SOURCE also
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class GqlInput (
val input: KClass<*>
)
@Query("Query")
@Response
data class Response(
val id: Int,
val title: String,
val label: String,
val images: List<String>,
@Input(Parameter::class)
val slug: Slug
)
I am able to loop through the constructor parameter using kotlinpoet-metadata (which is extended version of kotlinx-metadata-jvm) by this way:
val parentMetadata = (element as TypeElement).toImmutableKmClass()
parentMetadata.constructors[0].valueParameters.map { prop ->
// this will loop through class properties and return ImmutableKmValueParameter
// eg: id, title, label, images, slug.
// then, I need to get the annotation that property has here.
// eg: @Input(Parameter::class) on slug property.
// if prop is slug, this will return true.
val isAnnotationAvailable = prop.hasAnnotations
// Here I need to get the annotated value
// val annotation = [..something..].getAnnotation(Input::class)
// How to get the annotation? So I can do this:
if ([prop annotation] has [@Input]) {
do the something.
}
}
I was post this on stackoverflow, but there’s no kotlinx-metadata tag (not enough point to create new tag). So I’ll also post this here. If you want to answer on stackoverflow: Getting annotation from constructor value parameter using kotlinpoet-metadata - Stack Overflow
Thanks in advance