Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/shared/TestContext.java
41161 views
/*1* Copyright (c) 2013, 2021, 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*/2223package vm.runtime.defmeth.shared;2425import vm.runtime.defmeth.shared.data.method.body.CallMethod;26import vm.runtime.defmeth.shared.executor.MHInvokeWithArgsTest;2728import java.lang.invoke.MethodHandle;29import java.lang.invoke.MethodHandles;30import java.lang.invoke.MethodType;31import java.lang.reflect.Constructor;32import java.lang.reflect.Executable;33import java.lang.reflect.InvocationTargetException;34import java.lang.reflect.Method;3536/**37* Encapsulates test context and provides utility methods to invoke methods38* in that context through reflection. Should be loaded in the same class loader as all other test classes.39* It is needed for correct resolution of initiating class loader in case of reflection invocation scenario.40*/41public class TestContext {42public static Object invoke(Method m, Object obj, Object... args)43throws InvocationTargetException, IllegalAccessException44{45return m.invoke(obj, args);46}4748public static Object invoke(Constructor m, Object... args)49throws InvocationTargetException, IllegalAccessException, InstantiationException {50return m.newInstance(args);51}5253public static Object invokeWithArguments(CallMethod.Invoke invokeType, Class<?> declaringClass,54String methodName, MethodType type, Object... arguments) throws Throwable {55// Need to do method lookup in the right context56// Otherwise, ClassNotFoundException is thrown57MethodHandles.Lookup LOOKUP = MethodHandles.lookup();58MethodHandle mh;5960// FYI: can't use switch over enum here, because to implement it javac produces auxiliary class TestContext$161// and it can't be accessed from the context where TestContext is loaded62if (invokeType == CallMethod.Invoke.VIRTUAL || invokeType == CallMethod.Invoke.INTERFACE) {63mh = LOOKUP.findVirtual(declaringClass, methodName, type);64} else if (invokeType == CallMethod.Invoke.SPECIAL) {65if (methodName.equals("<init>") && type.returnType() == void.class) {66mh = LOOKUP.findConstructor(declaringClass, type);67} else {68mh = LOOKUP.findSpecial(declaringClass, methodName, type, declaringClass);69}70} else if (invokeType == CallMethod.Invoke.STATIC) {71mh = LOOKUP.findStatic(declaringClass, methodName, type);72} else {73throw new Error("Unknown invoke instruction: "+invokeType);74}7576return mh.invokeWithArguments(arguments);77}78}79808182