https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Milse113

02/11/2022, 8:53 PM
Hey — I have a module system in my project which allows me to write generalized extensions and features for my project. At the moment I register these modules by manually adding it to a list in a manager class. I want to try to clean up this process a bit by adding an
@Register
annotation which allows the manager class to automatically register it. While I could do this with reflection, I want to be able to include and exclude some of these modules at compile-time (IE if I have a beta vs prod build), and preferably also generate the list at compile-time. from what I can tell, the best way to do this is with a compiler plugin, but I can’t find a framework which allows this as most major Kotlin compiler plugin libraries don’t allow modifying existing files, but instead only creating new ones. How should I go about doing this?
j

Joffrey

02/11/2022, 9:34 PM
Why not just generate a new file that contains the list, and access that list from the manager? I don't believe you need to modify existing files.
Maybe you can check Kotlin Symbol Processing (KSP)
m

Milse113

02/11/2022, 10:21 PM
@Joffrey because I also want to remove the unused code from the compiled binary
so I was thinking I’d just have something that gut the module contents
j

Joffrey

02/13/2022, 9:38 PM
If you're on the JVM you can compile different modules into different JARs and just not put the ones you don't want on the classpath when running the code. It depends on what kind of software you're packaging I guess
m

Milse113

02/13/2022, 9:40 PM
That probably wouldn’t work for my use case. Is there really no solution for excluding things at compile-time based on annotations?
j

Joffrey

02/13/2022, 9:43 PM
But if those are already modules, I don't quite get from which compilation you want to exclude them
m

Milse113

02/13/2022, 9:49 PM
@Joffrey Essentially the registry annotation also contains an enum which notates whether it’s ready for production, beta, or development builds. I want to be able to create different build configurations for these modes which includes or excludes these things based on the annotation
3 Views