RFR: 8328874: Class::forName0 should validate the class name length early [v5]

ExE Boss duke at openjdk.org
Sat Aug 23 14:11:52 UTC 2025


On Thu, 21 Aug 2025 02:00:47 GMT, Guanqiang Han <ghan at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/Class.java line 4160:
>> 
>>> 4158:     // Checks whether the class name exceeds the maximum allowed length.
>>> 4159:     private static boolean classNameLengthIsValid(String name) {
>>> 4160:         Objects.requireNonNull(name);
>> 
>> This is not needed as the `name.length()` call already performs an implicit `null` check.
>
> hi @ExE-Boss ,thanks for your comment regarding the Objects.requireNonNull(name) call. I understand that name.length() triggers an implicit null check, which could make the explicit check seem redundant.
> 
> However, I wonder if there is a subtle difference worth considering here: the implicit null check relies on the system exception (signal) mechanism and subsequent JVM handling, which does have some overhead. In addition, as i know, C2 decides whether to use implicit null checks after performing statistical analysis. For this kind of core library, should we assume that null is a low-probability event?

Well, **C2** can elide even explicit `null` checks, but `Objects​.requireNonNull(name)` doesn’t ([currently][JDK‑8233268][^1]) get a helpful message like what an implicit null check gets.

[JDK‑8233268]: https://bugs.openjdk.org/browse/JDK-8233268
[^1]: https://git.openjdk.org/jdk/pull/26600

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26802#discussion_r2296091939


More information about the core-libs-dev mailing list