Search code examples
javagradlelomboksun

How does Lombok use internal API's without requiring the end-user to export them?


I'm trying to make an annotation processor similar to Lombok but the issue I'm getting is all projects that use my processor as a library require for jvm arguments to export / open the internal JVM libs. How can I use the internal API's without any other projects needing them?

I have tried to

  1. Use the tools.jar instead of adding jvm exports
  2. Tried to open the libs instead of exporting them
  3. just tried a bunch of random things to try get it to work

Right now, any projects using my lib require the following in their build.gradle.

compileJava {
    options.fork = true
    options.forkOptions.jvmArgs += [
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED'
    ]
}

Solution

  • To do it, you need to mess around a bit with the internal API and call the function that is called when the param is provided to open the API. If anyone wants an example of how to do this, it is here

    Note: This code will not work after JDK26 or maybe even before that.

    Disclaimer: This code was written by me