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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 import java.lang.reflect.InvocationTargetException; 26 27 /** 28 * Tries various ways of ultimately invoking MethodSupplier.m(), 29 * except that m has been made inaccessible and some exception should be 30 * thrown instead. 31 */ 32 public class InvokeSeveralWays { 33 public static int test(String args[], Class expected) throws Exception { 34 int failures = 0; 35 try { 36 Class.forName("Invoker").getMethod("invoke").invoke(null); 37 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 38 failures++; 39 } catch (InvocationTargetException e) { 40 Throwable c = e.getCause(); 41 if (BootstrapMethodError.class.isInstance(c)) { 42 c = c.getCause(); 43 if (expected.isInstance(c)) 44 System.out.println("EXPECTED: " + expected.getName() + ", "+ c); 45 else { 46 failures++; 47 System.out.println("FAIL: Unexpected wrapped exception " + c); 48 e.printStackTrace(System.out); 49 } 50 } else { 51 failures++; 52 System.out.println("FAIL: Exception from MethodHandle invocation not wrapped in BootstrapMethodError " + c); 53 e.printStackTrace(System.out); 54 } 55 } catch (Throwable e) { 56 failures++; 57 System.out.println("FAIL: Unexpected exception has been caught " + e); 58 e.printStackTrace(System.out); 59 } 60 System.out.println(); 61 try { 62 Class.forName("Invoker").getMethod("invoke2").invoke(null); 63 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 64 failures++; 65 } catch (InvocationTargetException e) { 66 Throwable c = e.getCause(); 67 if (expected.isInstance(c)) 68 System.out.println("EXPECTED: " + expected.getName() + ", "+ c); 69 else { 70 failures++; 71 System.out.println("FAIL: Unexpected wrapped exception " + c); 72 e.printStackTrace(System.out); 73 } 74 } catch (Throwable e) { 75 failures++; 76 System.out.println("FAIL: Unexpected exception has been caught " + e); 77 e.printStackTrace(System.out); 78 } 79 System.out.println(); 80 try { 81 Invoker.invoke(); 82 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 83 failures++; 84 } catch (BootstrapMethodError e) { 85 Throwable c = e.getCause(); 86 if (expected.isInstance(c)) 87 System.out.println("EXPECTED: " + expected.getName() + ", "+ c); 88 else { 89 failures++; 90 System.out.println("FAIL: Unexpected exception has been caught " + c); 91 e.printStackTrace(System.out); 92 } 93 } catch (Throwable e) { 94 failures++; 95 System.out.println("FAIL: Exception from MethodHandle invocation not wrapped in BootstrapMethodError " + e); 96 e.printStackTrace(System.out); 97 } 98 System.out.println(); 99 try { 100 Invoker.invoke2(); 101 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 102 failures++; 103 } catch (Throwable e) { 104 if (expected.isInstance(e)) 105 System.out.println("EXPECTED: " + expected.getName() + ", "+ e); 106 else { 107 failures++; 108 System.out.println("FAIL: Unexpected exception has been caught " + e); 109 e.printStackTrace(System.out); 110 } 111 } 112 System.out.println(); 113 if (failures > 0) { 114 System.out.println("Saw " + failures + " failures"); 115 } 116 return failures; 117 } 118 }