Skip to content
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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 VClass {
Integer x;
Integer y;
VClass(Integer x, Integer 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!");
}
Expand Down Expand Up @@ -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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point -> VClass

int[] negativeSizes = { minusOne, Integer.MIN_VALUE };
Class<?>[] valueTypes = { Integer.class, VClass.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.");
}
}