5452N/A/*
5452N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
5452N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5452N/A *
5452N/A * This code is free software; you can redistribute it and/or modify it
5452N/A * under the terms of the GNU General Public License version 2 only, as
5452N/A * published by the Free Software Foundation. Oracle designates this
5452N/A * particular file as subject to the "Classpath" exception as provided
5452N/A * by Oracle in the LICENSE file that accompanied this code.
5452N/A *
5452N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5452N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5452N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5452N/A * version 2 for more details (a copy is included in the LICENSE file that
5452N/A * accompanied this code).
5452N/A *
5452N/A * You should have received a copy of the GNU General Public License version
5452N/A * 2 along with this work; if not, write to the Free Software Foundation,
5452N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5452N/A *
5452N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5452N/A * or visit www.oracle.com if you need additional information or have any
5452N/A * questions.
5452N/A */
5452N/A
5452N/A/* @test
5452N/A * @summary unit tests for method handles which permute their arguments
5452N/A * @run junit test.java.lang.invoke.ThrowExceptionsTest
5452N/A */
5452N/A
5452N/Apackage test.java.lang.invoke;
5452N/A
5452N/Aimport org.junit.*;
5452N/A
5452N/Aimport java.util.*;
5452N/Aimport java.lang.reflect.*;
5452N/A
5452N/Aimport java.lang.invoke.*;
5452N/Aimport static java.lang.invoke.MethodHandles.*;
5452N/Aimport static java.lang.invoke.MethodType.*;
5452N/A
5452N/Apublic class ThrowExceptionsTest {
5455N/A private static final Class<?> CLASS = ThrowExceptionsTest.class;
5452N/A private static final Lookup LOOKUP = lookup();
5452N/A
5452N/A public static void main(String argv[]) throws Throwable {
5452N/A new ThrowExceptionsTest().testAll((argv.length == 0 ? null : Arrays.asList(argv).toString()));
5452N/A }
5452N/A
5452N/A @Test
5452N/A public void testWMT() throws Throwable {
5452N/A // mostly call testWMTCallee, but sometimes call its void-returning variant
5452N/A MethodHandle mh = testWMTCallee();
5452N/A MethodHandle mh1 = mh.asType(mh.type().changeReturnType(void.class));
5452N/A assert(mh1 != mh);
5452N/A testWMT(mh, mh1, 1000);
5452N/A }
5452N/A
5452N/A @Test
5452N/A public void testBoundWMT() throws Throwable {
5452N/A // mostly call exactInvoker.bindTo(testWMTCallee), but sometimes call its void-returning variant
5452N/A MethodHandle callee = testWMTCallee();
5452N/A MethodHandle callee1 = callee.asType(callee.type().changeReturnType(void.class));
5452N/A MethodHandle invoker = exactInvoker(callee.type());
5452N/A MethodHandle mh = invoker.bindTo(callee);
5452N/A MethodHandle mh1 = invoker.bindTo(callee1);
5452N/A testWMT(mh, mh1, 1000);
5452N/A }
5452N/A
5452N/A @Test
5452N/A public void testFoldWMT() throws Throwable {
5452N/A // mostly call exactInvoker.fold(constant(testWMTCallee)), but sometimes call its void-returning variant
5452N/A MethodHandle callee = testWMTCallee();
5452N/A MethodHandle callee1 = callee.asType(callee.type().changeReturnType(void.class));
5452N/A MethodHandle invoker = exactInvoker(callee.type());
5452N/A MethodHandle mh = foldArguments(invoker, constant(MethodHandle.class, callee));
5452N/A MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
5452N/A testWMT(mh, mh1, 1000);
5452N/A }
5452N/A
5452N/A @Test
5452N/A public void testFoldCCE() throws Throwable {
5452N/A MethodHandle callee = testWMTCallee();
5452N/A MethodHandle callee1 = callee.asType(callee.type().changeParameterType(1, Number.class)).asType(callee.type());
5452N/A MethodHandle invoker = exactInvoker(callee.type());
5452N/A MethodHandle mh = foldArguments(invoker, constant(MethodHandle.class, callee));
5452N/A MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
5452N/A testWMT(mh, mh1, 1000);
5452N/A }
5452N/A
5452N/A @Test
5452N/A public void testStackOverflow() throws Throwable {
5452N/A MethodHandle callee = testWMTCallee();
5452N/A MethodHandle callee1 = makeStackOverflow().asType(callee.type());
5452N/A MethodHandle invoker = exactInvoker(callee.type());
5452N/A MethodHandle mh = foldArguments(invoker, constant(MethodHandle.class, callee));
5452N/A MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
5452N/A for (int i = 0; i < REPEAT; i++) {
5452N/A try {
5452N/A testWMT(mh, mh1, 1000);
5452N/A } catch (StackOverflowError ex) {
5452N/A // OK, try again
5452N/A }
5452N/A }
5452N/A }
5452N/A
5452N/A private static MethodHandle makeStackOverflow() {
5452N/A MethodType cellType = methodType(void.class);
5452N/A MethodHandle[] cell = { null }; // recursion point
5452N/A MethodHandle getCell = insertArguments(arrayElementGetter(cell.getClass()), 0, cell, 0);
5452N/A MethodHandle invokeCell = foldArguments(exactInvoker(cellType), getCell);
5452N/A assert(invokeCell.type() == cellType);
5452N/A cell[0] = invokeCell;
5452N/A // make it conformable to any type:
5452N/A invokeCell = dropArguments(invokeCell, 0, Object[].class).asVarargsCollector(Object[].class);
5452N/A return invokeCell;
5452N/A }
5452N/A
5452N/A static int testCases;
5452N/A
5452N/A private void testAll(String match) throws Throwable {
5452N/A testCases = 0;
5452N/A Lookup lookup = lookup();
5452N/A for (Method m : CLASS.getDeclaredMethods()) {
5452N/A String name = m.getName();
5452N/A if (name.startsWith("test") &&
5452N/A (match == null || match.contains(name.substring("test".length()))) &&
5452N/A m.getParameterTypes().length == 0 &&
5452N/A Modifier.isPublic(m.getModifiers()) &&
5452N/A !Modifier.isStatic(m.getModifiers())) {
5452N/A System.out.println("["+name+"]");
5452N/A int tc = testCases;
5452N/A try {
5452N/A m.invoke(this);
5455N/A } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
5452N/A System.out.println("*** "+ex);
5455N/A ex.printStackTrace(System.out);
5452N/A }
5452N/A if (testCases == tc) testCases++;
5452N/A }
5452N/A }
5452N/A if (testCases == 0) throw new RuntimeException("no test cases found");
5452N/A System.out.println("ran a total of "+testCases+" test cases");
5452N/A }
5452N/A
5452N/A private static MethodHandle findStatic(String name) {
5452N/A return findMethod(name, true);
5452N/A }
5452N/A private static MethodHandle findVirtual(String name) {
5452N/A return findMethod(name, false);
5452N/A }
5452N/A private static MethodHandle findMethod(String name, boolean isStatic) {
5452N/A MethodHandle mh = null;
5452N/A for (Method m : CLASS.getDeclaredMethods()) {
5452N/A if (m.getName().equals(name) &&
5452N/A Modifier.isStatic(m.getModifiers()) == isStatic) {
5452N/A if (mh != null)
5452N/A throw new RuntimeException("duplicate methods: "+name);
5452N/A try {
5452N/A mh = LOOKUP.unreflect(m);
5452N/A } catch (ReflectiveOperationException ex) {
5452N/A throw new RuntimeException(ex);
5452N/A }
5452N/A }
5452N/A }
5452N/A if (mh == null)
5452N/A throw new RuntimeException("no method: "+name);
5452N/A return mh;
5452N/A }
5452N/A
5452N/A int testWMTCallee;
5452N/A private int testWMTCallee(String x) {
5452N/A return testWMTCallee++;
5452N/A }
5452N/A private static MethodHandle testWMTCallee() {
5452N/A MethodHandle callee = findVirtual("testWMTCallee");
5452N/A // FIXME: should not have to retype callee
5452N/A callee = callee.asType(callee.type().changeParameterType(0, Object.class));
5452N/A return callee;
5452N/A }
5452N/A
5452N/A private Exception testWMT(MethodHandle[] mhs, int reps) throws Throwable {
5452N/A testCases += 1;
5452N/A testWMTCallee = 0;
5452N/A int catches = 0;
5452N/A Exception savedEx = null;
5452N/A for (int i = 0; i < reps; i++) {
5452N/A MethodHandle mh = mhs[i % mhs.length];
5452N/A int n;
5452N/A try {
5452N/A // FIXME: should not have to retype this
5452N/A n = (int) mh.invokeExact((Object)this, "x");
5452N/A assertEquals(n, i - catches);
5452N/A // Using the exact type for this causes endless deopt due to
5452N/A // 'non_cached_result' in SystemDictionary::find_method_handle_invoke.
5452N/A // The problem is that the compiler thread needs to access a cached
5452N/A // invoke method, but invoke methods are not cached if one of the
5452N/A // component types is not on the BCP.
5452N/A } catch (Exception ex) {
5452N/A savedEx = ex;
5452N/A catches++;
5452N/A }
5452N/A }
5452N/A //VERBOSE: System.out.println("reps="+reps+" catches="+catches);
5452N/A return savedEx;
5452N/A }
5452N/A
5452N/A private static final int REPEAT = Integer.getInteger(CLASS.getSimpleName()+".REPEAT", 10);
5452N/A
5452N/A private Exception testWMT(MethodHandle mh, MethodHandle mh1, int reps) throws Throwable {
5452N/A //VERBOSE: System.out.println("mh="+mh+" mh1="+mh1);
5452N/A MethodHandle[] mhs = new MethodHandle[100];
5452N/A Arrays.fill(mhs, mh);
5452N/A int patch = mhs.length-1;
5452N/A Exception savedEx = null;
5452N/A for (int i = 0; i < REPEAT; i++) {
5452N/A mhs[patch] = mh;
5452N/A testWMT(mhs, 10000);
5452N/A mhs[patch] = mh1;
5452N/A savedEx = testWMT(mhs, reps);
5452N/A }
5452N/A return savedEx;
5452N/A }
5452N/A
5459N/A private static void assertEquals(Object x, Object y) {
5452N/A if (x == y || x != null && x.equals(y)) return;
5452N/A throw new RuntimeException(x+" != "+y);
5452N/A }
5452N/A}