RFR: JDK-8283075: Bad IllegalArgumentException message when calling ClassDesc.arrayType(int) which results in a rank > 255 [v2]
ExE Boss
duke at openjdk.java.net
Tue Mar 15 13:55:40 UTC 2022
On Mon, 14 Mar 2022 21:27:29 GMT, Joe Darcy <darcy at openjdk.org> wrote:
>> Improving the exception messages for out-of-supported-range array types.
>>
>> I'll update copyrights before pushing.
>
> Joe Darcy has updated the pull request incrementally with one additional commit since the last revision:
>
> Respond to review feedback.
src/java.base/share/classes/java/lang/constant/ClassDesc.java line 185:
> 183: netRank = Math.addExact(currentDepth, rank);
> 184: if (netRank > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
> 185: throw new IllegalArgumentException("rank: " + netRank +
This doesn’t need to use <code>[Math.addExact](<var>currentDepth</var>, <var>rank</var>)</code>, as <code><var>currentDepth</var></code> will always be in [0, 255], and <code><var>rank</var></code> here is confined to [1, 2<sup>31</sup>-1], so it’s possible to check for <code><var>netRank</var> < 0</code> and use <code>[Integer.toUnsignedString](<var>netRank</var>)</code> (or <code>[Integer.toUnsignedLong](<var>netRank</var>)</code> and let <code>[StringConcatFactory]</code> deal with it):
Suggestion:
netRank = currentDepth + rank;
if (netRank > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS || netRank < 0) {
throw new IllegalArgumentException("rank: " + Integer.toUnsignedLong(netRank) +
[Integer.toUnsignedLong]: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Integer.html#toUnsignedLong(int)
[Integer.toUnsignedString]: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Integer.html#toUnsignedString(int)
[Math.addExact]: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html#addExact(int,int)
[StringConcatFactory]: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/invoke/StringConcatFactory.html
-------------
PR: https://git.openjdk.java.net/jdk/pull/7812
More information about the core-libs-dev
mailing list