15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package java.lang.invoke;
25
26 import sun.invoke.util.Wrapper;
27
28 /* @test
29 * @summary unit tests for MethodHandles.explicitCastArguments()
30 *
31 * @run main/bootclasspath java.lang.invoke.ExplicitCastArgumentsTest
32 */
33 public class ExplicitCastArgumentsTest {
34 private static final boolean VERBOSE = Boolean.getBoolean("verbose");
35
36 public static void main(String[] args) throws Throwable {
37 for (Wrapper from : Wrapper.values()) {
38 for (Wrapper to : Wrapper.values()) {
39 if (from == Wrapper.VOID || to == Wrapper.VOID) continue;
40 testRef2Prim (from, to);
41 }
42 }
43 System.out.println("TEST PASSED");
44 }
45
46 public static void testRef2Prim(Wrapper from, Wrapper to) throws Throwable {
47 // MHs.eCA javadoc:
48 // If T0 is a reference and T1 a primitive, and if the reference is null at runtime, a zero value is introduced.
49 test(from.wrapperType(), to.primitiveType(), null, false);
50 }
51
52 public static void test(Class<?> from, Class<?> to, Object param, boolean failureExpected) throws Throwable {
53 if (VERBOSE) System.out.printf("%-10s => %-10s: %5s: ", from.getSimpleName(), to.getSimpleName(), param);
54
55 MethodHandle original = MethodHandles.identity(from);
56 MethodType newType = original.type().changeReturnType(to);
57
58 try {
59 MethodHandle target = MethodHandles.explicitCastArguments(original, newType);
60 Object result = target.invokeWithArguments(param);
61
62 if (VERBOSE) {
63 String resultStr;
|
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package java.lang.invoke;
25
26 import sun.invoke.util.Wrapper;
27
28 /* @test
29 * @summary unit tests for MethodHandles.explicitCastArguments()
30 *
31 * @run main/bootclasspath java.lang.invoke.ExplicitCastArgumentsTest
32 */
33 public class ExplicitCastArgumentsTest {
34 private static final boolean VERBOSE = Boolean.getBoolean("verbose");
35 private static final Class<?> THIS_CLASS = ExplicitCastArgumentsTest.class;
36
37 public static void main(String[] args) throws Throwable {
38 testVarargsCollector();
39 testRef2Prim();
40 System.out.println("TEST PASSED");
41 }
42
43 public static String[] f(String... args) { return args; }
44
45 public static void testVarargsCollector() throws Throwable {
46 MethodType mt = MethodType.methodType(String[].class, String[].class);
47 MethodHandle mh = MethodHandles.publicLookup().findStatic(THIS_CLASS, "f", mt);
48 mh = MethodHandles.explicitCastArguments(mh, MethodType.methodType(Object.class, Object.class));
49 mh.invokeWithArguments((Object)(new String[] {"str1", "str2"}));
50 }
51
52 public static void testRef2Prim() throws Throwable {
53 for (Wrapper from : Wrapper.values()) {
54 for (Wrapper to : Wrapper.values()) {
55 if (from == Wrapper.VOID || to == Wrapper.VOID) continue;
56 testRef2Prim(from, to);
57 }
58 }
59 }
60
61 public static void testRef2Prim(Wrapper from, Wrapper to) throws Throwable {
62 // MHs.eCA javadoc:
63 // If T0 is a reference and T1 a primitive, and if the reference is null at runtime, a zero value is introduced.
64 test(from.wrapperType(), to.primitiveType(), null, false);
65 }
66
67 public static void test(Class<?> from, Class<?> to, Object param, boolean failureExpected) throws Throwable {
68 if (VERBOSE) System.out.printf("%-10s => %-10s: %5s: ", from.getSimpleName(), to.getSimpleName(), param);
69
70 MethodHandle original = MethodHandles.identity(from);
71 MethodType newType = original.type().changeReturnType(to);
72
73 try {
74 MethodHandle target = MethodHandles.explicitCastArguments(original, newType);
75 Object result = target.invokeWithArguments(param);
76
77 if (VERBOSE) {
78 String resultStr;
|