https://kotlinlang.org logo
#compiler
Title
# compiler
m

MerlinTHS

11/26/2023, 5:42 PM
Hi, I have an annotation class to mark interfaces for some modifications. A user can create a custom annotation class for his own purpose (for example his annotation processor with KSP) and annotate his annotation with the one my compiler plugin uses. Now I want to get all classes that have annotations annotated with my annotation. Is there an idiomatic way to look up these classes in a FirDeclarationGenerationExtension?
d

dmitriy.novozhilov

11/26/2023, 6:45 PM
Meta annotations can not be used for searching for declarations, only to check if some given declaration is annotated with it
m

MerlinTHS

11/26/2023, 7:02 PM
So “Note that [MetaAnnotatedWith] predicate has no implementation in [LookupPredicate] hierarchy and can not be used for global lookup” means, I cannot register a DeclarationPredicate for meta annotations in my extension? I can only use it for filtering already matched symbols?
j

Javier

11/26/2023, 7:16 PM
As a workaround the classId of that annotation can be passed as compiler argument so you do not force to use any annotation to your users
👍 1
r

ralf

11/27/2023, 5:24 AM
[LookupPredicate] allows user to get all declarations in current module
Is there a mechanism for the entire classpath yet? There was a way to do this in K1, it was slow but possible. The Anvil compiler plugin generated code in a specific package to speed up look up times.
d

dmitriy.novozhilov

11/27/2023, 10:28 AM
So “Note that [MetaAnnotatedWith] predicate has no implementation in [LookupPredicate] hierarchy and can not be used for global lookup” means, I cannot register a DeclarationPredicate for meta annotations in my extension? I can only use it for filtering already matched symbols?
Yes. It's done in this way because of technical limitation For lookup predicates compiler generates indices of annotated declaration before all analysis phases (to make lookups fast) And including meta-annotations for lookups causes a lot of problems with this approach, because it requires multiple rounds of this indexing and problems with invalidating of those indices if on real analysis stage some annotation on declaration will be resolved to different one Also there is a cycle in logic: • meta-annotated annotation may be generated by the plugin • before invoking plugins compiler should build indices • to build complete index compiler should consider annotation generated by plugin
1
Is there a mechanism for the entire classpath yet?
There isn't (and not actually planned)