Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/invoke/InvokeWithArgumentsTest.java
41149 views
1
/*
2
* Copyright (c) 2017, 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
/* @test
25
* @summary basic tests for MethodHandle.invokeWithArguments
26
* @run testng test.java.lang.invoke.InvokeWithArgumentsTest
27
*/
28
29
package test.java.lang.invoke;
30
31
import org.testng.Assert;
32
import org.testng.annotations.Test;
33
34
import java.lang.invoke.MethodHandle;
35
import java.lang.invoke.MethodHandles;
36
import java.lang.invoke.WrongMethodTypeException;
37
38
import static java.lang.invoke.MethodType.methodType;
39
40
public class InvokeWithArgumentsTest {
41
static final MethodHandles.Lookup L = MethodHandles.lookup();
42
43
static Object[] arity(Object o1, Object o2, Object... a) {
44
return a;
45
}
46
47
@Test
48
public void testArity() throws Throwable {
49
MethodHandle mh = L.findStatic(L.lookupClass(), "arity",
50
methodType(Object[].class, Object.class, Object.class, Object[].class));
51
52
try {
53
mh.invokeWithArguments("");
54
Assert.fail("WrongMethodTypeException expected");
55
} catch (WrongMethodTypeException e) {}
56
}
57
58
static Object[] passThrough(String... a) {
59
return a;
60
}
61
62
static Object[] pack(Object o, Object... a) {
63
return a;
64
}
65
66
@Test
67
public void testArrayNoPassThrough() throws Throwable {
68
String[] actual = {"A", "B"};
69
70
MethodHandle mh = L.findStatic(L.lookupClass(), "passThrough",
71
methodType(Object[].class, String[].class));
72
73
// Note: the actual array is not preserved, the elements will be
74
// unpacked and then packed into a new array before invoking the method
75
String[] expected = (String[]) mh.invokeWithArguments(actual);
76
77
Assert.assertTrue(actual != expected, "Array should not pass through");
78
Assert.assertEquals(actual, expected, "Array contents should be equal");
79
}
80
81
@Test
82
public void testArrayPack() throws Throwable {
83
String[] actual = new String[]{"A", "B"};
84
85
MethodHandle mh = L.findStatic(L.lookupClass(), "pack",
86
methodType(Object[].class, Object.class, Object[].class));
87
88
// Note: since String[] can be cast to Object, the actual String[] array
89
// will cast to Object become the single element of a new Object[] array
90
Object[] expected = (Object[]) mh.invokeWithArguments("", actual);
91
92
Assert.assertEquals(1, expected.length, "Array should contain just one element");
93
Assert.assertTrue(actual == expected[0], "Array should pass through");
94
}
95
96
static void intArray(int... a) {
97
}
98
99
@Test
100
public void testPrimitiveArrayWithNull() throws Throwable {
101
MethodHandle mh = L.findStatic(L.lookupClass(), "intArray",
102
methodType(void.class, int[].class));
103
try {
104
mh.invokeWithArguments(null, null);
105
Assert.fail("NullPointerException expected");
106
} catch (NullPointerException e) {}
107
}
108
109
@Test
110
public void testPrimitiveArrayWithRef() throws Throwable {
111
MethodHandle mh = L.findStatic(L.lookupClass(), "intArray",
112
methodType(void.class, int[].class));
113
try {
114
mh.invokeWithArguments("A", "B");
115
Assert.fail("ClassCastException expected");
116
} catch (ClassCastException e) {}
117
}
118
119
120
static void numberArray(Number... a) {
121
}
122
123
@Test
124
public void testRefArrayWithCast() throws Throwable {
125
MethodHandle mh = L.findStatic(L.lookupClass(), "numberArray",
126
methodType(void.class, Number[].class));
127
// All numbers, should not throw
128
mh.invokeWithArguments(1, 1.0, 1.0F, 1L);
129
130
try {
131
mh.invokeWithArguments("A");
132
Assert.fail("ClassCastException expected");
133
} catch (ClassCastException e) {}
134
}
135
}
136
137