Extend TypeNotPresentException probing to nested annotations - #37157
Open
junhyeong9812 wants to merge 1 commit into
Open
Extend TypeNotPresentException probing to nested annotations#37157junhyeong9812 wants to merge 1 commit into
junhyeong9812 wants to merge 1 commit into
Conversation
AttributeMethods probes Class, Class[], and enum attributes so that annotations whose values cannot be resolved at runtime are filtered during scanning. Annotation-typed attributes were not probed, and probing them by invocation alone is not sufficient: the JDK returns the nested annotation proxy successfully and only throws when one of its own attributes is accessed. An annotation whose nested annotation references a stale enum constant therefore passed canLoad() and leaked EnumConstantNotPresentException (or the exception instance itself via attribute maps) on later access. canLoad() and validate() now recurse into annotation and annotation array attribute values, reusing the same probing for the nested annotation's own attributes. Recursion is guaranteed to terminate because the JLS forbids cyclic annotation member types. Signed-off-by: junhyeong9812 <pickjog@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
AttributeMethodsprobesClass,Class[], and enum attributes so that annotations whose values cannot be resolved at runtime are filtered during scanning instead of failing later. Annotation-typed attributes are not probed, and probing them by invocation alone would not be sufficient: the JDK returns the nested annotation proxy successfully and only throws when one of the nested annotation's own attributes is accessed.Problem
An annotation whose nested annotation member references a constant or type that no longer exists at runtime (a binary-incompatible classpath, for example partially upgraded dependencies) passes
canLoad()unprobed:MergedAnnotationsreports the annotation as present, while the same corruption in a direct attribute is filtered with a warning log.asMap()returns the corrupted nested annotation proxy as an attribute value, deferring the failure intoAnnotationAttributesconsumers.EnumConstantNotPresentExceptionfrom deep inside scanning or user code.Fix
canLoad()andvalidate()now capture the probed value and recurse into annotation and annotation array attribute values, applying the same probing to the nested annotation's own attributes. The probe flag computation marks annotation-typed attributes, reusing the adjacent nested-annotation detection idiom. Recursion terminates structurally because the JLS forbids cyclic annotation member types.Behavior notes:
Classand enum attributes ("true if all values are present"). The warning log names the actual failing annotation.validate()propagates theIllegalStateExceptionraised for the failing nested attribute without re-wrapping, so the message identifies the innermost failing annotation.getDeclaredAnnotations()parsing, and are out of scope for this probing.Cost, measured with a
canLoad()microbenchmark (before/after, best of 5 x 500k iterations): existing probes are unchanged (enum attribute 25ns before and after); annotation-typed attributes add roughly 50-70ns percanLoad()invocation, dominated by the added reflective read; the scanner's per-element caching bounds repetition on regularClass/Membersources.Tests cover the probe flag for annotation and annotation array attributes,
canLoad()andvalidate()for failing and healthy nested annotations, annotation arrays including the empty array, and two-level nesting; the failure assertions pin the exception message and cause to the innermost annotation.This change is complementary to #37153 (enum array attributes) and overlaps with it only on the flag computation line.