/
GetAnnotationByTypeProcessor.kt
36 lines (31 loc) · 1.34 KB
/
GetAnnotationByTypeProcessor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.google.devtools.ksp.processor
import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
annotation class KotlinAnnotationWithInnerDefaults(
val innerAnnotationVal: InnerAnnotation = InnerAnnotation(innerAnnotationDefault = 7)
) {
annotation class InnerAnnotation(
val innerAnnotationDefault: Int,
val moreInnerAnnotation: MoreInnerAnnotation = MoreInnerAnnotation("OK")
) {
annotation class MoreInnerAnnotation(val moreInnerAnnotationDefault: String)
}
}
class GetAnnotationByTypeProcessor : AbstractTestProcessor() {
val results = mutableListOf<String>()
private val annotationKClass = KotlinAnnotationWithInnerDefaults::class
override fun toResult(): List<String> {
return results
}
@OptIn(KspExperimental::class)
override fun process(resolver: Resolver): List<KSAnnotated> {
val decl = resolver.getAllFiles().single().declarations
.single { it.simpleName.asString() == "A" } as KSClassDeclaration
val anno = decl.getAnnotationsByType(annotationKClass).first()
results.add(anno.innerAnnotationVal.toString())
return emptyList()
}
}