test/jdk/java/lang/invoke/TryFinallyTest.java
changeset 59075 355f4f42dda5
parent 49790 403e2f61f384
equal deleted inserted replaced
59072:b987ea528c21 59075:355f4f42dda5
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 /* @test
    26 /* @test
    27  * @bug 8139885 8150824 8150825 8194238
    27  * @bug 8139885 8150824 8150825 8194238 8233920
    28  * @run testng/othervm -ea -esa test.java.lang.invoke.TryFinallyTest
    28  * @run testng/othervm -ea -esa -Xverify:all test.java.lang.invoke.TryFinallyTest
    29  */
    29  */
    30 
    30 
    31 package test.java.lang.invoke;
    31 package test.java.lang.invoke;
    32 
    32 
    33 import java.lang.invoke.MethodHandle;
    33 import java.lang.invoke.MethodHandle;
    51     @Test
    51     @Test
    52     public static void testTryFinally() throws Throwable {
    52     public static void testTryFinally() throws Throwable {
    53         MethodHandle hello = MethodHandles.tryFinally(TryFinally.MH_greet, TryFinally.MH_exclaim);
    53         MethodHandle hello = MethodHandles.tryFinally(TryFinally.MH_greet, TryFinally.MH_exclaim);
    54         assertEquals(TryFinally.MT_hello, hello.type());
    54         assertEquals(TryFinally.MT_hello, hello.type());
    55         assertEquals("Hello, world!", hello.invoke("world"));
    55         assertEquals("Hello, world!", hello.invoke("world"));
       
    56     }
       
    57 
       
    58     @DataProvider
       
    59     static Object[][] tryFinallyArgs() {
       
    60         return new Object[][] {
       
    61                 { boolean.class, true },
       
    62                 { byte.class, (byte) 2 },
       
    63                 { short.class, (short) 2 },
       
    64                 { char.class, (char) 2 },
       
    65                 { int.class, 2 },
       
    66                 { long.class, 2L },
       
    67                 { float.class, 2f },
       
    68                 { double.class, 2D },
       
    69                 { Object.class, new Object() }
       
    70         };
       
    71     }
       
    72 
       
    73     @Test(dataProvider = "tryFinallyArgs")
       
    74     public static void testTryFinally(Class<?> argType, Object arg) throws Throwable {
       
    75         MethodHandle identity = MethodHandles.identity(argType);
       
    76         MethodHandle tryFinally = MethodHandles.tryFinally(
       
    77                 identity,
       
    78                 MethodHandles.dropArguments(identity, 0, Throwable.class));
       
    79         assertEquals(methodType(argType, argType), tryFinally.type());
       
    80         assertEquals(arg, tryFinally.invoke(arg));
       
    81     }
       
    82 
       
    83     @Test(dataProvider = "tryFinallyArgs", expectedExceptions = TryFinally.T1.class)
       
    84     public static void testTryFinallyException(Class<?> argType, Object arg) throws Throwable {
       
    85         MethodHandle identity = TryFinally.MH_throwingTargetIdentity.asType(methodType(argType, argType));
       
    86         MethodHandle tryFinally = MethodHandles.tryFinally(
       
    87                 identity,
       
    88                 MethodHandles.dropArguments(identity, 0, TryFinally.T1.class));
       
    89         assertEquals(methodType(argType, argType), tryFinally.type());
       
    90         tryFinally.invoke(arg); // should throw
    56     }
    91     }
    57 
    92 
    58     @Test
    93     @Test
    59     public static void testTryFinallyVoid() throws Throwable {
    94     public static void testTryFinallyVoid() throws Throwable {
    60         MethodHandle tfVoid = MethodHandles.tryFinally(TryFinally.MH_print, TryFinally.MH_printMore);
    95         MethodHandle tfVoid = MethodHandles.tryFinally(TryFinally.MH_print, TryFinally.MH_printMore);
   173 
   208 
   174         static void throwingTarget() throws Throwable {
   209         static void throwingTarget() throws Throwable {
   175             throw new T1();
   210             throw new T1();
   176         }
   211         }
   177 
   212 
       
   213         static Object throwingTargetIdentity(Object o) throws Throwable {
       
   214             throw new T1();
       
   215         }
       
   216 
   178         static void catchingCleanup(T2 t) throws Throwable {
   217         static void catchingCleanup(T2 t) throws Throwable {
   179         }
   218         }
   180 
   219 
   181         static final Class<TryFinally> TRY_FINALLY = TryFinally.class;
   220         static final Class<TryFinally> TRY_FINALLY = TryFinally.class;
   182 
   221 
   187         static final MethodType MT_greetMore = methodType(String.class, String.class, String.class);
   226         static final MethodType MT_greetMore = methodType(String.class, String.class, String.class);
   188         static final MethodType MT_exclaimMore = methodType(String.class, Throwable.class, String.class, String.class);
   227         static final MethodType MT_exclaimMore = methodType(String.class, Throwable.class, String.class, String.class);
   189         static final MethodType MT_voidTarget = methodType(void.class);
   228         static final MethodType MT_voidTarget = methodType(void.class);
   190         static final MethodType MT_voidCleanup = methodType(void.class, Throwable.class);
   229         static final MethodType MT_voidCleanup = methodType(void.class, Throwable.class);
   191         static final MethodType MT_throwingTarget = methodType(void.class);
   230         static final MethodType MT_throwingTarget = methodType(void.class);
       
   231         static final MethodType MT_throwingTargetIdentity = methodType(Object.class, Object.class);
   192         static final MethodType MT_catchingCleanup = methodType(void.class, T2.class);
   232         static final MethodType MT_catchingCleanup = methodType(void.class, T2.class);
   193 
   233 
   194         static final MethodHandle MH_greet;
   234         static final MethodHandle MH_greet;
   195         static final MethodHandle MH_exclaim;
   235         static final MethodHandle MH_exclaim;
   196         static final MethodHandle MH_print;
   236         static final MethodHandle MH_print;
   198         static final MethodHandle MH_greetMore;
   238         static final MethodHandle MH_greetMore;
   199         static final MethodHandle MH_exclaimMore;
   239         static final MethodHandle MH_exclaimMore;
   200         static final MethodHandle MH_voidTarget;
   240         static final MethodHandle MH_voidTarget;
   201         static final MethodHandle MH_voidCleanup;
   241         static final MethodHandle MH_voidCleanup;
   202         static final MethodHandle MH_throwingTarget;
   242         static final MethodHandle MH_throwingTarget;
       
   243         static final MethodHandle MH_throwingTargetIdentity;
   203         static final MethodHandle MH_catchingCleanup;
   244         static final MethodHandle MH_catchingCleanup;
   204 
   245 
   205         static final MethodHandle MH_dummyTarget;
   246         static final MethodHandle MH_dummyTarget;
   206 
   247 
   207         static final MethodType MT_hello = methodType(String.class, String.class);
   248         static final MethodType MT_hello = methodType(String.class, String.class);
   217                 MH_greetMore = LOOKUP.findStatic(TRY_FINALLY, "greetMore", MT_greetMore);
   258                 MH_greetMore = LOOKUP.findStatic(TRY_FINALLY, "greetMore", MT_greetMore);
   218                 MH_exclaimMore = LOOKUP.findStatic(TRY_FINALLY, "exclaimMore", MT_exclaimMore);
   259                 MH_exclaimMore = LOOKUP.findStatic(TRY_FINALLY, "exclaimMore", MT_exclaimMore);
   219                 MH_voidTarget = LOOKUP.findStatic(TRY_FINALLY, "voidTarget", MT_voidTarget);
   260                 MH_voidTarget = LOOKUP.findStatic(TRY_FINALLY, "voidTarget", MT_voidTarget);
   220                 MH_voidCleanup = LOOKUP.findStatic(TRY_FINALLY, "voidCleanup", MT_voidCleanup);
   261                 MH_voidCleanup = LOOKUP.findStatic(TRY_FINALLY, "voidCleanup", MT_voidCleanup);
   221                 MH_throwingTarget = LOOKUP.findStatic(TRY_FINALLY, "throwingTarget", MT_throwingTarget);
   262                 MH_throwingTarget = LOOKUP.findStatic(TRY_FINALLY, "throwingTarget", MT_throwingTarget);
       
   263                 MH_throwingTargetIdentity = LOOKUP.findStatic(TRY_FINALLY, "throwingTargetIdentity", MT_throwingTargetIdentity);
   222                 MH_catchingCleanup = LOOKUP.findStatic(TRY_FINALLY, "catchingCleanup", MT_catchingCleanup);
   264                 MH_catchingCleanup = LOOKUP.findStatic(TRY_FINALLY, "catchingCleanup", MT_catchingCleanup);
   223                 MH_dummyTarget = MethodHandles.dropArguments(MH_voidTarget, 0, int.class, long.class, Object.class,
   265                 MH_dummyTarget = MethodHandles.dropArguments(MH_voidTarget, 0, int.class, long.class, Object.class,
   224                         int.class, long.class, Object.class);
   266                         int.class, long.class, Object.class);
   225             } catch (Exception e) {
   267             } catch (Exception e) {
   226                 throw new ExceptionInInitializerError(e);
   268                 throw new ExceptionInInitializerError(e);