1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26/* @test 27 * @bug 8147078 28 * @run testng/othervm -ea -esa Test8147078 29 */ 30 31import org.testng.annotations.Test; 32 33import java.lang.invoke.MethodHandle; 34import java.lang.invoke.MethodHandles; 35 36import static java.lang.invoke.MethodType.methodType; 37 38import static org.testng.AssertJUnit.*; 39 40public class Test8147078 { 41 42 static int target(int x) { 43 throw new RuntimeException("ieps"); 44 } 45 46 static int handler(String s, int x) { 47 return 4*x; 48 } 49 50 static final MethodHandle MH_target; 51 static final MethodHandle MH_handler; 52 static final MethodHandle MH_catchException; 53 54 static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); 55 56 static { 57 try { 58 Class<Test8147078> C = Test8147078.class; 59 MH_target = LOOKUP.findStatic(C, "target", methodType(int.class, int.class)); 60 MH_handler = LOOKUP.findStatic(C, "handler", methodType(int.class, String.class, int.class)); 61 MH_catchException = LOOKUP.findStatic(MethodHandles.class, "catchException", 62 methodType(MethodHandle.class, MethodHandle.class, Class.class, MethodHandle.class)); 63 } catch (Exception e) { 64 throw new ExceptionInInitializerError(e); 65 } 66 } 67 68 @Test 69 public void testNoExceptionType() { 70 boolean caught = false; 71 try { 72 MethodHandle eek = (MethodHandle) MH_catchException.invoke(MH_target, String.class, MH_handler); 73 } catch (ClassCastException cce) { 74 assertEquals("java.lang.String", cce.getMessage()); 75 caught = true; 76 } catch (Throwable t) { 77 fail("unexpected exception caught: " + t); 78 } 79 assertTrue(caught); 80 } 81 82}