Specification of handling of absent annotations at runtime

Chen Liang chen.l.liang at oracle.com
Mon Oct 7 19:22:27 UTC 2024


Hello Bjorn,

In JVMS 4.7, there's a minor provision right above this table https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-4.html#jvms-4.7-300:

> An implementation of the Java Virtual Machine may use the information that these attributes contain, or otherwise must silently ignore these attributes.

This means that if an annotation class is absent, the JVM will never fail. (In fact, a missing annotation interface means that the attribute itself is still conformant.) Otherwise, the JVM doesn't care about the annotations.

The annotation parsing is handled by core libraries, specifically, core reflection. The specification for parsing is available at https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/reflect/AnnotatedElement.html

It appears that this specification does not mention what happens when the annotation interface of an annotation in the class file attribute cannot be found. The reference implementation omits such annotations: https://github.com/openjdk/jdk/blob/fc7244da96a9423146c4a46bcc3bbfc205900c3b/src/java.base/share/classes/sun/reflect/annotation/AnnotationParser.java#L185 and is verified by a MissingTest.java

For history, per https://bugs.openjdk.org/browse/JDK-6322301 it seems that ignoring is the correct way; but people seem to just have forgotten to add it to the specification of AnnotatedElement.

So, in conclusion, skipping is the right way and enforced by tests. We probably should make it to the core reflection specification. Please send future discussion to the core-libs-dev list, as this problem now becomes specific to core reflection.

Regards,
Chen
________________________________
From: jdk-dev <jdk-dev-retn at openjdk.org> on behalf of Björn Kautler <Bjoern at kautler.net>
Sent: Monday, October 7, 2024 9:45 AM
To: jdk-dev at openjdk.org <jdk-dev at openjdk.org>
Subject: Specification of handling of absent annotations at runtime

Hi

Is there somewhere some definition how a JRE has to handle absent annotation classes at runtime?
All JREs I know just skip annotations if their class is not present at runtime.
But as far as I have seen neither the JLS nor the JVMS define how the situation has to be handled.
Can anyone tell me, whether there is some specification or JCK rule or similar that definitely defines that this is the way how absent annotation classes have to be handled?

Cheers
Björn

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20241007/b124f637/attachment-0001.htm>


More information about the jdk-dev mailing list