Foso
12/06/2020, 6:40 PMshikasd
12/06/2020, 9:40 PMZac Sweers
01/12/2021, 5:51 AMshikasd
01/12/2021, 11:24 AMZac Sweers
01/12/2021, 5:55 PMshikasd
01/12/2021, 6:36 PMshikasd
01/12/2021, 8:18 PMKtFile
is pretty straight-forward, there's a factory in compiler that exposes a method to parse VirtualFile
(thin abstraction on top of disk/memory files) into `KtFile`: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]/compose/compiler/plugins/kotlin/AbstractCodegenTest.kt;l=251
IrFile
is a bit more annoying, there are a lot of weird things to setup, and you also need to assemble half of compiler here, but you can do it as well: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]pose/compiler/plugins/kotlin/AbstractIrTransformTest.kt;l=341
Otherwise, if you want to create a compiler plugin which hooks into existing compilation and analyses classes from there, you need to use extension API.
For your goals, I suggest using DeclarationChecker
, e.g. https://github.com/ShikaSD/compiler-plugin-talk-example/blob/master/compiler-plugin/src/main/kotlin/SerializationDeclarationChecker.kt. You can check the annotations, emit resources into the build folder and then merge them into final jar.
But, if you fancy something more powerful (e.g. running a separate gradle task for all of this), AnalysisHandlerExtension
is the thing you need. It is allows to fully analyse the module and then abandon codegen/return an error/retry with more files. Afaik, KSP uses it under the hood.Zac Sweers
01/12/2021, 10:12 PMAnalysisHandlerExtension
is what I'd been looking at currently