1 /* 2 * Copyright (c) 2013, 2018, 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 package vm.runtime.defmeth.shared; 25 26 import vm.runtime.defmeth.shared.data.method.body.CallMethod; 27 import vm.runtime.defmeth.shared.executor.MHInvokeWithArgsTest; 28 29 import java.lang.invoke.MethodHandle; 30 import java.lang.invoke.MethodHandles; 31 import java.lang.invoke.MethodType; 32 import java.lang.reflect.InvocationTargetException; 33 import java.lang.reflect.Method; 34 35 /** 36 * Encapsulates test context and provides utility methods to invoke methods 37 * in that context through reflection. Should be loaded in the same class loader as all other test classes. 38 * It is needed for correct resolution of initiating class loader in case of reflection invocation scenario. 39 */ 40 public class TestContext { invoke(Method m, Object obj, Object... args)41 public static Object invoke(Method m, Object obj, Object... args) 42 throws InvocationTargetException, IllegalAccessException 43 { 44 return m.invoke(obj, args); 45 } 46 invokeWithArguments(CallMethod.Invoke invokeType, Class<?> declaringClass, String methodName, MethodType type, Object... arguments)47 public static Object invokeWithArguments(CallMethod.Invoke invokeType, Class<?> declaringClass, 48 String methodName, MethodType type, Object... arguments) throws Throwable { 49 // Need to do method lookup in the right context 50 // Otherwise, ClassNotFoundException is thrown 51 MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); 52 MethodHandle mh; 53 54 // FYI: can't use switch over enum here, because to implement it javac produces auxiliary class TestContext$1 55 // and it can't be accessed from the context where TestContext is loaded 56 if (invokeType == CallMethod.Invoke.VIRTUAL || invokeType == CallMethod.Invoke.INTERFACE) { 57 mh = LOOKUP.findVirtual(declaringClass, methodName, type); 58 } else if (invokeType == CallMethod.Invoke.SPECIAL) { 59 mh = LOOKUP.findSpecial(declaringClass, methodName, type, declaringClass); 60 } else if (invokeType == CallMethod.Invoke.STATIC) { 61 mh = LOOKUP.findStatic(declaringClass, methodName, type); 62 } else { 63 throw new Error("Unknown invoke instruction: "+invokeType); 64 } 65 66 return mh.invokeWithArguments(arguments); 67 } 68 } 69 70