RFR: 8371960: Missing null check in AnnotatedType annotation accessor methods
Alan Bateman
alanb at openjdk.org
Mon Nov 17 10:48:13 UTC 2025
On Sun, 16 Nov 2025 16:02:58 GMT, Chen Liang <liach at openjdk.org> wrote:
> `AnnotatedElement` methods specify they throw NPE if they take any null argument: `isAnnotationPresent`, `get(Declared)Annotation`, `get(Declared)AnnnotationsByType`. However, `AnnotatedType` has implementation quality issues that it does not throw NPE for `isAnnotationPresent` and `get(Declared)Annotation`.
>
> This patch fixes this oversight by adding explicit null checks. For `get(Declared)AnnotationsByType`, even though it did throw exceptions, it was in the middle of parsing, so throwing it in the beginning is better.
Marked as reviewed by alanb (Reviewer).
test/jdk/java/lang/reflect/AnnotatedElement/AnnotatedElementNullCheckTest.java line 87:
> 85: var clazz = impl.getClass();
> 86: if (!set.add(clazz)) {
> 87: fail("Duplicate implementation class %s in %s".formatted(clazz, impl));
Good sanity test on the method source.
-------------
PR Review: https://git.openjdk.org/jdk/pull/28341#pullrequestreview-3472103024
PR Review Comment: https://git.openjdk.org/jdk/pull/28341#discussion_r2533583081
More information about the core-libs-dev
mailing list