Skip to content

8364751: ConstantBootstraps.explicitCast contradictory specification for null-to-primitive #26714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, 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
Expand Down Expand Up @@ -373,13 +373,14 @@ public static VarHandle arrayVarHandle(MethodHandles.Lookup lookup, String name,
* is applied to {@code value} as if by calling {@code dstType.cast(value)}.
* <li>If {@code dstType} is a primitive type, then, if the runtime type
* of {@code value} is a primitive wrapper type (such as {@link Integer}),
* a Java unboxing conversion is applied {@jls 5.1.8} followed by a
* Java casting conversion {@jls 5.5} converting either directly to
* a Java unboxing conversion is applied (JLS {@jls 5.1.8}) followed by a
* Java casting conversion (JLS {@jls 5.5}) converting either directly to
* {@code dstType}, or, if {@code dstType} is {@code boolean},
* to {@code int}, which is then converted to either {@code true}
* or {@code false} depending on whether the least-significant-bit
* is 1 or 0 respectively. If the runtime type of {@code value} is
* not a primitive wrapper type a {@link ClassCastException} is thrown.
* is 1 or 0 respectively. If {@code value} is null, the zero value for
* the {@code dstType} is returned. Otherwise, a {@link ClassCastException}
* is thrown.
* </ol>
* <p>
* The result is the same as when using the following code:
Expand All @@ -397,8 +398,8 @@ public static VarHandle arrayVarHandle(MethodHandles.Lookup lookup, String name,
* @return the converted value
* @throws ClassCastException when {@code dstType} is {@code void},
* when a cast per (1) fails, or when {@code dstType} is a primitive type
* and the runtime type of {@code value} is not a primitive wrapper type
* (such as {@link Integer})
* and {@code value} is not null and its runtime type is not a primitive
* wrapper type (such as {@link Integer})
*
* @since 15
*/
Expand Down
79 changes: 0 additions & 79 deletions test/jdk/java/lang/constant/ConvertTest.java

This file was deleted.

46 changes: 44 additions & 2 deletions test/jdk/java/lang/invoke/condy/ConstantBootstrapsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, 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
Expand All @@ -23,14 +23,15 @@

/*
* @test
* @bug 8186046 8195694
* @bug 8186046 8195694 8241100 8364751
* @summary Test dynamic constant bootstraps
* @library /java/lang/invoke/common
* @build test.java.lang.invoke.lib.InstructionHelper
* @run testng ConstantBootstrapsTest
* @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 ConstantBootstrapsTest
*/

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import test.java.lang.invoke.lib.InstructionHelper;

Expand Down Expand Up @@ -241,4 +242,45 @@ public void testVarHandleArray() throws Throwable {
assertEquals(vhandle.varType(), String.class);
assertEquals(vhandle.coordinateTypes(), List.of(String[].class, int.class));
}

@DataProvider
public static Object[][] cceCasts() {
return new Object[][]{
{ void.class, null },
{ Integer.class, "a" },
{ int.class, BigInteger.ZERO },
};
}

@Test(dataProvider = "cceCasts", expectedExceptions = ClassCastException.class)
public void testBadCasts(Class<?> dstType, Object value) {
ConstantBootstraps.explicitCast(null, null, dstType, value);
}

@DataProvider
public static Object[][] validCasts() {
Object o = new Object();
return new Object[][]{
{ Object.class, null, null },
{ Object.class, o, o },
{ String.class, "abc", "abc" },
{ short.class, 10, (short) 10 },
{ int.class, (short) 10, 10 },
{ boolean.class, 1, true },
{ boolean.class, 2, false },
{ int.class, true, 1 },
{ int.class, false, 0 },
{ int.class, 10, 10 },
{ Integer.class, 10, 10 },
{ Object.class, 10, 10 },
{ Number.class, 10, 10 },
{ char.class, null, (char) 0 }
};
}

@Test(dataProvider = "validCasts")
public void testSuccessfulCasts(Class<?> dstType, Object value, Object expected) {
Object actual = ConstantBootstraps.explicitCast(null, null, dstType, value);
assertEquals(actual, expected);
}
}