I’m trying to obtain all the functions annotated w...
# ksp
j
I’m trying to obtain all the functions annotated with a specific annotation while processing another annotation but I always get an empty list from
resolverd.resolver.getSymbolsWithAnnotation
Any idea why? Some context:
Copy code
@AnnotationA
fun someFun1() = ...

@AnnotationA
fun someFun2() = ...
Those are processed to create an enumeration like
Copy code
enum class FunAfunctions {
  SomeFun1,
  SomeFun2,
}
Later I process another annotation which uses this enum as a parameter type
Copy code
@AnnotationB(FunAfunctions.SomeFun1)
fun anotherFunction() = ...
the thing is while I generate the code associated with
AnnotationB
I would like to check if the
AnnotationA
associated with is as parameter is a suspending function. For that I was thinking of passing down the
Resolver
object to my generator class and call
resolverd.resolver.getSymbolsWithAnnotation
like I do to generate the enum, but I always get a empty list as a result
j
What is the argument you pass for calling
getSymbolsWithAnnotation
the second time? Is it
AnnotationB
?
Also please don’t keep
resolver
object on your own,
resolver
are meant to be recreated every round to reflect new changes, old resolver object won’t be able to know what has changed across rounds, besides it also creates memory leak issues.
j
No it is annotationA since I want to find the function annotated with @AnnotationA that has a name matching the enum value used in @AnnotationB. The problem might be coming from holding on the resolver then
I use
resolver.getSymbolsWithAnnotation(AnnotationA::class.qualifiedName!!)
to generate an enum file. Will the resolver return anything when I call it a second time later on during the generation of code for annotation
AnnotationB
? Does it has some sort of logic checking if some code was generated after resolving the annotation to not return the annotated code later?
j
If you have code generated, you should rely on the multiple round mechanism in KSP to let KSP pick up generated files.
j
Should I create an internal metadata enum specifying which function uses suspend then? Something like this?
Copy code
// Created based on @AnnotationA

// the enum exposed to the consumer of the generated code
enum class FunAfunctions {
  SomeFun1,
  SomeFun2,
}

// internal enum to use when generating code for AnnotationB
internal enum class FunAfunctionsMetadata(val isSuspending: Boolean) {
  SomeFun1(false),
  SomeFun2(true),
}
And then when I process an
AnnotationB
I should get a hold on the generated
FunAfunctionsMetadata
? But is that possible since the code inside my generator probably don’t have access to generated code?
Can I use something like this in my generator code?
ClassName(MY_PACKAGE, "FunAfunctionsMetadata")
and somehow convert the
ClassName
type to the actual
FunAfunctionsMetadata
type so I can access
FunAfunctionsMetadata.SomeFun1.isSuspending
? Is that possible?
j
This requires the said
FunAfunctionsMetadata
being on classpath which is not possible due to this is generated. For checking if a function is suspend function, you can check
KSFunctionDeclaration.modifiers
contains
SUSPEND
j
Yes but the problem is that to get a hold on
KSFunctionDeclaration
I use
resolver.getSymbolsWithAnnotations
but it returns nothing since I already generated the enum before
But it seems like
resolver.getKClassDeclarationByName
works? It does return the class declaration of the generated code. I need to see if I can read the data inside the generated enum that way though.
j
I am interested in the part where you said
resolver.getSymbolsWithAnnotations
returned nothing part, do you have a reproduce case?
j
I’ll try to make one 🙂
The project I work on is quite big, so not easy to share