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