Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/8022701/InvokeSeveralWays.java
47311 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.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*22*/2324import java.lang.reflect.InvocationTargetException;2526/**27* Tries various ways of ultimately invoking MethodSupplier.m(),28* except that m has been made inaccessible and some exception should be29* thrown instead.30*/31public class InvokeSeveralWays {32public static int test(String args[], Class expected) throws Exception {33int failures = 0;34try {35Class.forName("Invoker").getMethod("invoke").invoke(null);36System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier");37failures++;38} catch (InvocationTargetException e) {39Throwable c = e.getCause();40if (BootstrapMethodError.class.isInstance(c)) {41c = c.getCause();42if (expected.isInstance(c))43System.out.println("EXPECTED: " + expected.getName() + ", "+ c);44else {45failures++;46System.out.println("FAIL: Unexpected wrapped exception " + c);47e.printStackTrace(System.out);48}49} else {50failures++;51System.out.println("FAIL: Exception from MethodHandle invocation not wrapped in BootstrapMethodError " + c);52e.printStackTrace(System.out);53}54} catch (Throwable e) {55failures++;56System.out.println("FAIL: Unexpected exception has been caught " + e);57e.printStackTrace(System.out);58}59System.out.println();60try {61Class.forName("Invoker").getMethod("invoke2").invoke(null);62System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier");63failures++;64} catch (InvocationTargetException e) {65Throwable c = e.getCause();66if (expected.isInstance(c))67System.out.println("EXPECTED: " + expected.getName() + ", "+ c);68else {69failures++;70System.out.println("FAIL: Unexpected wrapped exception " + c);71e.printStackTrace(System.out);72}73} catch (Throwable e) {74failures++;75System.out.println("FAIL: Unexpected exception has been caught " + e);76e.printStackTrace(System.out);77}78System.out.println();79try {80Invoker.invoke();81System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier");82failures++;83} catch (BootstrapMethodError e) {84Throwable c = e.getCause();85if (expected.isInstance(c))86System.out.println("EXPECTED: " + expected.getName() + ", "+ c);87else {88failures++;89System.out.println("FAIL: Unexpected exception has been caught " + c);90e.printStackTrace(System.out);91}92} catch (Throwable e) {93failures++;94System.out.println("FAIL: Exception from MethodHandle invocation not wrapped in BootstrapMethodError " + e);95e.printStackTrace(System.out);96}97System.out.println();98try {99Invoker.invoke2();100System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier");101failures++;102} catch (Throwable e) {103if (expected.isInstance(e))104System.out.println("EXPECTED: " + expected.getName() + ", "+ e);105else {106failures++;107System.out.println("FAIL: Unexpected exception has been caught " + e);108e.printStackTrace(System.out);109}110}111System.out.println();112if (failures > 0) {113System.out.println("Saw " + failures + " failures");114}115return failures;116}117}118119120