From 2c4f1fbde9a38c744669c0fb7d7b6f87f8df86dc Mon Sep 17 00:00:00 2001 From: Shiv Shah Date: Mon, 11 May 2026 12:53:53 -0500 Subject: [PATCH 1/6] 8384109: Update NegativeArraySizeExceptionTest to check arrays of value classes --- .../NegativeArraySizeExceptionValueTest.java | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java new file mode 100644 index 00000000000..a7db95c5ddc --- /dev/null +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/** + * @test + * @bug 8384109 + * @summary Test that NegativeArraySizeException reports the wrong size for value class arrays. + * @library /test/lib + * @compile NegativeArraySizeExceptionValueTest.java + * @run main NegativeArraySizeExceptionValueTest + */ +import java.lang.reflect.Array; +import jdk.test.lib.Asserts; +public class NegativeArraySizeExceptionValueTest { + private static void fail() throws Exception { + throw new RuntimeException("Array allocation with negative size expected to fail!"); + } + public static void main(String[] args) throws Exception { + int minusOne = -1; + Object r = null; + // Value class array allocation with negative size. + try { + r = new Integer[minusOne]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Integer[Integer.MIN_VALUE]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Multidimensional value class array, inner array has wrong size. + try { + r = new Integer[3][minusOne]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Integer[3][Integer.MIN_VALUE]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Multidimensional value class array, outer array has wrong size. + try { + r = new Integer[minusOne][3]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Integer[Integer.MIN_VALUE][3]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Multidimensional value class array, inner array wrong size, outer size 0. + try { + r = new Integer[0][minusOne]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Integer[0][Integer.MIN_VALUE]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Reflection: Array.newInstance with value class type. + try { + Array.newInstance(Integer.class, minusOne); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + Array.newInstance(Integer.class, Integer.MIN_VALUE); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Reflection: multi-dimensional with value class type. + try { + Array.newInstance(Integer.class, new int[] {3, minusOne}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + Array.newInstance(Integer.class, new int[] {3, Integer.MIN_VALUE}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + try { + Array.newInstance(Integer.class, new int[] {0, minusOne}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + Array.newInstance(Integer.class, new int[] {0, Integer.MIN_VALUE}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + try { + Array.newInstance(Integer.class, new int[] {minusOne, 3}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + Array.newInstance(Integer.class, new int[] {Integer.MIN_VALUE, 3}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail."); + } +} From 41a6aa80d3908b99ee28f7d2229cbcdaf4f2b750 Mon Sep 17 00:00:00 2001 From: Shiv Shah Date: Mon, 11 May 2026 15:23:17 -0500 Subject: [PATCH 2/6] 8384109: Add custom value class NegativeArraySizeException test --- ...tiveArraySizeExceptionCustomValueTest.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java new file mode 100644 index 00000000000..058fa100899 --- /dev/null +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/** + * @test + * @bug 8384109 + * @summary Test that NegativeArraySizeException reports the wrong size for custom value class arrays. + * @library /test/lib + * @enablePreview + * @run main NegativeArraySizeExceptionCustomValueTest + */ +import java.lang.reflect.Array; +import jdk.test.lib.Asserts; +public class NegativeArraySizeExceptionCustomValueTest { + static value class Point { + int x; + int y; + Point(int x, int y) { + this.x = x; + this.y = y; + } + } + private static void fail() throws Exception { + throw new RuntimeException("Array allocation with negative size expected to fail!"); + } + public static void main(String[] args) throws Exception { + int minusOne = -1; + Object r = null; + // Custom value class array with negative size. + try { + r = new Point[minusOne]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Point[Integer.MIN_VALUE]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Multidimensional, inner array wrong size. + try { + r = new Point[3][minusOne]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Point[3][Integer.MIN_VALUE]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Multidimensional, outer array wrong size. + try { + r = new Point[minusOne][3]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Point[Integer.MIN_VALUE][3]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Multidimensional, inner wrong, outer zero. + try { + r = new Point[0][minusOne]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + r = new Point[0][Integer.MIN_VALUE]; + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Reflection with custom value class. + try { + Array.newInstance(Point.class, minusOne); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + Array.newInstance(Point.class, Integer.MIN_VALUE); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-2147483648", e.getMessage()); + } + // Reflection multi-dimensional with custom value class. + try { + Array.newInstance(Point.class, new int[] {3, minusOne}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + try { + Array.newInstance(Point.class, new int[] {minusOne, 3}); + fail(); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ("-1", e.getMessage()); + } + Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail."); + } +} From db01ca0645e99c6bd1711faec73d6f6cad466246 Mon Sep 17 00:00:00 2001 From: Shiv Shah Date: Mon, 11 May 2026 17:04:03 -0500 Subject: [PATCH 3/6] 8384109: Compact value class tests into single file --- ...tiveArraySizeExceptionCustomValueTest.java | 128 -------------- .../NegativeArraySizeExceptionValueTest.java | 162 ++++++------------ 2 files changed, 56 insertions(+), 234 deletions(-) delete mode 100644 test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java deleted file mode 100644 index 058fa100899..00000000000 --- a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionCustomValueTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -/** - * @test - * @bug 8384109 - * @summary Test that NegativeArraySizeException reports the wrong size for custom value class arrays. - * @library /test/lib - * @enablePreview - * @run main NegativeArraySizeExceptionCustomValueTest - */ -import java.lang.reflect.Array; -import jdk.test.lib.Asserts; -public class NegativeArraySizeExceptionCustomValueTest { - static value class Point { - int x; - int y; - Point(int x, int y) { - this.x = x; - this.y = y; - } - } - private static void fail() throws Exception { - throw new RuntimeException("Array allocation with negative size expected to fail!"); - } - public static void main(String[] args) throws Exception { - int minusOne = -1; - Object r = null; - // Custom value class array with negative size. - try { - r = new Point[minusOne]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Point[Integer.MIN_VALUE]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Multidimensional, inner array wrong size. - try { - r = new Point[3][minusOne]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Point[3][Integer.MIN_VALUE]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Multidimensional, outer array wrong size. - try { - r = new Point[minusOne][3]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Point[Integer.MIN_VALUE][3]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Multidimensional, inner wrong, outer zero. - try { - r = new Point[0][minusOne]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Point[0][Integer.MIN_VALUE]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Reflection with custom value class. - try { - Array.newInstance(Point.class, minusOne); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - Array.newInstance(Point.class, Integer.MIN_VALUE); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Reflection multi-dimensional with custom value class. - try { - Array.newInstance(Point.class, new int[] {3, minusOne}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - Array.newInstance(Point.class, new int[] {minusOne, 3}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail."); - } -} diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java index a7db95c5ddc..35a3d07a8b4 100644 --- a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java @@ -23,122 +23,72 @@ /** * @test * @bug 8384109 - * @summary Test that NegativeArraySizeException reports the wrong size for value class arrays. + * @summary Test NegativeArraySizeException message for value class arrays (migrated and custom). * @library /test/lib - * @compile NegativeArraySizeExceptionValueTest.java + * @enablePreview * @run main NegativeArraySizeExceptionValueTest */ import java.lang.reflect.Array; import jdk.test.lib.Asserts; public class NegativeArraySizeExceptionValueTest { - private static void fail() throws Exception { - throw new RuntimeException("Array allocation with negative size expected to fail!"); - } - public static void main(String[] args) throws Exception { - int minusOne = -1; - Object r = null; - // Value class array allocation with negative size. - try { - r = new Integer[minusOne]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Integer[Integer.MIN_VALUE]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Multidimensional value class array, inner array has wrong size. - try { - r = new Integer[3][minusOne]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Integer[3][Integer.MIN_VALUE]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Multidimensional value class array, outer array has wrong size. - try { - r = new Integer[minusOne][3]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Integer[Integer.MIN_VALUE][3]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Multidimensional value class array, inner array wrong size, outer size 0. - try { - r = new Integer[0][minusOne]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - r = new Integer[0][Integer.MIN_VALUE]; - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Reflection: Array.newInstance with value class type. - try { - Array.newInstance(Integer.class, minusOne); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - Array.newInstance(Integer.class, Integer.MIN_VALUE); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - // Reflection: multi-dimensional with value class type. - try { - Array.newInstance(Integer.class, new int[] {3, minusOne}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - Array.newInstance(Integer.class, new int[] {3, Integer.MIN_VALUE}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); - } - try { - Array.newInstance(Integer.class, new int[] {0, minusOne}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); - } - try { - Array.newInstance(Integer.class, new int[] {0, Integer.MIN_VALUE}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); + + static value class Point { + int x; + int y; + Point(int x, int y) { + this.x = x; + this.y = y; } + } + + @FunctionalInterface + interface AllocAction { + void run() throws Exception; + } + + private static void assertNASE(int size, AllocAction action) throws Exception { try { - Array.newInstance(Integer.class, new int[] {minusOne, 3}); - fail(); + action.run(); + throw new RuntimeException("Array allocation with negative size expected to fail!"); } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-1", e.getMessage()); + Asserts.assertEQ(Integer.toString(size), e.getMessage()); } - try { - Array.newInstance(Integer.class, new int[] {Integer.MIN_VALUE, 3}); - fail(); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ("-2147483648", e.getMessage()); + } + + public static void main(String[] args) throws Exception { + int[] sizes = { -1, Integer.MIN_VALUE }; + Class[] types = { Integer.class, Point.class }; + + for (int size : sizes) { + for (Class type : types) { + // Direct allocation via reflection + assertNASE(size, () -> Array.newInstance(type, size)); + + // Multi-dimensional: inner wrong + assertNASE(size, () -> Array.newInstance(type, new int[] {3, size})); + + // Multi-dimensional: outer wrong + assertNASE(size, () -> Array.newInstance(type, new int[] {size, 3})); + + // Multi-dimensional: inner wrong, outer zero + assertNASE(size, () -> Array.newInstance(type, new int[] {0, size})); + } + } + + // Direct new[] expressions — Integer + for (int size : sizes) { + assertNASE(size, () -> { Object r = new Integer[size]; }); + assertNASE(size, () -> { Object r = new Integer[3][size]; }); + assertNASE(size, () -> { Object r = new Integer[size][3]; }); + assertNASE(size, () -> { Object r = new Integer[0][size]; }); + } + + // Direct new[] expressions — custom value class Point + for (int size : sizes) { + assertNASE(size, () -> { Object r = new Point[size]; }); + assertNASE(size, () -> { Object r = new Point[3][size]; }); + assertNASE(size, () -> { Object r = new Point[size][3]; }); + assertNASE(size, () -> { Object r = new Point[0][size]; }); } - Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail."); } } From 3227daceb2d418b640d3352fad6e9dc6d4ff1411 Mon Sep 17 00:00:00 2001 From: Shiv Shah Date: Wed, 20 May 2026 14:00:21 -0400 Subject: [PATCH 4/6] 8384109: Add value class array cases to NegativeArraySizeExceptionTest --- .../NegativeArraySizeExceptionTest.java | 37 +++++++- .../NegativeArraySizeExceptionValueTest.java | 94 ------------------- 2 files changed, 36 insertions(+), 95 deletions(-) delete mode 100644 test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java index 51d95fb4417..703baa6fa8f 100644 --- a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -33,9 +33,31 @@ import java.lang.reflect.Array; import jdk.test.lib.Asserts; +import jdk.test.lib.valueclass.AsValueClass; public class NegativeArraySizeExceptionTest { + @AsValueClass + static class Point { + int x; + int y; + Point(int x, int y) { this.x = x; this.y = y; } + } + + @FunctionalInterface + interface AllocAction { + void run() throws Exception; + } + + private static void assertNASE(int size, AllocAction action) throws Exception { + try { + action.run(); + throw new RuntimeException("Array allocation with negative size expected to fail!"); + } catch (NegativeArraySizeException e) { + Asserts.assertEQ(Integer.toString(size), e.getMessage()); + } + } + private static void fail () throws Exception { throw new RuntimeException("Array allocation with negative size expected to fail!"); } @@ -301,6 +323,19 @@ public static void main(String[] args) throws Exception { Asserts.assertEQ("-2147483648", e.getMessage()); } + + // Tests for value class arrays (migrated Integer and custom Point). + int[] negativeSizes = { minusOne, Integer.MIN_VALUE }; + Class[] valueTypes = { Integer.class, Point.class }; + for (int size : negativeSizes) { + for (Class type : valueTypes) { + assertNASE(size, () -> Array.newInstance(type, size)); + assertNASE(size, () -> Array.newInstance(type, new int[] {3, size})); + assertNASE(size, () -> Array.newInstance(type, new int[] {size, 3})); + assertNASE(size, () -> Array.newInstance(type, new int[] {0, size})); + } + } + Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail."); } } diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java deleted file mode 100644 index 35a3d07a8b4..00000000000 --- a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionValueTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -/** - * @test - * @bug 8384109 - * @summary Test NegativeArraySizeException message for value class arrays (migrated and custom). - * @library /test/lib - * @enablePreview - * @run main NegativeArraySizeExceptionValueTest - */ -import java.lang.reflect.Array; -import jdk.test.lib.Asserts; -public class NegativeArraySizeExceptionValueTest { - - static value class Point { - int x; - int y; - Point(int x, int y) { - this.x = x; - this.y = y; - } - } - - @FunctionalInterface - interface AllocAction { - void run() throws Exception; - } - - private static void assertNASE(int size, AllocAction action) throws Exception { - try { - action.run(); - throw new RuntimeException("Array allocation with negative size expected to fail!"); - } catch (NegativeArraySizeException e) { - Asserts.assertEQ(Integer.toString(size), e.getMessage()); - } - } - - public static void main(String[] args) throws Exception { - int[] sizes = { -1, Integer.MIN_VALUE }; - Class[] types = { Integer.class, Point.class }; - - for (int size : sizes) { - for (Class type : types) { - // Direct allocation via reflection - assertNASE(size, () -> Array.newInstance(type, size)); - - // Multi-dimensional: inner wrong - assertNASE(size, () -> Array.newInstance(type, new int[] {3, size})); - - // Multi-dimensional: outer wrong - assertNASE(size, () -> Array.newInstance(type, new int[] {size, 3})); - - // Multi-dimensional: inner wrong, outer zero - assertNASE(size, () -> Array.newInstance(type, new int[] {0, size})); - } - } - - // Direct new[] expressions — Integer - for (int size : sizes) { - assertNASE(size, () -> { Object r = new Integer[size]; }); - assertNASE(size, () -> { Object r = new Integer[3][size]; }); - assertNASE(size, () -> { Object r = new Integer[size][3]; }); - assertNASE(size, () -> { Object r = new Integer[0][size]; }); - } - - // Direct new[] expressions — custom value class Point - for (int size : sizes) { - assertNASE(size, () -> { Object r = new Point[size]; }); - assertNASE(size, () -> { Object r = new Point[3][size]; }); - assertNASE(size, () -> { Object r = new Point[size][3]; }); - assertNASE(size, () -> { Object r = new Point[0][size]; }); - } - } -} From aed735933e129c5b1423eccc32085c03c595060c Mon Sep 17 00:00:00 2001 From: Shiv Shah Date: Wed, 20 May 2026 14:48:38 -0400 Subject: [PATCH 5/6] 8384109: Rename to VClass with Integer fields per review --- .../NegativeArraySizeExceptionTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java index 703baa6fa8f..5ccf137134f 100644 --- a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java @@ -38,10 +38,10 @@ public class NegativeArraySizeExceptionTest { @AsValueClass - static class Point { - int x; - int y; - Point(int x, int y) { this.x = x; this.y = y; } + static class VClass { + Integer x; + Integer y; + VClass(Integer x, Integer y) { this.x = x; this.y = y; } } @FunctionalInterface @@ -326,7 +326,7 @@ public static void main(String[] args) throws Exception { // Tests for value class arrays (migrated Integer and custom Point). int[] negativeSizes = { minusOne, Integer.MIN_VALUE }; - Class[] valueTypes = { Integer.class, Point.class }; + Class[] valueTypes = { Integer.class, VClass.class }; for (int size : negativeSizes) { for (Class type : valueTypes) { assertNASE(size, () -> Array.newInstance(type, size)); From a319a3b3898a0778c42e9820ea4df97d4c8508c1 Mon Sep 17 00:00:00 2001 From: Shiv Shah Date: Wed, 20 May 2026 15:44:39 -0400 Subject: [PATCH 6/6] 8384109: Fix comment Point -> VClass --- .../NegativeArraySizeExceptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java index 5ccf137134f..518f8c70432 100644 --- a/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java +++ b/test/hotspot/jtreg/runtime/exceptionMsgs/NegativeArraySizeException/NegativeArraySizeExceptionTest.java @@ -324,7 +324,7 @@ public static void main(String[] args) throws Exception { } - // Tests for value class arrays (migrated Integer and custom Point). + // Tests for value class arrays (migrated Integer and custom VClass). int[] negativeSizes = { minusOne, Integer.MIN_VALUE }; Class[] valueTypes = { Integer.class, VClass.class }; for (int size : negativeSizes) {