jean
08/12/2022, 9:24 AMGrégory Lureau
08/12/2022, 10:01 AMjean
08/12/2022, 11:41 AMGrégory Lureau
08/12/2022, 11:47 AMval aggVisitor = AggregatorVisitor(resolver)
val annotated = resolver.getSymbolsWithAnnotation(MyAnnotation::class.qualifiedName!!)
annotated.forEach { it.accept(aggVisitor, Unit) }
then you generate your code, and finally
environment.codeGenerator.createNewFile(
dependencies = Dependencies(aggregating = true, *aggVisitor.sources), // Here you must put all the sources aggregated
packageName = packageName,
fileName = name
).use { outputStream ->
//...
}
jean
08/12/2022, 12:09 PM@MyAnnotation
data class ClassOne(val a: String, val b: String, val c: String)
@MyAnnotation
data class ClassOne(val b: String, val c: String, val d: String)
-- generated code
enum class MyEnum { A, B, C, D }
I managed to create a separate enum for each data class, but can’t figure out how to combine them or if I can create a complete one directlyGrégory Lureau
08/12/2022, 12:16 PMdata class MyEnum(val enumName: String, val enumEntries: List<String>)
class AggVisitor : KSVisitorVoid() {
val enums = mutableListOf<MyEnum>()
override visitClass(...) {
enums.add(MyEnum(...))
}
}
with that, you can generate 1 enum at the end of the (module) compilation.
If it's multi-module it's not easily feasible afaik.Jiaxiang
08/12/2022, 5:57 PMjean
08/13/2022, 7:48 AMHristijan
10/16/2023, 11:06 PMobject GraphFactory { val list = listOf<Graph>(
module A: appends MyGraphA,
module B: appends MyGraphB,
module C (last module): appends MyGraphC) }
Jiaxiang
10/23/2023, 9:52 PMapp module
will be compiled last since compiling it before module A B C makes no sense due to not yet exist MyGraph
classes? In that case when you run KSP on app module, the corresponding MyGraph A B C should have been generated and you should be able to get them? The tricky part in this case is that you can’t get the generated MyGraph A B C
easily from KSP’s get symbols with annotation utils since these MyGraph classes are not part of the source from app module’s point of view.Hristijan
10/24/2023, 12:54 PMjean
10/24/2023, 3:33 PMJiaxiang
10/25/2023, 4:56 PMGraphFactory
and if you can get it via getClassDeclarationByName
then it is created, does it work for you?Hristijan
10/26/2023, 7:25 AMgetDeclarationsFromPackage
since I'm using a common package for the generated code classes and that's how I aggregate them, I guess it doesn't differ much from your mentioned approach since yours is directly accessing the classDeclarationBy the name?