test/jdk/java/lang/invoke/TryFinallyTest.java
changeset 49790 403e2f61f384
parent 47216 71c04702a3d5
child 59075 355f4f42dda5
equal deleted inserted replaced
49789:27b359322b1e 49790:403e2f61f384
    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
    27  * @bug 8139885 8150824 8150825 8194238
    28  * @bug 8150824
       
    29  * @bug 8150825
       
    30  * @run testng/othervm -ea -esa test.java.lang.invoke.TryFinallyTest
    28  * @run testng/othervm -ea -esa test.java.lang.invoke.TryFinallyTest
    31  */
    29  */
    32 
    30 
    33 package test.java.lang.invoke;
    31 package test.java.lang.invoke;
    34 
    32 
   124             caught = true;
   122             caught = true;
   125         }
   123         }
   126         assertTrue(caught);
   124         assertTrue(caught);
   127     }
   125     }
   128 
   126 
       
   127     @Test
       
   128     public static void testTryFinallyThrowableCheck() {
       
   129         MethodHandle mh = MethodHandles.tryFinally(TryFinally.MH_throwingTarget,
       
   130                                                    TryFinally.MH_catchingCleanup);
       
   131         try {
       
   132             mh.invoke();
       
   133             fail("ClassCastException expected");
       
   134         } catch (Throwable t) {
       
   135             assertTrue("Throwable not assignable to ClassCastException: " + t,
       
   136                        ClassCastException.class.isAssignableFrom(t.getClass()));
       
   137         }
       
   138     }
       
   139 
   129     static class TryFinally {
   140     static class TryFinally {
   130 
   141 
   131         static String greet(String whom) {
   142         static String greet(String whom) {
   132             return "Hello, " + whom;
   143             return "Hello, " + whom;
   133         }
   144         }
   153         }
   164         }
   154 
   165 
   155         static void voidTarget() {}
   166         static void voidTarget() {}
   156 
   167 
   157         static void voidCleanup(Throwable t) {}
   168         static void voidCleanup(Throwable t) {}
       
   169 
       
   170         static class T1 extends Throwable {}
       
   171 
       
   172         static class T2 extends Throwable {}
       
   173 
       
   174         static void throwingTarget() throws Throwable {
       
   175             throw new T1();
       
   176         }
       
   177 
       
   178         static void catchingCleanup(T2 t) throws Throwable {
       
   179         }
   158 
   180 
   159         static final Class<TryFinally> TRY_FINALLY = TryFinally.class;
   181         static final Class<TryFinally> TRY_FINALLY = TryFinally.class;
   160 
   182 
   161         static final MethodType MT_greet = methodType(String.class, String.class);
   183         static final MethodType MT_greet = methodType(String.class, String.class);
   162         static final MethodType MT_exclaim = methodType(String.class, Throwable.class, String.class, String.class);
   184         static final MethodType MT_exclaim = methodType(String.class, Throwable.class, String.class, String.class);
   164         static final MethodType MT_printMore = methodType(void.class, Throwable.class, String.class);
   186         static final MethodType MT_printMore = methodType(void.class, Throwable.class, String.class);
   165         static final MethodType MT_greetMore = methodType(String.class, String.class, String.class);
   187         static final MethodType MT_greetMore = methodType(String.class, String.class, String.class);
   166         static final MethodType MT_exclaimMore = methodType(String.class, Throwable.class, String.class, String.class);
   188         static final MethodType MT_exclaimMore = methodType(String.class, Throwable.class, String.class, String.class);
   167         static final MethodType MT_voidTarget = methodType(void.class);
   189         static final MethodType MT_voidTarget = methodType(void.class);
   168         static final MethodType MT_voidCleanup = methodType(void.class, Throwable.class);
   190         static final MethodType MT_voidCleanup = methodType(void.class, Throwable.class);
       
   191         static final MethodType MT_throwingTarget = methodType(void.class);
       
   192         static final MethodType MT_catchingCleanup = methodType(void.class, T2.class);
   169 
   193 
   170         static final MethodHandle MH_greet;
   194         static final MethodHandle MH_greet;
   171         static final MethodHandle MH_exclaim;
   195         static final MethodHandle MH_exclaim;
   172         static final MethodHandle MH_print;
   196         static final MethodHandle MH_print;
   173         static final MethodHandle MH_printMore;
   197         static final MethodHandle MH_printMore;
   174         static final MethodHandle MH_greetMore;
   198         static final MethodHandle MH_greetMore;
   175         static final MethodHandle MH_exclaimMore;
   199         static final MethodHandle MH_exclaimMore;
   176         static final MethodHandle MH_voidTarget;
   200         static final MethodHandle MH_voidTarget;
   177         static final MethodHandle MH_voidCleanup;
   201         static final MethodHandle MH_voidCleanup;
       
   202         static final MethodHandle MH_throwingTarget;
       
   203         static final MethodHandle MH_catchingCleanup;
   178 
   204 
   179         static final MethodHandle MH_dummyTarget;
   205         static final MethodHandle MH_dummyTarget;
   180 
   206 
   181         static final MethodType MT_hello = methodType(String.class, String.class);
   207         static final MethodType MT_hello = methodType(String.class, String.class);
   182         static final MethodType MT_printHello = methodType(void.class, String.class);
   208         static final MethodType MT_printHello = methodType(void.class, String.class);
   190                 MH_printMore = LOOKUP.findStatic(TRY_FINALLY, "printMore", MT_printMore);
   216                 MH_printMore = LOOKUP.findStatic(TRY_FINALLY, "printMore", MT_printMore);
   191                 MH_greetMore = LOOKUP.findStatic(TRY_FINALLY, "greetMore", MT_greetMore);
   217                 MH_greetMore = LOOKUP.findStatic(TRY_FINALLY, "greetMore", MT_greetMore);
   192                 MH_exclaimMore = LOOKUP.findStatic(TRY_FINALLY, "exclaimMore", MT_exclaimMore);
   218                 MH_exclaimMore = LOOKUP.findStatic(TRY_FINALLY, "exclaimMore", MT_exclaimMore);
   193                 MH_voidTarget = LOOKUP.findStatic(TRY_FINALLY, "voidTarget", MT_voidTarget);
   219                 MH_voidTarget = LOOKUP.findStatic(TRY_FINALLY, "voidTarget", MT_voidTarget);
   194                 MH_voidCleanup = LOOKUP.findStatic(TRY_FINALLY, "voidCleanup", MT_voidCleanup);
   220                 MH_voidCleanup = LOOKUP.findStatic(TRY_FINALLY, "voidCleanup", MT_voidCleanup);
       
   221                 MH_throwingTarget = LOOKUP.findStatic(TRY_FINALLY, "throwingTarget", MT_throwingTarget);
       
   222                 MH_catchingCleanup = LOOKUP.findStatic(TRY_FINALLY, "catchingCleanup", MT_catchingCleanup);
   195                 MH_dummyTarget = MethodHandles.dropArguments(MH_voidTarget, 0, int.class, long.class, Object.class,
   223                 MH_dummyTarget = MethodHandles.dropArguments(MH_voidTarget, 0, int.class, long.class, Object.class,
   196                         int.class, long.class, Object.class);
   224                         int.class, long.class, Object.class);
   197             } catch (Exception e) {
   225             } catch (Exception e) {
   198                 throw new ExceptionInInitializerError(e);
   226                 throw new ExceptionInInitializerError(e);
   199             }
   227             }