1 /* 2 * Copyright (c) 2013, 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 /* 27 * @test 28 * @bug 8019184 29 * @summary MethodHandles.catchException() fails when methods have 8 args + varargs 30 */ 31 32 import java.util.*; 33 import java.lang.invoke.*; 34 35 public class TestCatchExceptionWithVarargs { 36 37 private static final Class<?> CLASS = TestCatchExceptionWithVarargs.class; 38 private static final int MAX_MH_ARITY = 254; 39 40 public static MethodHandle target; 41 public static MethodHandle handler; 42 43 private static Object firstArg; 44 45 static class MyException extends Exception { 46 } 47 48 public static Object target(Object... a) throws Exception { 49 if (a[0] != firstArg) { 50 throw new AssertionError("first argument different than expected: " + a[0] + " != " + firstArg); 51 } 52 throw new MyException(); 53 } 54 55 public static Object handler(Object... a) { 56 if (a[0] != firstArg) { 57 throw new AssertionError("first argument different than expected: " + a[0] + " != " + firstArg); 58 } 59 return a[0]; 60 } 61 62 static { 63 try { 64 MethodType mtype = MethodType.methodType(Object.class, Object[].class); 65 target = MethodHandles.lookup().findStatic(CLASS, "target", mtype); 66 handler = MethodHandles.lookup().findStatic(CLASS, "handler", mtype); 67 } catch (Exception e) { 68 throw new AssertionError(e); 69 } 70 } 71 72 public static void main(String[] args) throws Throwable { 73 List<Class<?>> ptypes = new LinkedList<>(); 74 ptypes.add(Object[].class); 75 76 // We use MAX_MH_ARITY - 1 here to account for the Object[] argument. 77 for (int i = 1; i < MAX_MH_ARITY - 1; i++) { 78 ptypes.add(0, Object.class); 79 80 MethodHandle targetWithArgs = target.asType(MethodType.methodType(Object.class, ptypes)); 81 MethodHandle handlerWithArgs = handler.asType(MethodType.methodType(Object.class, ptypes)); 82 handlerWithArgs = MethodHandles.dropArguments(handlerWithArgs, 0, MyException.class); 83 84 MethodHandle gwc1 = MethodHandles.catchException(targetWithArgs, MyException.class, handlerWithArgs); 85 86 // The next line throws an IllegalArgumentException if there is a bug. 87 MethodHandle gwc2 = MethodHandles.catchException(gwc1, MyException.class, handlerWithArgs); 88 89 // This is only to verify that the method handles can actually be invoked and do the right thing. 90 firstArg = new Object(); 91 Object o = gwc2.asSpreader(Object[].class, ptypes.size() - 1).invoke(firstArg, new Object[i]); 92 if (o != firstArg) { 93 throw new AssertionError("return value different than expected: " + o + " != " + firstArg); 94 } 95 } 96 } 97 }