我有这样的数据类
@Tagme("Response")
@TagResponse
data class Response(
val id: Int,
val title: String,
val label: String,
val images: List<String>,
@Input(Parameter::class)
val slug: Slug
)
使用注释处理器,我能够Response
使用这种方法获取属性:
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.
}
}
以前我试图得到这样的注释:
val annotations = prop.type?.annotations
但是,我得到了空列表,甚至isAnnotationAvailable
值是true
提前致谢!