RFR: JDK-8267936: PreserveAllAnnotations option doesn't expose the annotation to Java code [v2]

Jaroslav Tulach github.com+26887752+jaroslavtulach at openjdk.java.net
Sun May 30 20:09:19 UTC 2021


On Sun, 30 May 2021 19:56:52 GMT, Jaroslav Tulach <github.com+26887752+JaroslavTulach at openjdk.org> wrote:

>> This PR exposes runtime invisible annotations via `Class.getAnnotation` when `-XX:+PreserveAllAnnotations` option is passed to the JVM.
>> 
>> Existing `-XX:+PreserveAllAnnotations` option can be very useful for code that needs to access annotations with `RetentionPolicy.CLASS` without the need to parse the .class files manually. While the RuntimeInvisibleAnnotations are kept in the runtime, they are not visible via java.lang.reflect API. I assume that's just an omission.
>> 
>> This PR provides a new test and a fix to make `Class.getAnnotation(...)` useful when `-XX:+PreserveAllAnnotations` option is on.
>
> Jaroslav Tulach has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Only expose non RetentionPolicy.RUNTIME annotations when -XX:+PreserveAllAnnotations is specified

My use-case relates to [@JavaScriptBody](https://bits.netbeans.org/html+java/1.7.1/net/java/html/js/package-summary.html) annotation used for Java/JavaScript interop. Originally all existing usages (Post Processing Classes, Instrumentation Agent, Special classloading, etc.) of the annotation performed the manipulation on `.class` files. However recently a new use-case relying on JVMCI appeared. JVMCI is working on "live" classes and doesn't provide access to the `.class` files (as far as I know, CCing @dougxc). Using `-XX:+PreserveAllAnnotation` solved the first part of problem - the HotSpot code loaded the `RuntimeInvisibleAnnotations`"in". The second part was solved by on the fly patching of `@JavaScriptBody` class to claim to have`RetentionPolicy.RUNTIME`. That part is a bit tricky, so I created this pull request for your consideration.

Obvious suggestion is to change the `@JavaScriptBody` retention to `RUNTIME` - however there are plenty of libraries already compiled with the existing annotation and they would have to be all recompiled to new version. I'd rather avoid that and that's another reason why I decided to bring the `-XX:+PreserveAllAnnotation` to your attention and propose to improve it.

-------------

PR: https://git.openjdk.java.net/jdk/pull/4245


More information about the core-libs-dev mailing list