author | amurillo |
Wed, 15 Aug 2012 16:49:38 -0700 | |
changeset 13465 | d3fc5d192448 |
parent 13423 | 17843fff200d |
child 13610 | 28122b96858e |
permissions | -rw-r--r-- |
9646 | 1 |
/* |
2 |
* Copyright (c) 2009, 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. |
|
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 test.sun.invoke.util; |
|
25 |
||
26 |
import sun.invoke.util.ValueConversions; |
|
27 |
import sun.invoke.util.Wrapper; |
|
28 |
import java.lang.invoke.MethodType; |
|
29 |
import java.lang.invoke.MethodHandle; |
|
30 |
import java.io.Serializable; |
|
31 |
import java.util.Arrays; |
|
32 |
import java.util.Collections; |
|
33 |
import org.junit.Test; |
|
34 |
import static org.junit.Assert.*; |
|
35 |
||
36 |
/* @test |
|
37 |
* @summary unit tests for value-type conversion utilities |
|
38 |
* @ignore This test requires a special compilation environment to access sun.inovke.util. Run by hand. |
|
39 |
* @run junit/othervm test.sun.invoke.util.ValueConversionsTest |
|
40 |
* @run junit/othervm |
|
41 |
* -DValueConversionsTest.MAX_ARITY=255 -DValueConversionsTest.START_ARITY=250 |
|
42 |
* test.sun.invoke.util.ValueConversionsTest |
|
43 |
*/ |
|
44 |
||
45 |
// This might take a while and burn lots of metadata: |
|
46 |
// @run junit/othervm -DValueConversionsTest.MAX_ARITY=255 -DValueConversionsTest.EXHAUSTIVE=true test.sun.invoke.util.ValueConversionsTest |
|
47 |
||
48 |
/** |
|
49 |
* |
|
50 |
* @author jrose |
|
51 |
*/ |
|
52 |
public class ValueConversionsTest { |
|
11534
9949ffb8eb3a
7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents:
9646
diff
changeset
|
53 |
private static final Class<?> CLASS = ValueConversionsTest.class; |
9646 | 54 |
private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40); |
55 |
private static final int START_ARITY = Integer.getInteger(CLASS.getSimpleName()+".START_ARITY", 0); |
|
56 |
private static final boolean EXHAUSTIVE = Boolean.getBoolean(CLASS.getSimpleName()+".EXHAUSTIVE"); |
|
57 |
||
58 |
@Test |
|
59 |
public void testUnbox() throws Throwable { |
|
60 |
testUnbox(false); |
|
61 |
} |
|
62 |
||
63 |
@Test |
|
64 |
public void testUnboxCast() throws Throwable { |
|
65 |
testUnbox(true); |
|
66 |
} |
|
67 |
||
68 |
private void testUnbox(boolean doCast) throws Throwable { |
|
69 |
//System.out.println("unbox"); |
|
70 |
for (Wrapper dst : Wrapper.values()) { |
|
71 |
//System.out.println(dst); |
|
72 |
for (Wrapper src : Wrapper.values()) { |
|
73 |
testUnbox(doCast, dst, src); |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable { |
|
79 |
boolean expectThrow = !doCast && !dst.isConvertibleFrom(src); |
|
80 |
if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT) return; // must have prims |
|
81 |
if (dst == Wrapper.OBJECT) |
|
82 |
expectThrow = false; // everything (even VOID==null here) converts to OBJECT |
|
83 |
try { |
|
84 |
for (int n = -5; n < 10; n++) { |
|
85 |
Object box = src.wrap(n); |
|
86 |
switch (src) { |
|
87 |
case VOID: assertEquals(box, null); break; |
|
88 |
case OBJECT: box = box.toString(); break; |
|
89 |
case SHORT: assertEquals(box.getClass(), Short.class); break; |
|
90 |
default: assertEquals(box.getClass(), src.wrapperType()); break; |
|
91 |
} |
|
92 |
MethodHandle unboxer; |
|
93 |
if (doCast) |
|
94 |
unboxer = ValueConversions.unboxCast(dst.primitiveType()); |
|
95 |
else |
|
96 |
unboxer = ValueConversions.unbox(dst.primitiveType()); |
|
97 |
Object expResult = (box == null) ? dst.zero() : dst.wrap(box); |
|
98 |
Object result = null; |
|
99 |
switch (dst) { |
|
100 |
case INT: result = (int) unboxer.invokeExact(box); break; |
|
101 |
case LONG: result = (long) unboxer.invokeExact(box); break; |
|
102 |
case FLOAT: result = (float) unboxer.invokeExact(box); break; |
|
103 |
case DOUBLE: result = (double) unboxer.invokeExact(box); break; |
|
104 |
case CHAR: result = (char) unboxer.invokeExact(box); break; |
|
105 |
case BYTE: result = (byte) unboxer.invokeExact(box); break; |
|
106 |
case SHORT: result = (short) unboxer.invokeExact(box); break; |
|
107 |
case OBJECT: result = (Object) unboxer.invokeExact(box); break; |
|
108 |
case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break; |
|
109 |
case VOID: result = null; unboxer.invokeExact(box); break; |
|
110 |
} |
|
111 |
if (expectThrow) { |
|
112 |
expResult = "(need an exception)"; |
|
113 |
} |
|
114 |
assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box), |
|
115 |
expResult, result); |
|
116 |
} |
|
117 |
} catch (RuntimeException ex) { |
|
118 |
if (expectThrow) return; |
|
119 |
System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src)); |
|
120 |
throw ex; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
@Test |
|
125 |
public void testBox() throws Throwable { |
|
126 |
//System.out.println("box"); |
|
127 |
for (Wrapper w : Wrapper.values()) { |
|
128 |
if (w == Wrapper.VOID) continue; // skip this; no unboxed form |
|
129 |
//System.out.println(w); |
|
130 |
for (int n = -5; n < 10; n++) { |
|
131 |
Object box = w.wrap(n); |
|
132 |
MethodHandle boxer = ValueConversions.box(w.primitiveType()); |
|
133 |
Object expResult = box; |
|
134 |
Object result = null; |
|
135 |
switch (w) { |
|
11534
9949ffb8eb3a
7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents:
9646
diff
changeset
|
136 |
case INT: result = boxer.invokeExact(/*int*/n); break; |
9646 | 137 |
case LONG: result = boxer.invokeExact((long)n); break; |
138 |
case FLOAT: result = boxer.invokeExact((float)n); break; |
|
139 |
case DOUBLE: result = boxer.invokeExact((double)n); break; |
|
140 |
case CHAR: result = boxer.invokeExact((char)n); break; |
|
141 |
case BYTE: result = boxer.invokeExact((byte)n); break; |
|
142 |
case SHORT: result = boxer.invokeExact((short)n); break; |
|
143 |
case OBJECT: result = boxer.invokeExact((Object)n); break; |
|
144 |
case BOOLEAN: result = boxer.invokeExact((n & 1) != 0); break; |
|
145 |
} |
|
146 |
assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box), |
|
147 |
expResult, result); |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
@Test |
|
153 |
public void testCast() throws Throwable { |
|
154 |
//System.out.println("cast"); |
|
155 |
Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class }; |
|
156 |
Object[] objects = { new Object(), Boolean.FALSE, "hello", (Long)12L, (Integer)6 }; |
|
157 |
for (Class<?> dst : types) { |
|
158 |
MethodHandle caster = ValueConversions.cast(dst); |
|
159 |
assertEquals(caster.type(), ValueConversions.identity().type()); |
|
160 |
for (Object obj : objects) { |
|
161 |
Class<?> src = obj.getClass(); |
|
162 |
boolean canCast; |
|
163 |
if (dst.isInterface()) { |
|
164 |
canCast = true; |
|
165 |
} else { |
|
166 |
canCast = dst.isAssignableFrom(src); |
|
167 |
assertEquals(canCast, dst.isInstance(obj)); |
|
168 |
} |
|
169 |
//System.out.println("obj="+obj+" <: dst="+dst); |
|
170 |
try { |
|
171 |
Object result = caster.invokeExact(obj); |
|
172 |
if (canCast) |
|
173 |
assertEquals(obj, result); |
|
174 |
else |
|
175 |
assertEquals("cast should not have succeeded", dst, obj); |
|
176 |
} catch (ClassCastException ex) { |
|
177 |
if (canCast) |
|
178 |
throw ex; |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
@Test |
|
185 |
public void testIdentity() throws Throwable { |
|
186 |
//System.out.println("identity"); |
|
187 |
MethodHandle id = ValueConversions.identity(); |
|
188 |
Object expResult = "foo"; |
|
189 |
Object result = id.invokeExact(expResult); |
|
190 |
// compiler bug: ValueConversions.identity().invokeExact("bar"); |
|
191 |
assertEquals(expResult, result); |
|
192 |
} |
|
193 |
||
194 |
@Test |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
195 |
public void testConvert() throws Throwable { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
196 |
//System.out.println("convert"); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
197 |
for (long tval = 0, ctr = 0;;) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
198 |
if (++ctr > 99999) throw new AssertionError("too many test values"); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
199 |
// next test value: |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
200 |
//System.out.println(Long.toHexString(tval)); // prints 3776 test patterns |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
201 |
tval = nextTestValue(tval); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
202 |
if (tval == 0) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
203 |
//System.out.println("test value count = "+ctr); // 3776 = 8*59*8 |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
204 |
break; // repeat |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
205 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
206 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
207 |
for (Wrapper src : Wrapper.values()) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
208 |
for (Wrapper dst : Wrapper.values()) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
209 |
testConvert(src, dst, 0); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
210 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
211 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
212 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
213 |
static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
214 |
//System.out.println(src+" => "+dst); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
215 |
boolean testSingleCase = (tval != 0); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
216 |
final long tvalInit = tval; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
217 |
MethodHandle conv = ValueConversions.convertPrimitive(src, dst); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
218 |
MethodType convType; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
219 |
if (src == Wrapper.VOID) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
220 |
convType = MethodType.methodType(dst.primitiveType() /* , void */); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
221 |
else |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
222 |
convType = MethodType.methodType(dst.primitiveType(), src.primitiveType()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
223 |
assertEquals(convType, conv.type()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
224 |
MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class)); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
225 |
for (;;) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
226 |
long n = tval; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
227 |
Object testValue = src.wrap(n); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
228 |
Object expResult = dst.cast(testValue, dst.primitiveType()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
229 |
Object result; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
230 |
switch (src) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
231 |
case INT: result = converter.invokeExact((int)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
232 |
case LONG: result = converter.invokeExact(/*long*/n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
233 |
case FLOAT: result = converter.invokeExact((float)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
234 |
case DOUBLE: result = converter.invokeExact((double)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
235 |
case CHAR: result = converter.invokeExact((char)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
236 |
case BYTE: result = converter.invokeExact((byte)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
237 |
case SHORT: result = converter.invokeExact((short)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
238 |
case OBJECT: result = converter.invokeExact((Object)n); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
239 |
case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
240 |
case VOID: result = converter.invokeExact(); break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
241 |
default: throw new AssertionError(); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
242 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
243 |
assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue), |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
244 |
expResult, result); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
245 |
if (testSingleCase) break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
246 |
// next test value: |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
247 |
tval = nextTestValue(tval); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
248 |
if (tval == tvalInit) break; // repeat |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
249 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
250 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
251 |
static long tweakSign(long x) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
252 |
// Assuming that x is mostly zeroes, make those zeroes follow bit #62 (just below the sign). |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
253 |
// This function is self-inverse. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
254 |
final long MID_SIGN_BIT = 62; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
255 |
long sign = -((x >>> MID_SIGN_BIT) & 1); // all ones or all zeroes |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
256 |
long flip = (sign >>> -MID_SIGN_BIT); // apply the sign below the mid-bit |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
257 |
return x ^ flip; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
258 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
259 |
static long nextTestValue(long x) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
260 |
// Produce 64 bits with three component bitfields: [ high:3 | mid:58 | low:3 ]. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
261 |
// The high and low fields vary through all possible bit patterns. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
262 |
// The middle field is either all zero or has a single bit set. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
263 |
// For better coverage of the neighborhood of zero, an internal sign bit is xored downward also. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
264 |
long ux = tweakSign(x); // unsign the middle field |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
265 |
final long LOW_BITS = 3, LOW_BITS_MASK = (1L << LOW_BITS)-1; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
266 |
final long HIGH_BITS = 3, HIGH_BITS_MASK = ~(-1L >>> HIGH_BITS); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
267 |
if ((ux & LOW_BITS_MASK) != LOW_BITS_MASK) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
268 |
++ux; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
269 |
} else { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
270 |
ux &= ~LOW_BITS_MASK; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
271 |
long midBit = (ux & ~HIGH_BITS_MASK); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
272 |
if (midBit == 0) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
273 |
midBit = (1L<<LOW_BITS); // introduce a low bit |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
274 |
ux += midBit; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
275 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
276 |
return tweakSign(ux); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
277 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
278 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
279 |
@Test |
9646 | 280 |
public void testVarargsArray() throws Throwable { |
281 |
//System.out.println("varargsArray"); |
|
282 |
final int MIN = START_ARITY; |
|
283 |
final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added |
|
284 |
for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) { |
|
285 |
MethodHandle target = ValueConversions.varargsArray(nargs); |
|
286 |
Object[] args = new Object[nargs]; |
|
287 |
for (int i = 0; i < nargs; i++) |
|
288 |
args[i] = "#"+i; |
|
289 |
Object res = target.invokeWithArguments(args); |
|
290 |
assertArrayEquals(args, (Object[])res); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
@Test |
|
295 |
public void testVarargsReferenceArray() throws Throwable { |
|
296 |
//System.out.println("varargsReferenceArray"); |
|
297 |
testTypedVarargsArray(Object[].class); |
|
298 |
testTypedVarargsArray(String[].class); |
|
299 |
testTypedVarargsArray(Number[].class); |
|
300 |
} |
|
301 |
||
302 |
@Test |
|
303 |
public void testVarargsPrimitiveArray() throws Throwable { |
|
304 |
//System.out.println("varargsPrimitiveArray"); |
|
305 |
testTypedVarargsArray(int[].class); |
|
306 |
testTypedVarargsArray(long[].class); |
|
307 |
testTypedVarargsArray(byte[].class); |
|
308 |
testTypedVarargsArray(boolean[].class); |
|
309 |
testTypedVarargsArray(short[].class); |
|
310 |
testTypedVarargsArray(char[].class); |
|
311 |
testTypedVarargsArray(float[].class); |
|
312 |
testTypedVarargsArray(double[].class); |
|
313 |
} |
|
314 |
||
315 |
private static int nextArgCount(int nargs, int density, int MAX) { |
|
316 |
if (EXHAUSTIVE) return nargs + 1; |
|
317 |
if (nargs >= MAX) return Integer.MAX_VALUE; |
|
318 |
int BOT = 20, TOP = MAX-5; |
|
319 |
if (density < 10) { BOT = 10; MAX = TOP-2; } |
|
320 |
if (nargs <= BOT || nargs >= TOP) { |
|
321 |
++nargs; |
|
322 |
} else { |
|
323 |
int bump = Math.max(1, 100 / density); |
|
324 |
nargs += bump; |
|
325 |
if (nargs > TOP) nargs = TOP; |
|
326 |
} |
|
327 |
return nargs; |
|
328 |
} |
|
329 |
||
330 |
private void testTypedVarargsArray(Class<?> arrayType) throws Throwable { |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11534
diff
changeset
|
331 |
//System.out.println(arrayType.getSimpleName()); |
9646 | 332 |
Class<?> elemType = arrayType.getComponentType(); |
333 |
int MIN = START_ARITY; |
|
334 |
int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added |
|
335 |
int density = 3; |
|
336 |
if (elemType == int.class || elemType == long.class) density = 7; |
|
337 |
if (elemType == long.class || elemType == double.class) { MAX /= 2; MIN /= 2; } |
|
338 |
for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, density, MAX)) { |
|
339 |
Object[] args = makeTestArray(elemType, nargs); |
|
340 |
MethodHandle varargsArray = ValueConversions.varargsArray(arrayType, nargs); |
|
341 |
MethodType vaType = varargsArray.type(); |
|
342 |
assertEquals(arrayType, vaType.returnType()); |
|
343 |
if (nargs != 0) { |
|
344 |
assertEquals(elemType, vaType.parameterType(0)); |
|
345 |
assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1)); |
|
346 |
} |
|
347 |
assertEquals(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)), |
|
348 |
vaType); |
|
349 |
Object res = varargsArray.invokeWithArguments(args); |
|
350 |
String resString = toArrayString(res); |
|
351 |
assertEquals(Arrays.toString(args), resString); |
|
352 |
||
353 |
MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs); |
|
354 |
MethodType stype = spreader.type(); |
|
355 |
assert(stype == MethodType.methodType(arrayType, arrayType)); |
|
356 |
if (nargs <= 5) { |
|
357 |
// invoke target as a spreader also: |
|
11534
9949ffb8eb3a
7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents:
9646
diff
changeset
|
358 |
@SuppressWarnings("cast") |
9646 | 359 |
Object res2 = spreader.invokeWithArguments((Object)res); |
360 |
String res2String = toArrayString(res2); |
|
361 |
assertEquals(Arrays.toString(args), res2String); |
|
362 |
// invoke the spreader on a generic Object[] array; check for error |
|
363 |
try { |
|
364 |
Object res3 = spreader.invokeWithArguments((Object)args); |
|
365 |
String res3String = toArrayString(res3); |
|
366 |
assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class)); |
|
367 |
assertEquals(Arrays.toString(args), res3String); |
|
368 |
} catch (ClassCastException ex) { |
|
369 |
assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class)); |
|
370 |
} |
|
371 |
} |
|
372 |
if (nargs == 0) { |
|
373 |
// invoke spreader on null arglist |
|
374 |
Object res3 = spreader.invokeWithArguments((Object)null); |
|
375 |
String res3String = toArrayString(res3); |
|
376 |
assertEquals(Arrays.toString(args), res3String); |
|
377 |
} |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
private static Object[] makeTestArray(Class<?> elemType, int len) { |
|
382 |
Wrapper elem = null; |
|
383 |
if (elemType.isPrimitive()) |
|
384 |
elem = Wrapper.forPrimitiveType(elemType); |
|
385 |
else if (Wrapper.isWrapperType(elemType)) |
|
386 |
elem = Wrapper.forWrapperType(elemType); |
|
387 |
Object[] args = new Object[len]; |
|
388 |
for (int i = 0; i < len; i++) { |
|
389 |
Object arg = i * 100; |
|
390 |
if (elem == null) { |
|
391 |
if (elemType == String.class) |
|
392 |
arg = "#"+arg; |
|
393 |
arg = elemType.cast(arg); // just to make sure |
|
394 |
} else { |
|
395 |
switch (elem) { |
|
396 |
case BOOLEAN: arg = (i % 3 == 0); break; |
|
397 |
case CHAR: arg = 'a' + i; break; |
|
398 |
case LONG: arg = (long)i * 1000_000_000; break; |
|
399 |
case FLOAT: arg = (float)i / 100; break; |
|
400 |
case DOUBLE: arg = (double)i / 1000_000; break; |
|
401 |
} |
|
402 |
arg = elem.cast(arg, elemType); |
|
403 |
} |
|
404 |
args[i] = arg; |
|
405 |
} |
|
406 |
//System.out.println(elemType.getName()+Arrays.toString(args)); |
|
407 |
return args; |
|
408 |
} |
|
409 |
||
410 |
private static String toArrayString(Object a) { |
|
411 |
if (a == null) return "null"; |
|
412 |
Class<?> elemType = a.getClass().getComponentType(); |
|
413 |
if (elemType == null) return a.toString(); |
|
414 |
if (elemType.isPrimitive()) { |
|
415 |
switch (Wrapper.forPrimitiveType(elemType)) { |
|
416 |
case INT: return Arrays.toString((int[])a); |
|
417 |
case BYTE: return Arrays.toString((byte[])a); |
|
418 |
case BOOLEAN: return Arrays.toString((boolean[])a); |
|
419 |
case SHORT: return Arrays.toString((short[])a); |
|
420 |
case CHAR: return Arrays.toString((char[])a); |
|
421 |
case FLOAT: return Arrays.toString((float[])a); |
|
422 |
case LONG: return Arrays.toString((long[])a); |
|
423 |
case DOUBLE: return Arrays.toString((double[])a); |
|
424 |
} |
|
425 |
} |
|
426 |
return Arrays.toString((Object[])a); |
|
427 |
} |
|
428 |
||
429 |
@Test |
|
430 |
public void testVarargsList() throws Throwable { |
|
431 |
//System.out.println("varargsList"); |
|
432 |
final int MIN = START_ARITY; |
|
433 |
final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added |
|
434 |
for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 7, MAX)) { |
|
435 |
MethodHandle target = ValueConversions.varargsList(nargs); |
|
436 |
Object[] args = new Object[nargs]; |
|
437 |
for (int i = 0; i < nargs; i++) |
|
438 |
args[i] = "#"+i; |
|
439 |
Object res = target.invokeWithArguments(args); |
|
440 |
assertEquals(Arrays.asList(args), res); |
|
441 |
} |
|
442 |
} |
|
443 |
} |