Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/MutableCallSiteTest.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9.jsr292;
23
24
import org.testng.annotations.Test;
25
import org.testng.AssertJUnit;
26
import java.lang.invoke.MethodHandle;
27
import java.lang.invoke.MethodHandles;
28
import java.lang.invoke.MethodType;
29
import java.lang.invoke.MutableCallSite;
30
import java.lang.invoke.WrongMethodTypeException;
31
32
/**
33
* @author mesbah
34
* This class contains tests for MutableCallSite API
35
*/
36
public class MutableCallSiteTest {
37
/**
38
* Basic sanity test for MutableCallSite
39
* @throws Throwable
40
*/
41
@Test(groups = { "level.extended" })
42
public void testBasic_MutableCallSite() throws Throwable {
43
MethodType mt = MethodType.methodType(double.class,String.class);
44
MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", mt);
45
MutableCallSite mcs = new MutableCallSite(mt);
46
mcs.setTarget(mh);
47
double d = (double)mcs.dynamicInvoker().invokeExact("1.1");
48
49
AssertJUnit.assertEquals(1.1,d);
50
}
51
52
/**
53
* Test for validating default MethodHandle behavior of MutableCallSite
54
* using MutableCallSite.dynamicInvoker().invoke()
55
* @throws Throwable
56
*/
57
@Test(groups = { "level.extended" })
58
public void testIllegalState_dynamicInvoker_MutableCallSite() throws Throwable {
59
MethodType mt = MethodType.methodType(void.class);
60
MutableCallSite mcs = new MutableCallSite(mt);
61
boolean iseHit = false;
62
63
try {
64
mcs.dynamicInvoker().invoke();
65
}
66
catch ( IllegalStateException e ) {
67
iseHit = true;
68
}
69
70
AssertJUnit.assertTrue(iseHit);
71
}
72
73
/**
74
* Test for validating default MethodHandle behavior of MutableCallSite
75
* using MutableCallSite.getTarget().invoke()
76
* @throws Throwable
77
*/
78
@Test(groups = { "level.extended" })
79
public void testIllegalState_getTarget_MutableCallSite() throws Throwable {
80
MethodType mt = MethodType.methodType(void.class);
81
MutableCallSite mcs = new MutableCallSite(mt);
82
boolean iseHit = false;
83
84
try {
85
mcs.getTarget().invoke();
86
}
87
catch ( IllegalStateException e ) {
88
iseHit = true;
89
}
90
91
AssertJUnit.assertTrue(iseHit);
92
}
93
94
/**
95
* Basic negative test for MutableCallSite
96
* @throws Throwable
97
*/
98
@Test(groups = { "level.extended" })
99
public void testBasicNegative_MutableCallSite() throws Throwable {
100
MethodHandle mh = MethodHandles.lookup().findStatic(Math.class, "pow", MethodType.methodType(double.class,double.class,double.class));
101
MutableCallSite mcs = new MutableCallSite(mh);
102
103
boolean wmtThrown = false;
104
105
try {
106
String s = (String)mcs.dynamicInvoker().invokeExact(2,3);
107
}
108
catch ( WrongMethodTypeException e ) {
109
wmtThrown = true;
110
}
111
112
AssertJUnit.assertTrue(wmtThrown);
113
}
114
115
/**
116
* Test for MutableCallSite.type()
117
* @throws Throwable
118
*/
119
@Test(groups = { "level.extended" })
120
public void testType() throws Throwable {
121
MethodType mt = MethodType.methodType(String.class);
122
MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", mt);
123
MutableCallSite mcs = new MutableCallSite(mh);
124
125
AssertJUnit.assertEquals(mt,mcs.type());
126
}
127
128
/**
129
* MutableCallSite subclass that overrides setTarget, one where setTarget calls the inherited one.
130
* @throws Throwable
131
*/
132
@Test(groups = { "level.extended" })
133
public void testMCSChild_CallsParent() throws Throwable {
134
MethodType mt = MethodType.methodType(double.class,String.class);
135
MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", mt);
136
MCSChild1 mcs = new MCSChild1(mt);
137
mcs.setTarget(mh);
138
double d = (double)mcs.dynamicInvoker().invokeExact("1.1");
139
140
AssertJUnit.assertEquals(1.1,d);
141
}
142
143
/**
144
* MutableCallSite subclass that overrides setTarget, one where setTarget does not calls the inherited one.
145
* @throws Throwable
146
*/
147
@Test(groups = { "level.extended" })
148
public void testMCSChild_DoesNotCallParent() throws Throwable {
149
MethodType mt = MethodType.methodType(double.class,String.class);
150
MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", mt);
151
MCSChild2 mcs = new MCSChild2(mt);
152
mcs.setTarget(mh);
153
154
AssertJUnit.assertEquals(mcs.ignoredTarget.toString(),mh.toString());
155
}
156
}
157
158
159
160