Hi, I have a problem while writing a compiler plu...
# compiler
m
Hi, I have a problem while writing a compiler plugin. My goal is to collect functions with a specific annotation and create a class for each of them. The problem is that the class needs to be placed inside a different package than the actual function and i have no idea how to do this. Is this even possible with current apis?
s
You can try to use
PackageFragmentProvider
for this, it is a bit similar to what
kotlinx.synthetic.
does on android. Also, one of the solutions is to generate Kotlin files inside
build
folder and restart analysis with them through
RetryWithAdditionalRoots
in AnalysisHandler.
m
Thanks the AnalysisHandler solution is what i go for. I have another question. I generate those classes to get access to annotated functions from different compilations/modules because i need to create a class (kind of like ServiceLoader) which uses all annotated functions in the whole classpath. Do you think there's a better way to collect them?
s
It is hard to tell without knowing why you need to use them. Is it a bootstrapper / di of some kind?
@ralf
SyntheticResolveExtension
has something similar, it allows you to add supertypes to the class. I actually think
serialization
does its somewhere, but I can be wrong. Discovering classes in classpath can be harder, because compiler does not go through them directly. Maybe you can use some gradle magic to find such classes in moduleA/B and provide it to moduleC.
r
Thanks, I appreciate the response. Discovering the classes is the crucial part. Since this is for the JVM only, I probably will go with an annotation processor instead. I hoped that I would be able to use a compiler plugin, but it looks like it’s still too early or maybe my use case is not meant to be covered.
Actually, using
SyntheticResolveExtension
in conjunction with the annotation processor could make the integration easier. Thanks for the hint, I keep that in my back pocket.