<@U0NH24H1V> Unfortunately this is all undocumente...
# compiler
t
@lukaville Unfortunately this is all undocumented and this wisdom seems to be mostly passed on through word of mouth; I don't know of any good article that summarizes it. My advice would be to look at discussions on github issues of projects like the Kotlin Gradle Plugin or Buck build system. Sometimes you get lucky and a JB employee left a helpful comment. For a very high level overview you can also look at my kotlin-compile-testing libary which implements a simple build process with Java sources and annotation processors: https://github.com/tschuchortdev/kotlin-compile-testing/blob/master/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt Basically the build process is divided into 4 steps: 1. Generate stubs: The Kotlin compiler generates Java stub files which is like a Kotlin-to-Java source-to-source translation except that all the methods are empty (since the annotation processor doesn't care about that) 2. Run KAPT: The KAPT plugin is run which runs Java's APT tool internally (this is why the stub files are needed: APT only understands Java) 3. Compile Kotlin: K2JVMCompiler is run to compile only Kotlin sources. I think it actually does some compilation of Java sources internally, possibly by reusing Javac logic. Otherwise you wouldn't be able to reference Java classes in Kotlin 4. Compile Java: The remaining Java files are compiled regularly using Javac with already compiled Kotlin classes on the classpath. No annotation processing is done. Keep in mind that compiler internals are changing constantly and also heavily depend on the compiler flags. For example you can (theoretically) skip some of the steps by including certain compiler flags that may or may not be broken at any time.
🙏 1
g
3. As I understand it is a bit different, kotlinc doesn't reuse any javac for Java interop, instead compiler has part of IDEA which can get class declarations from Java sources without compiling it (interpretation?) and use this knowledge about Java declarations to compile Kotlin to reference Java classes
r
I can't recall the name exactly but there are facades for java files that can be overriden by compiler plugins in the StorageComponentContributor.registerComponents phase
You can there use container.impl to inject your facade or inject any object on which you want other services injected that then you can delegate or override
I think you can potentially intercept the calls before javac is called
At that point the container isn't wired yet and can't be used but it can be added new beans that are later available in all other phases where you have access to the container
I was able to replace this way the type checker, body and function resolvers and add callcheckers that can intercept resolution, then with the synthetic resolver extension you can replace discovered descriptors and use the ktpsifactory to create new code and descriptors with the binding context
I don't know how many know this kind of stuff that are not part of the compiler team but I thought I'd share this here based on my experience of trial and error given no docs for plugins