0

Is there any way to do something similar, using java>9, to what is done in this article where it adds the final modifier to private variables?

I know tools.jar has been declared as legacy but how does lombok still work in java11?

I know there are some trees described here that allows you to parse the code but you can't tamper with it. Is there any other alternative ? What was the reasoning behind making tools.jar unavailable ?(i know it can still be used if you add some JVM flags but who would want to use something that can be changed without notice).

user1934513
  • 693
  • 4
  • 21
  • The annotation processing API does not support modifying existing source code. Lombok works by _hacking_ into the internals of the compiler (the implementation, not just the interfaces) and directly modifying the AST, if I'm not mistaken. – Slaw Oct 21 '20 at 19:09
  • if I look in the source code of lombok it seems they're using classes from tools.jar like JCCompilationUnit but I don't understand how it works on java versions higher than 8 – user1934513 Oct 21 '20 at 19:14
  • When `tools.jar` was removed they moved the code into JDK-specific modules. For example, the `javac` implementation is now in the `jdk.compiler` module. That module contains the `JCCompilationUnit` class. I'm not exactly positive _how_ they access the class, as it's in a non-exported package, but that's _where_ it is. – Slaw Oct 21 '20 at 22:07
  • Actually I can't find on the oracle jdk 11 page for jdk.compiler https://docs.oracle.com/en/java/javase/11/docs/api/java.compiler/module-summary.html – user1934513 Oct 22 '20 at 05:37
  • The only api documented allows you to read the AST, not temper with it – user1934513 Oct 22 '20 at 05:46
  • First, it's `jdk.compiler`, not `java.compiler`, that contains the `javac` implementation. Second, classes such as `JCCompilationUnit` are in _non-exported packages_ and thus won't appear in the API documentation; that's why I used the word "hacking" to describe what Lombok does—it depends on implementation details. – Slaw Oct 22 '20 at 07:37
  • seems that you can do some reflection on the available Tree scanners which point to some com.sun.tools objects which can be modified with reflection. Thanks Oracle. – user1934513 Oct 22 '20 at 10:41

0 Answers0