min
09/01/2026, 11:51 AMannotation class that accepts a KClass<*>. If I want to retrieve the type in my compiler plugin,
@OptIn(SymbolInternals::class) annotatedClass.fir.annotations
.first { annot ->
annot.annotationTypeRef.coneType.classId == ANNOTATION_CLASS
}
.argumentMapping
.mapping
.values
.single()
.resolvedType
.classId!!,
Is this the best I can do?min
09/01/2026, 11:52 AMfirst predicate
annot.annotationTypeRef.coneType.classId == ANNOTATION_CLASS
and this alternative?
@OptIn(UnresolvedExpressionTypeAccess::class)
annot.coneTypeOrNull?.classId == ANNOTATION_CLASSYoussef Shoaib [MOD]
09/01/2026, 12:08 PMFirAnnotation.getXArgument family of extensions that can help you here. getKClassArgument should be the one you're looking for (source)min
09/01/2026, 12:29 PM.getKClassArgument(Name.identifier("kClass")) should do the job then, right? (Is there a better way to obtain the Name, eg by querying the annotation class?) This is so much better, thank you!min
09/01/2026, 12:34 PM@OptIn(SymbolInternals::class) annotatedClass.fir.annotations
.getAnnotationByClassId(classId = ANNOTATION_CLASS, session = this.session)
And I think this is better than
@OptIn(SymbolInternals::class) annotatedClass.fir.annotations
.first { annot ->
annot.annotationTypeRef.coneType.classId == ANNOTATION_CLASS
}
This current version. But are there any subtle differences I should be aware of? Or can it be improved even further?min
09/01/2026, 12:39 PMannotatedClass
.getAnnotationByClassId(classId = ANNOTATION_CLASS, session = this.session)!!Youssef Shoaib [MOD]
09/01/2026, 12:47 PMgetAnnotationByClassId seems to be better AFAIK. In general, I prefer using these utilities than accessing argumentMapping and such directly
Re: querying the name: I think you can use session.symbolProvider.getClassLikeSymbolByClassId (source) to get the annotation class. What I've seen people do instead is add the relevant annotations library as a dependency in their plugins, and thus get access to the names from there (e.g. by doing MyAnnotation::kClass.name)min
09/02/2026, 7:01 AMgetAnnotationByClassId. Thanks for helping me discover it! Also, you mean by doing Name.identifier(MyAnnotation::kClass.name), right? I also like this, thank you!