Skip to content
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}
Comment on lines +381 to +382
Copy link
Member

Choose a reason for hiding this comment

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

Is 'zero value' a well-defined term?

Copy link
Member

Choose a reason for hiding this comment

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

Also, 'Otherwise' seems to refer to If {@code value} is null which I don't think is the intent? Maybe you could add another list item (<li>) With:

If {@code dstType} is a primitive type and the runtime type of {@code value} is <em>not</em> a primitive wrapper type, a {@link ClassCastException} is thrown

Copy link
Member Author

Choose a reason for hiding this comment

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

I use the same wording as MethodHandles.explicitCastArguments here. I recommend updating that method as well if you have such questions.

Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if the term was more well define, especially for char and boolean, but that doesn't have to be done in this patch.

I do think it would be good to split up the cases into separate list items though. (I made it way too convoluted the first time around). Like I said in my second comment, I don't think the current text works.

* 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);
}
}