Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/TestCatchExceptionWithVarargs.java
47209 views
/*1* Copyright (c) 2013, 2015, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 801918426* @library /lib/testlibrary /lib/testlibrary/jsr29227* @summary MethodHandles.catchException() fails when methods have 8 args + varargs28* @run main TestCatchExceptionWithVarargs29*/3031import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;32import java.util.*;33import java.lang.invoke.*;3435public class TestCatchExceptionWithVarargs {3637private static final Class<?> CLASS = TestCatchExceptionWithVarargs.class;38private static final int MAX_MH_ARITY = 254;3940public static MethodHandle target;41public static MethodHandle handler;4243private static Object firstArg;4445static class MyException extends Exception {46}4748public static Object target(Object... a) throws Exception {49if (a[0] != firstArg) {50throw new AssertionError("first argument different than expected: " + a[0] + " != " + firstArg);51}52throw new MyException();53}5455public static Object handler(Object... a) {56if (a[0] != firstArg) {57throw new AssertionError("first argument different than expected: " + a[0] + " != " + firstArg);58}59return a[0];60}6162static {63try {64MethodType mtype = MethodType.methodType(Object.class, Object[].class);65target = MethodHandles.lookup().findStatic(CLASS, "target", mtype);66handler = MethodHandles.lookup().findStatic(CLASS, "handler", mtype);67} catch (Exception e) {68throw new AssertionError(e);69}70}7172public static void main(String[] args) throws Throwable {73CodeCacheOverflowProcessor74.runMHTest(TestCatchExceptionWithVarargs::test);75}7677public static void test() throws Throwable {78List<Class<?>> ptypes = new LinkedList<>();79ptypes.add(Object[].class);8081// We use MAX_MH_ARITY - 1 here to account for the Object[] argument.82for (int i = 1; i < MAX_MH_ARITY - 1; i++) {83ptypes.add(0, Object.class);8485MethodHandle targetWithArgs = target.asType(MethodType.methodType(Object.class, ptypes));86MethodHandle handlerWithArgs = handler.asType(MethodType.methodType(Object.class, ptypes));87handlerWithArgs = MethodHandles.dropArguments(handlerWithArgs, 0, MyException.class);8889MethodHandle gwc1 = MethodHandles.catchException(targetWithArgs, MyException.class, handlerWithArgs);9091// The next line throws an IllegalArgumentException if there is a bug.92MethodHandle gwc2 = MethodHandles.catchException(gwc1, MyException.class, handlerWithArgs);9394// This is only to verify that the method handles can actually be invoked and do the right thing.95firstArg = new Object();96Object o = gwc2.asSpreader(Object[].class, ptypes.size() - 1).invoke(firstArg, new Object[i]);97if (o != firstArg) {98throw new AssertionError("return value different than expected: " + o + " != " + firstArg);99}100}101}102}103104105