test/jdk/java/lang/invoke/condy/ConstantBootstrapsTest.java
changeset 48834 19ef3f64bc10
parent 48827 8772acd913e5
child 48878 2f265d2ac735
equal deleted inserted replaced
48833:614c7e117327 48834:19ef3f64bc10
     1 /*
     1 /*
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8186046
    26  * @bug 8186046 8195694
    27  * @summary Test dynamic constant bootstraps
    27  * @summary Test dynamic constant bootstraps
    28  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
    28  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
    29  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
    29  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
    30  * @run testng ConstantBootstrapsTest
    30  * @run testng ConstantBootstrapsTest
    31  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 ConstantBootstrapsTest
    31  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 ConstantBootstrapsTest
    41 import java.lang.invoke.MethodHandles;
    41 import java.lang.invoke.MethodHandles;
    42 import java.lang.invoke.MethodType;
    42 import java.lang.invoke.MethodType;
    43 import java.lang.invoke.VarHandle;
    43 import java.lang.invoke.VarHandle;
    44 import java.lang.invoke.WrongMethodTypeException;
    44 import java.lang.invoke.WrongMethodTypeException;
    45 import java.math.BigInteger;
    45 import java.math.BigInteger;
       
    46 import java.util.Collection;
    46 import java.util.List;
    47 import java.util.List;
    47 import java.util.Map;
    48 import java.util.Map;
    48 
    49 
    49 import static org.testng.Assert.assertEquals;
    50 import static org.testng.Assert.assertEquals;
    50 import static org.testng.Assert.assertNull;
    51 import static org.testng.Assert.assertNull;
   187                     S.add("42");
   188                     S.add("42");
   188                 });
   189                 });
   189         assertEquals(handle.invoke(), 42);
   190         assertEquals(handle.invoke(), 42);
   190     }
   191     }
   191 
   192 
       
   193     public void testInvokeAsTypeVariableArity() throws Throwable {
       
   194         // The constant type is Collection but the invoke return type is List
       
   195         var handle = InstructionHelper.ldcDynamicConstant(
       
   196                 L, "_", Collection.class,
       
   197                 ConstantBootstraps.class, "invoke", lookupMT(Object.class, MethodHandle.class, Object[].class),
       
   198                 S -> {
       
   199                     S.add("", (P, Z) -> {
       
   200                         return P.putHandle(MethodHandleInfo.REF_invokeStatic, "java/util/List", "of",
       
   201                                            MethodType.methodType(List.class, Object[].class).toMethodDescriptorString(),
       
   202                                            true);
       
   203                     });
       
   204                     S.add(1).add(2).add(3).add(4);
       
   205                 });
       
   206         assertEquals(handle.invoke(), List.of(1, 2, 3, 4));
       
   207     }
       
   208 
   192     @Test(expectedExceptions = ClassCastException.class)
   209     @Test(expectedExceptions = ClassCastException.class)
   193     public void testInvokeAsTypeClassCast() throws Throwable {
   210     public void testInvokeAsTypeClassCast() throws Throwable {
   194         ConstantBootstraps.invoke(MethodHandles.lookup(), "_", String.class,
   211         ConstantBootstraps.invoke(MethodHandles.lookup(), "_", String.class,
   195                                   MethodHandles.lookup().findStatic(Integer.class, "valueOf", MethodType.methodType(Integer.class, String.class)),
   212                                   MethodHandles.lookup().findStatic(Integer.class, "valueOf", MethodType.methodType(Integer.class, String.class)),
   196                                   "42");
   213                                   "42");