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