Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/shared/TestContext.java
41161 views
1
/*
2
* Copyright (c) 2013, 2021, 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.Constructor;
33
import java.lang.reflect.Executable;
34
import java.lang.reflect.InvocationTargetException;
35
import java.lang.reflect.Method;
36
37
/**
38
* Encapsulates test context and provides utility methods to invoke methods
39
* in that context through reflection. Should be loaded in the same class loader as all other test classes.
40
* It is needed for correct resolution of initiating class loader in case of reflection invocation scenario.
41
*/
42
public class TestContext {
43
public static Object invoke(Method m, Object obj, Object... args)
44
throws InvocationTargetException, IllegalAccessException
45
{
46
return m.invoke(obj, args);
47
}
48
49
public static Object invoke(Constructor m, Object... args)
50
throws InvocationTargetException, IllegalAccessException, InstantiationException {
51
return m.newInstance(args);
52
}
53
54
public static Object invokeWithArguments(CallMethod.Invoke invokeType, Class<?> declaringClass,
55
String methodName, MethodType type, Object... arguments) throws Throwable {
56
// Need to do method lookup in the right context
57
// Otherwise, ClassNotFoundException is thrown
58
MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
59
MethodHandle mh;
60
61
// FYI: can't use switch over enum here, because to implement it javac produces auxiliary class TestContext$1
62
// and it can't be accessed from the context where TestContext is loaded
63
if (invokeType == CallMethod.Invoke.VIRTUAL || invokeType == CallMethod.Invoke.INTERFACE) {
64
mh = LOOKUP.findVirtual(declaringClass, methodName, type);
65
} else if (invokeType == CallMethod.Invoke.SPECIAL) {
66
if (methodName.equals("<init>") && type.returnType() == void.class) {
67
mh = LOOKUP.findConstructor(declaringClass, type);
68
} else {
69
mh = LOOKUP.findSpecial(declaringClass, methodName, type, declaringClass);
70
}
71
} else if (invokeType == CallMethod.Invoke.STATIC) {
72
mh = LOOKUP.findStatic(declaringClass, methodName, type);
73
} else {
74
throw new Error("Unknown invoke instruction: "+invokeType);
75
}
76
77
return mh.invokeWithArguments(arguments);
78
}
79
}
80
81
82