Path: blob/master/test/jdk/java/lang/invoke/MethodHandles/TestCatchException.java
41152 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* @test27* @bug 802782328* @run junit test.java.lang.invoke.TestCatchException29*/30package test.java.lang.invoke;3132import java.lang.invoke.*;33import org.junit.*;34import static org.junit.Assert.*;3536public class TestCatchException {37static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();38static final MethodType M_TYPE = MethodType.methodType(int.class, Object.class, Object.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class);3940private static int noThrow(Object o1, Object o2, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {41return 42;42}4344private static int throwEx(Object o1, Object o2, int i1, int i2, int i3, int i4, int i5, int i6, int i7) throws Exception {45throw new Exception();46}4748private static int handler(Exception e) {49return 17;50}5152@Test53public void testNoThrowPath() throws Throwable {54MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "noThrow", M_TYPE);55MethodHandle handler = LOOKUP.findStatic(TestCatchException.class, "handler", MethodType.methodType(int.class, Exception.class));5657MethodHandle h = MethodHandles.catchException(target, Exception.class, handler);5859int x = (int)h.invokeExact(new Object(), new Object(), 1, 2, 3, 4, 5, 6, 7);60assertEquals(x, 42);61}6263@Test64public void testThrowPath() throws Throwable {65MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "throwEx", M_TYPE);66MethodHandle handler = LOOKUP.findStatic(TestCatchException.class, "handler", MethodType.methodType(int.class, Exception.class));6768MethodHandle h = MethodHandles.catchException(target, Exception.class, handler);6970int x = (int)h.invokeExact(new Object(), new Object(), 1, 2, 3, 4, 5, 6, 7);71assertEquals(x, 17);72}7374static final Object masterParam = new Object();75static final Object[] masterTail = new Object[] { "str" };76static Exception masterEx = new Exception();7778public static Object m1(Object o1, Object o2, Object o3, Object o4, Object o5,79Object o6, Object o7, Object o8, Object... tail) {80assertEquals(masterParam, o1);81assertEquals(masterParam, o2);82assertEquals(masterParam, o3);83assertEquals(masterParam, o4);84assertEquals(masterParam, o5);85assertEquals(masterParam, o6);86assertEquals(masterParam, o7);87assertEquals(masterParam, o8);88assertEquals(masterTail, tail);89return tail;90}9192public static Object m2(Exception e, Object o1, Object o2, Object o3, Object o4,93Object o5, Object o6, Object o7, Object o8, Object... tail) {94assertEquals(masterEx, e);95assertEquals(masterParam, o1);96assertEquals(masterParam, o2);97assertEquals(masterParam, o3);98assertEquals(masterParam, o4);99assertEquals(masterParam, o5);100assertEquals(masterParam, o6);101assertEquals(masterParam, o7);102assertEquals(masterParam, o8);103assertEquals(masterTail, tail);104return tail;105}106107public static Object throwEx(Object o1, Object o2, Object o3, Object o4, Object o5,108Object o6, Object o7, Object o8, Object... tail) throws Exception {109assertEquals(masterParam, o1);110assertEquals(masterParam, o2);111assertEquals(masterParam, o3);112assertEquals(masterParam, o4);113assertEquals(masterParam, o5);114assertEquals(masterParam, o6);115assertEquals(masterParam, o7);116assertEquals(masterParam, o8);117assertEquals(masterTail, tail);118throw masterEx;119}120121@Test122public void testVarargsCollectorNoThrow() throws Throwable {123MethodType t1 = MethodType.methodType(Object.class, Object.class, Object.class, Object.class, Object.class,124Object.class, Object.class, Object.class, Object.class, Object[].class);125126MethodType t2 = t1.insertParameterTypes(0, Exception.class);127128MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "m1", t1)129.asVarargsCollector(Object[].class);130MethodHandle catcher = LOOKUP.findStatic(TestCatchException.class, "m2", t2)131.asVarargsCollector(Object[].class);132MethodHandle gwc = MethodHandles.catchException(target, Exception.class, catcher);133134Object o = masterParam;135Object[] obj1 = masterTail;136137Object r2 = gwc.invokeExact(o, o, o, o, o, o, o, o, obj1);138assertEquals(r2, obj1);139}140141@Test142public void testVarargsCollectorThrow() throws Throwable {143MethodType t1 = MethodType.methodType(Object.class, Object.class, Object.class, Object.class, Object.class,144Object.class, Object.class, Object.class, Object.class, Object[].class);145146MethodType t2 = t1.insertParameterTypes(0, Exception.class);147148MethodHandle target = LOOKUP.findStatic(TestCatchException.class, "throwEx", t1)149.asVarargsCollector(Object[].class);150MethodHandle catcher = LOOKUP.findStatic(TestCatchException.class, "m2", t2)151.asVarargsCollector(Object[].class);152MethodHandle gwc = MethodHandles.catchException(target, Exception.class, catcher);153154Object o = masterParam;155Object[] obj1 = masterTail;156157Object r2 = gwc.invokeExact(o, o, o, o, o, o, o, o, obj1);158assertEquals(r2, obj1);159}160161public static void main(String[] args) throws Throwable {162TestCatchException test = new TestCatchException();163test.testNoThrowPath();164test.testThrowPath();165test.testVarargsCollectorNoThrow();166test.testVarargsCollectorThrow();167System.out.println("TEST PASSED");168}169}170171172