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 interface MyFunctionalInterface { 28 void invokeMethodReference(); 29 } 30 31 class MethodInvoker { 32 /** 33 * Use a method handle to invoke. 34 */ 35 public static void invoke() { 36 MyFunctionalInterface fi = null; 37 fi = new MethodSupplier()::<Integer, String, Long>m; 38 fi.invokeMethodReference(); 39 } 40 /** 41 * Invoke directly. 42 */ 43 public static void invoke2() { 44 MethodSupplier ms = new MethodSupplier(); 45 ms.m(); 46 } 47 } 48 /** 49 * 50 */ 51 public class InvokeSeveralWays { 52 public static void main(String args[]) throws Exception { 53 int failures = 0; 54 try { 55 Class.forName("MethodInvoker").getMethod("invoke").invoke(null); 56 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 57 failures++; 58 } catch (InvocationTargetException e) { 59 Throwable c = e.getCause(); 60 if (c instanceof IllegalAccessError) 61 System.out.println("EXPECTED: IllegalAccessError " + c); 62 else { 63 failures++; 64 System.out.println("FAIL: Unexpected wrapped exception " + c); 65 e.printStackTrace(System.out); 66 } 67 } catch (Throwable e) { 68 failures++; 69 System.out.println("FAIL: Unexpected exception has been caught " + e); 70 e.printStackTrace(System.out); 71 } 72 System.out.println(); 73 try { 74 Class.forName("MethodInvoker").getMethod("invoke2").invoke(null); 75 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 76 failures++; 77 } catch (InvocationTargetException e) { 78 Throwable c = e.getCause(); 79 if (c instanceof IllegalAccessError) 80 System.out.println("EXPECTED: IllegalAccessError " + c); 81 else { 82 failures++; 83 System.out.println("FAIL: Unexpected wrapped exception " + c); 84 e.printStackTrace(System.out); 85 } 86 } catch (Throwable e) { 87 failures++; 88 System.out.println("FAIL: Unexpected exception has been caught " + e); 89 e.printStackTrace(System.out); 90 } 91 System.out.println(); 92 try { 93 MethodInvoker.invoke(); 94 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 95 failures++; 96 } catch (IllegalAccessError e) { 97 System.out.println("EXPECTED: IllegalAccessError " + e); 98 } catch (Throwable e) { 99 failures++; 100 System.out.println("FAIL: Unexpected exception has been caught " + e); 101 e.printStackTrace(System.out); 102 } 103 System.out.println(); 104 try { 105 MethodInvoker.invoke2(); 106 System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); 107 failures++; 108 } catch (IllegalAccessError e) { 109 System.out.println("EXPECTED: IllegalAccessError " + e); 110 } catch (Throwable e) { 111 failures++; 112 System.out.println("FAIL: Unexpected exception has been caught " + e); 113 e.printStackTrace(System.out); 114 } 115 System.out.println(); 116 if (failures > 0) { 117 System.out.println("Saw " + failures + " failures"); 118 throw new Error("Saw " + failures + " failures, see standard out for details"); 119 } 120 } 121 }