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

Alan Bateman alanb at openjdk.java.net
Sun May 30 07:34:30 UTC 2021


On Fri, 28 May 2021 12:56:39 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.

JVMS 4.7.17 does allow for an implementation specific means to make available these annotations at run-time. I think we need to look into the history of `-XX:+PreserveAllAnnotations` to know if it was introduced for testing the VM code or for making available these otherwise invisible annotations at run-time as you propose. I'm also wondering how it might be used, is it to avoid tools needing to parse class files? is it because someone has missed RetentionPolicy.RUNTIME somewhere?

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

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


More information about the core-libs-dev mailing list