10081
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/* @test
|
|
27 |
* @summary unit tests for method handles which permute their arguments
|
|
28 |
* @run junit test.java.lang.invoke.ThrowExceptionsTest
|
|
29 |
*/
|
|
30 |
|
|
31 |
package test.java.lang.invoke;
|
|
32 |
|
|
33 |
import org.junit.*;
|
|
34 |
|
|
35 |
import java.util.*;
|
|
36 |
import java.lang.reflect.*;
|
|
37 |
|
|
38 |
import java.lang.invoke.*;
|
|
39 |
import static java.lang.invoke.MethodHandles.*;
|
|
40 |
import static java.lang.invoke.MethodType.*;
|
|
41 |
|
|
42 |
public class ThrowExceptionsTest {
|
|
43 |
private static final Class CLASS = ThrowExceptionsTest.class;
|
|
44 |
private static final Lookup LOOKUP = lookup();
|
|
45 |
|
|
46 |
public static void main(String argv[]) throws Throwable {
|
|
47 |
new ThrowExceptionsTest().testAll((argv.length == 0 ? null : Arrays.asList(argv).toString()));
|
|
48 |
}
|
|
49 |
|
|
50 |
@Test
|
|
51 |
public void testWMT() throws Throwable {
|
|
52 |
// mostly call testWMTCallee, but sometimes call its void-returning variant
|
|
53 |
MethodHandle mh = testWMTCallee();
|
|
54 |
MethodHandle mh1 = mh.asType(mh.type().changeReturnType(void.class));
|
|
55 |
assert(mh1 != mh);
|
|
56 |
testWMT(mh, mh1, 1000);
|
|
57 |
}
|
|
58 |
|
|
59 |
@Test
|
|
60 |
public void testBoundWMT() throws Throwable {
|
|
61 |
// mostly call exactInvoker.bindTo(testWMTCallee), but sometimes call its void-returning variant
|
|
62 |
MethodHandle callee = testWMTCallee();
|
|
63 |
MethodHandle callee1 = callee.asType(callee.type().changeReturnType(void.class));
|
|
64 |
MethodHandle invoker = exactInvoker(callee.type());
|
|
65 |
MethodHandle mh = invoker.bindTo(callee);
|
|
66 |
MethodHandle mh1 = invoker.bindTo(callee1);
|
|
67 |
testWMT(mh, mh1, 1000);
|
|
68 |
}
|
|
69 |
|
|
70 |
@Test
|
|
71 |
public void testFoldWMT() throws Throwable {
|
|
72 |
// mostly call exactInvoker.fold(constant(testWMTCallee)), but sometimes call its void-returning variant
|
|
73 |
MethodHandle callee = testWMTCallee();
|
|
74 |
MethodHandle callee1 = callee.asType(callee.type().changeReturnType(void.class));
|
|
75 |
MethodHandle invoker = exactInvoker(callee.type());
|
|
76 |
MethodHandle mh = foldArguments(invoker, constant(MethodHandle.class, callee));
|
|
77 |
MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
|
|
78 |
testWMT(mh, mh1, 1000);
|
|
79 |
}
|
|
80 |
|
|
81 |
@Test
|
|
82 |
public void testFoldCCE() throws Throwable {
|
|
83 |
MethodHandle callee = testWMTCallee();
|
|
84 |
MethodHandle callee1 = callee.asType(callee.type().changeParameterType(1, Number.class)).asType(callee.type());
|
|
85 |
MethodHandle invoker = exactInvoker(callee.type());
|
|
86 |
MethodHandle mh = foldArguments(invoker, constant(MethodHandle.class, callee));
|
|
87 |
MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
|
|
88 |
testWMT(mh, mh1, 1000);
|
|
89 |
}
|
|
90 |
|
|
91 |
@Test
|
|
92 |
public void testStackOverflow() throws Throwable {
|
|
93 |
MethodHandle callee = testWMTCallee();
|
|
94 |
MethodHandle callee1 = makeStackOverflow().asType(callee.type());
|
|
95 |
MethodHandle invoker = exactInvoker(callee.type());
|
|
96 |
MethodHandle mh = foldArguments(invoker, constant(MethodHandle.class, callee));
|
|
97 |
MethodHandle mh1 = foldArguments(invoker, constant(MethodHandle.class, callee1));
|
|
98 |
for (int i = 0; i < REPEAT; i++) {
|
|
99 |
try {
|
|
100 |
testWMT(mh, mh1, 1000);
|
|
101 |
} catch (StackOverflowError ex) {
|
|
102 |
// OK, try again
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
private static MethodHandle makeStackOverflow() {
|
|
108 |
MethodType cellType = methodType(void.class);
|
|
109 |
MethodHandle[] cell = { null }; // recursion point
|
|
110 |
MethodHandle getCell = insertArguments(arrayElementGetter(cell.getClass()), 0, cell, 0);
|
|
111 |
MethodHandle invokeCell = foldArguments(exactInvoker(cellType), getCell);
|
|
112 |
assert(invokeCell.type() == cellType);
|
|
113 |
cell[0] = invokeCell;
|
|
114 |
// make it conformable to any type:
|
|
115 |
invokeCell = dropArguments(invokeCell, 0, Object[].class).asVarargsCollector(Object[].class);
|
|
116 |
return invokeCell;
|
|
117 |
}
|
|
118 |
|
|
119 |
static int testCases;
|
|
120 |
|
|
121 |
private void testAll(String match) throws Throwable {
|
|
122 |
testCases = 0;
|
|
123 |
Lookup lookup = lookup();
|
|
124 |
for (Method m : CLASS.getDeclaredMethods()) {
|
|
125 |
String name = m.getName();
|
|
126 |
if (name.startsWith("test") &&
|
|
127 |
(match == null || match.contains(name.substring("test".length()))) &&
|
|
128 |
m.getParameterTypes().length == 0 &&
|
|
129 |
Modifier.isPublic(m.getModifiers()) &&
|
|
130 |
!Modifier.isStatic(m.getModifiers())) {
|
|
131 |
System.out.println("["+name+"]");
|
|
132 |
int tc = testCases;
|
|
133 |
try {
|
|
134 |
m.invoke(this);
|
|
135 |
} catch (Throwable ex) {
|
|
136 |
System.out.println("*** "+ex);
|
|
137 |
ex.printStackTrace();
|
|
138 |
}
|
|
139 |
if (testCases == tc) testCases++;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
if (testCases == 0) throw new RuntimeException("no test cases found");
|
|
143 |
System.out.println("ran a total of "+testCases+" test cases");
|
|
144 |
}
|
|
145 |
|
|
146 |
private static MethodHandle findStatic(String name) {
|
|
147 |
return findMethod(name, true);
|
|
148 |
}
|
|
149 |
private static MethodHandle findVirtual(String name) {
|
|
150 |
return findMethod(name, false);
|
|
151 |
}
|
|
152 |
private static MethodHandle findMethod(String name, boolean isStatic) {
|
|
153 |
MethodHandle mh = null;
|
|
154 |
for (Method m : CLASS.getDeclaredMethods()) {
|
|
155 |
if (m.getName().equals(name) &&
|
|
156 |
Modifier.isStatic(m.getModifiers()) == isStatic) {
|
|
157 |
if (mh != null)
|
|
158 |
throw new RuntimeException("duplicate methods: "+name);
|
|
159 |
try {
|
|
160 |
mh = LOOKUP.unreflect(m);
|
|
161 |
} catch (ReflectiveOperationException ex) {
|
|
162 |
throw new RuntimeException(ex);
|
|
163 |
}
|
|
164 |
}
|
|
165 |
}
|
|
166 |
if (mh == null)
|
|
167 |
throw new RuntimeException("no method: "+name);
|
|
168 |
return mh;
|
|
169 |
}
|
|
170 |
|
|
171 |
int testWMTCallee;
|
|
172 |
private int testWMTCallee(String x) {
|
|
173 |
return testWMTCallee++;
|
|
174 |
}
|
|
175 |
private static MethodHandle testWMTCallee() {
|
|
176 |
MethodHandle callee = findVirtual("testWMTCallee");
|
|
177 |
// FIXME: should not have to retype callee
|
|
178 |
callee = callee.asType(callee.type().changeParameterType(0, Object.class));
|
|
179 |
return callee;
|
|
180 |
}
|
|
181 |
|
|
182 |
private Exception testWMT(MethodHandle[] mhs, int reps) throws Throwable {
|
|
183 |
testCases += 1;
|
|
184 |
testWMTCallee = 0;
|
|
185 |
int catches = 0;
|
|
186 |
Exception savedEx = null;
|
|
187 |
for (int i = 0; i < reps; i++) {
|
|
188 |
MethodHandle mh = mhs[i % mhs.length];
|
|
189 |
int n;
|
|
190 |
try {
|
|
191 |
// FIXME: should not have to retype this
|
|
192 |
n = (int) mh.invokeExact((Object)this, "x");
|
|
193 |
assertEquals(n, i - catches);
|
|
194 |
// Using the exact type for this causes endless deopt due to
|
|
195 |
// 'non_cached_result' in SystemDictionary::find_method_handle_invoke.
|
|
196 |
// The problem is that the compiler thread needs to access a cached
|
|
197 |
// invoke method, but invoke methods are not cached if one of the
|
|
198 |
// component types is not on the BCP.
|
|
199 |
} catch (Exception ex) {
|
|
200 |
savedEx = ex;
|
|
201 |
catches++;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
//VERBOSE: System.out.println("reps="+reps+" catches="+catches);
|
|
205 |
return savedEx;
|
|
206 |
}
|
|
207 |
|
|
208 |
private static final int REPEAT = Integer.getInteger(CLASS.getSimpleName()+".REPEAT", 10);
|
|
209 |
|
|
210 |
private Exception testWMT(MethodHandle mh, MethodHandle mh1, int reps) throws Throwable {
|
|
211 |
//VERBOSE: System.out.println("mh="+mh+" mh1="+mh1);
|
|
212 |
MethodHandle[] mhs = new MethodHandle[100];
|
|
213 |
Arrays.fill(mhs, mh);
|
|
214 |
int patch = mhs.length-1;
|
|
215 |
Exception savedEx = null;
|
|
216 |
for (int i = 0; i < REPEAT; i++) {
|
|
217 |
mhs[patch] = mh;
|
|
218 |
testWMT(mhs, 10000);
|
|
219 |
mhs[patch] = mh1;
|
|
220 |
savedEx = testWMT(mhs, reps);
|
|
221 |
}
|
|
222 |
return savedEx;
|
|
223 |
}
|
|
224 |
|
|
225 |
private static void assertEquals(Object x, Object y) {
|
|
226 |
if (x == y || x != null && x.equals(y)) return;
|
|
227 |
throw new RuntimeException(x+" != "+y);
|
|
228 |
}
|
|
229 |
}
|