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

Peter Levart plevart at openjdk.java.net
Mon May 31 12:05:23 UTC 2021


On Mon, 31 May 2021 06:06:37 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 refreshed the contents of this pull request, and previous commits have been removed. The incremental views will show differences compared to the previous content of the PR.

A suggestion for this RFR. Maybe the title of the issue could be rephrased as: "Add a test for -XX+PreserveAllAnnotations option"
A test could be constructed so that it would mimic the migration of an annotation from CLASS to RUNTIME retention by using separate compilation of:

1. an annotation with CLASS retention together with a class that uses it to annotate itself
2. the same type of annotation but with RUNTIME retention

The test would use a class from compilation (1) combined with annotation from compilation (2) together with `-XX+PreserveAllAnnotations` JVM option to verify that the annotation is visible via reflection. No ClassLoader magic is necessary.

WDYT?

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

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


More information about the hotspot-runtime-dev mailing list