Is it possiable to write a kotlin compiler plugin to modify classes from all modules

Is it possible to write a kotlin compiler plugin to modify classes from all modules in multiplatform
I already tried IrGenerationExtension and found it can only modify current module.if I reference another module,I can’t find classes from external module.
And I did google for search infomation, and found nothing useful ,and there seams no kotlin compiler plugin documents.
what I want to do is:
1.collect classes in all modules(some modules may close source) with special Annotation
2.generate a new function for each collected classes
3.modify a special class’s init function , replace it’s body to invoke all functions prev step generated.
Is it possible to do this?
If it’s possible, please help me, Thank you!

and sorry for my English…

Best regards
LeeHo

You can’t do that with a compiler plugin.

If you are working on JVM, have a look at https://asm.ow2.io. I think this does exactly what you would like to achieve.

Thank you for reply,Yeah,ASM can do this,but it can only do this for jvm.what I want to do is targeting Kotlin Multiplatform,so Kotlin/JS also my target.
Thank you to tell me it can’t be done with kotlin compiler plugin.
Maybe If I put all kotlin source files in one single project(abandon close source lib),IrGenerationExtension should just work fine…So maybe It’s only solution for now.
Thank you very much.

Best Regards
LeeHo

Putting everything into one project and avoiding closed source might solve the problem, but not necessarily.

The big question in this case is the one of dependencies.

Case 1

You have no dependencies you want to transform.

You can use single or multi-project setup and your compiler plugin.

Case 2

You have only open source Kotlin dependencies.

Download the source code, compile them with your plugin active and use the locally compiled code in your project.

This works. However, it will be harder and harder to maintain in the future as you have to recompile those dependencies every time they change. Not to mention you might have to recompile the whole dependency tree… it’s a mess.

Case 3

You have only Kotlin dependencies (might be closed source) and all your dependencies are compiled with the IR compiler.

You might be able to solve this in a general way, but it will be difficult. It also have the problem of maintenance and deeper dependencies as in Case 2.

Case 4

You have platform dependent dependencies like JS libraries with JavaScript source or JVM libraries with Java source (or Kotlin but not compiled with IR),

You cannot use the Kotlin compiler to solve this problem.

Conclusion

I would choose case 1 and avoid dependencies I want to transform.

The init function

Also, there is one more thing to mention here.

Remember, that init runs only when you create an instance of that class. For objects it is even more complex as they may be initialised lazily. So, for example you cannot count on the init function to build up an object registry because anything you don’t touch won’t be in that registry.

It might be possible to solve your problem without a compiler plugin. It might be even better to do it without a compiler plugin.

Thank you again,tothiz!
for case 1:
it’s truly simplest way, and this can easily done by my current compiler plugin code.
for case 2:
compare to case 1 it’s main problem is how to maintain open source dependencies,If there has an easy way to solve this problem, maybe this can be the final solution.(but still not support closed source dependencies)

for case 3:
this case almost is my facing case.only one working kotlin project will apply my compiler plugin,and all denpendencies are available as kotlin source or kotlin IR compiled lib.

Should we discuss this?Currently I stuck in find a way to do this… And still don’t know how to process IR compiled dependencies(collect classes from it).

Conclusion
case 4 is not my case, so we can abandon it.
case 3 is almost my case, so if there has a solution ,it will be wonderful…
if case 3 can not be done,I will try to fallback to case 2, and try to find an easy way to maintain open source dependencies.

and the init function which I generated is in a class who has a way to auto invoke init function.so
this function like a auto register for collected classes with some other infomation

Best Regards
LeeHo

As for option 3. If you have an IR compiled lib then you have a *.klib file which - as far as I know - is the IR representation of the code. Nothing stops you from going over that file and collect the classes you would like to modify.

Then you can modify the IR itself (probably difficult) or use that information in your project to do whatever. Which way is better depends on your use case.