8389259: Class::desiredAssertionStatus asserts with primitive or array class receiver

Reviewed-by: liach, dholmes, alanb
This commit is contained in:
Coleen Phillimore 2026-08-03 15:59:50 +00:00
parent 3385e5163a
commit 218fb7f494
2 changed files with 52 additions and 0 deletions

View File

@ -3427,6 +3427,8 @@ public final class Class<T> implements java.io.Serializable,
* @since 1.4
*/
public boolean desiredAssertionStatus() {
if (isPrimitive() || isArray()) return false;
ClassLoader loader = classLoader;
// If the loader is null this is a system class, so ask the VM
if (loader == null)

View File

@ -0,0 +1,50 @@
/*
* 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 8389259
* @summary Sanity check that Class.desiredAssertionStatus() works.
* @library /test/lib
* @run testng/othervm -esa -ea -Dsys.option=on -Duser.option=on Sanity
* @run testng/othervm -dsa -ea -Dsys.option=off -Duser.option=on Sanity
* @run testng/othervm -esa -da -Dsys.option=on -Duser.option=off Sanity
* @run testng/othervm -dsa -da -Dsys.option=off -Duser.option=off Sanity
*/
import org.testng.annotations.Test;
import org.testng.Assert;
public class Sanity {
@Test
public void test() throws Exception {
boolean systemDefaultOn = System.getProperty("sys.option").equals("on");
boolean userDefaultOn = System.getProperty("user.option").equals("on");
Assert.assertTrue(int.class.desiredAssertionStatus() == false);
Assert.assertTrue(Object.class.desiredAssertionStatus() == systemDefaultOn);
Assert.assertTrue(Object[].class.desiredAssertionStatus() == false);
Assert.assertTrue(Sanity.class.desiredAssertionStatus() == userDefaultOn);
Assert.assertTrue(Sanity[].class.desiredAssertionStatus() == false);
}
}