RFR: 8371960: Missing null check in AnnotatedType annotation accessor methods
Chen Liang
liach at openjdk.org
Sun Nov 16 19:27:11 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.
There are impact to the 3 methods:
- `isAnnotationPresent`: previously returned `false`, now throws
- `getAnnotation`: previously returned `null`, now throws
- `getDeclaredAnnotation`: previously returned `null`, now throws
For these two methods:
- `getAnnnotationsByType`
- `getDeclaredAnnotationsByType`
They currently already throw NullPointerException from within AnnotationSupport.getIndirectlyPresent. The only action that happens before is a Map.get call, so I don't think the lifted null check would hide any existing linkage error or other exceptions.
I think the main impact of this change is that `AnnotatedType` is the only way to retrieve type annotations, so the accessors might see more frequent usage compared to those on other `AnnotatedElement`s.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/28341#issuecomment-3539163577
More information about the core-libs-dev
mailing list