author | jrose |
Thu, 12 May 2011 19:27:33 -0700 | |
changeset 9645 | dabb5e4edc4c |
parent 9033 | a88f5656f05d |
child 9646 | 5ebbe5ab084f |
permissions | -rw-r--r-- |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
1 |
/* |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
2 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
4 |
* |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
10 |
* |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
15 |
* accompanied this code). |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
16 |
* |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
20 |
* |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
23 |
* questions. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
24 |
*/ |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
25 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
26 |
/* @test |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
27 |
* @summary unit tests for java.lang.invoke.MethodHandle.invoke |
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
28 |
* @compile -target 7 InvokeGenericTest.java |
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
29 |
* @run junit/othervm test.java.lang.invoke.InvokeGenericTest |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
30 |
*/ |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
31 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
32 |
package test.java.lang.invoke; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
33 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
34 |
import java.lang.invoke.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
35 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
36 |
import static java.lang.invoke.MethodType.*; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
37 |
import java.lang.reflect.*; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
38 |
import java.util.*; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
39 |
import org.junit.*; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
40 |
import static org.junit.Assert.*; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
41 |
import static org.junit.Assume.*; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
42 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
43 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
44 |
/** |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
45 |
* |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
46 |
* @author jrose |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
47 |
*/ |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
48 |
public class InvokeGenericTest { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
49 |
// How much output? |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
50 |
static int verbosity = 0; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
51 |
static { |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
52 |
String vstr = System.getProperty("test.java.lang.invoke.InvokeGenericTest.verbosity"); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
53 |
if (vstr != null) verbosity = Integer.parseInt(vstr); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
54 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
55 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
56 |
@Test |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
57 |
public void testFirst() throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
58 |
verbosity += 9; try { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
59 |
// left blank for debugging |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
60 |
} finally { printCounts(); verbosity -= 9; } |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
61 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
62 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
63 |
public InvokeGenericTest() { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
64 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
65 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
66 |
@Before |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
67 |
public void checkImplementedPlatform() { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
68 |
boolean platformOK = false; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
69 |
Properties properties = System.getProperties(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
70 |
String vers = properties.getProperty("java.vm.version"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
71 |
String name = properties.getProperty("java.vm.name"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
72 |
String arch = properties.getProperty("os.arch"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
73 |
if ((arch.equals("amd64") || arch.equals("i386") || arch.equals("x86") || |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
74 |
arch.equals("sparc") || arch.equals("sparcv9")) && |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
75 |
(name.contains("Client") || name.contains("Server")) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
76 |
) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
77 |
platformOK = true; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
78 |
} else { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
79 |
System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
80 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
81 |
assumeTrue(platformOK); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
82 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
83 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
84 |
String testName; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
85 |
static int allPosTests, allNegTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
86 |
int posTests, negTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
87 |
@After |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
88 |
public void printCounts() { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
89 |
if (verbosity >= 2 && (posTests | negTests) != 0) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
90 |
System.out.println(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
91 |
if (posTests != 0) System.out.println("=== "+testName+": "+posTests+" positive test cases run"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
92 |
if (negTests != 0) System.out.println("=== "+testName+": "+negTests+" negative test cases run"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
93 |
allPosTests += posTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
94 |
allNegTests += negTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
95 |
posTests = negTests = 0; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
96 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
97 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
98 |
void countTest(boolean positive) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
99 |
if (positive) ++posTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
100 |
else ++negTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
101 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
102 |
void countTest() { countTest(true); } |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
103 |
void startTest(String name) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
104 |
if (testName != null) printCounts(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
105 |
if (verbosity >= 1) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
106 |
System.out.println(name); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
107 |
posTests = negTests = 0; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
108 |
testName = name; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
109 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
110 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
111 |
@BeforeClass |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
112 |
public static void setUpClass() throws Exception { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
113 |
calledLog.clear(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
114 |
calledLog.add(null); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
115 |
nextArgVal = INITIAL_ARG_VAL; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
116 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
117 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
118 |
@AfterClass |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
119 |
public static void tearDownClass() throws Exception { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
120 |
int posTests = allPosTests, negTests = allNegTests; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
121 |
if (verbosity >= 2 && (posTests | negTests) != 0) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
122 |
System.out.println(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
123 |
if (posTests != 0) System.out.println("=== "+posTests+" total positive test cases"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
124 |
if (negTests != 0) System.out.println("=== "+negTests+" total negative test cases"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
125 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
126 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
127 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
128 |
static List<Object> calledLog = new ArrayList<Object>(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
129 |
static Object logEntry(String name, Object... args) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
130 |
return Arrays.asList(name, Arrays.asList(args)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
131 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
132 |
static Object called(String name, Object... args) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
133 |
Object entry = logEntry(name, args); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
134 |
calledLog.add(entry); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
135 |
return entry; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
136 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
137 |
static void assertCalled(String name, Object... args) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
138 |
Object expected = logEntry(name, args); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
139 |
Object actual = calledLog.get(calledLog.size() - 1); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
140 |
if (expected.equals(actual) && verbosity < 9) return; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
141 |
System.out.println("assertCalled "+name+":"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
142 |
System.out.println("expected: "+expected); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
143 |
System.out.println("actual: "+actual); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
144 |
System.out.println("ex. types: "+getClasses(expected)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
145 |
System.out.println("act. types: "+getClasses(actual)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
146 |
assertEquals("previous method call", expected, actual); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
147 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
148 |
static void printCalled(MethodHandle target, String name, Object... args) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
149 |
if (verbosity >= 3) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
150 |
System.out.println("calling MH="+target+" to "+name+Arrays.toString(args)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
151 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
152 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
153 |
static Object castToWrapper(Object value, Class<?> dst) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
154 |
Object wrap = null; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
155 |
if (value instanceof Number) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
156 |
wrap = castToWrapperOrNull(((Number)value).longValue(), dst); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
157 |
if (value instanceof Character) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
158 |
wrap = castToWrapperOrNull((char)(Character)value, dst); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
159 |
if (wrap != null) return wrap; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
160 |
return dst.cast(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
161 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
162 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
163 |
static Object castToWrapperOrNull(long value, Class<?> dst) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
164 |
if (dst == int.class || dst == Integer.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
165 |
return (int)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
166 |
if (dst == long.class || dst == Long.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
167 |
return (long)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
168 |
if (dst == char.class || dst == Character.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
169 |
return (char)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
170 |
if (dst == short.class || dst == Short.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
171 |
return (short)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
172 |
if (dst == float.class || dst == Float.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
173 |
return (float)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
174 |
if (dst == double.class || dst == Double.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
175 |
return (double)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
176 |
if (dst == byte.class || dst == Byte.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
177 |
return (byte)(value); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
178 |
if (dst == boolean.class || dst == boolean.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
179 |
return ((value % 29) & 1) == 0; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
180 |
return null; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
181 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
182 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
183 |
static final int ONE_MILLION = (1000*1000), // first int value |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
184 |
TEN_BILLION = (10*1000*1000*1000), // scale factor to reach upper 32 bits |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
185 |
INITIAL_ARG_VAL = ONE_MILLION << 1; // <<1 makes space for sign bit; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
186 |
static long nextArgVal; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
187 |
static long nextArg(boolean moreBits) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
188 |
long val = nextArgVal++; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
189 |
long sign = -(val & 1); // alternate signs |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
190 |
val >>= 1; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
191 |
if (moreBits) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
192 |
// Guarantee some bits in the high word. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
193 |
// In any case keep the decimal representation simple-looking, |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
194 |
// with lots of zeroes, so as not to make the printed decimal |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
195 |
// strings unnecessarily noisy. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
196 |
val += (val % ONE_MILLION) * TEN_BILLION; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
197 |
return val ^ sign; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
198 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
199 |
static int nextArg() { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
200 |
// Produce a 32-bit result something like ONE_MILLION+(smallint). |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
201 |
// Example: 1_000_042. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
202 |
return (int) nextArg(false); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
203 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
204 |
static long nextArg(Class<?> kind) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
205 |
if (kind == long.class || kind == Long.class || |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
206 |
kind == double.class || kind == Double.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
207 |
// produce a 64-bit result something like |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
208 |
// ((TEN_BILLION+1) * (ONE_MILLION+(smallint))) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
209 |
// Example: 10_000_420_001_000_042. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
210 |
return nextArg(true); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
211 |
return (long) nextArg(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
212 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
213 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
214 |
static Object randomArg(Class<?> param) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
215 |
Object wrap = castToWrapperOrNull(nextArg(param), param); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
216 |
if (wrap != null) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
217 |
return wrap; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
218 |
} |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
219 |
// import sun.invoke.util.Wrapper; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
220 |
// Wrapper wrap = Wrapper.forBasicType(dst); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
221 |
// if (wrap == Wrapper.OBJECT && Wrapper.isWrapperType(dst)) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
222 |
// wrap = Wrapper.forWrapperType(dst); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
223 |
// if (wrap != Wrapper.OBJECT) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
224 |
// return wrap.wrap(nextArg++); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
225 |
if (param.isInterface()) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
226 |
for (Class<?> c : param.getClasses()) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
227 |
if (param.isAssignableFrom(c) && !c.isInterface()) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
228 |
{ param = c; break; } |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
229 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
230 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
231 |
if (param.isInterface() || param.isAssignableFrom(String.class)) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
232 |
return "#"+nextArg(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
233 |
else |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
234 |
try { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
235 |
return param.newInstance(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
236 |
} catch (InstantiationException ex) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
237 |
} catch (IllegalAccessException ex) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
238 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
239 |
return null; // random class not Object, String, Integer, etc. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
240 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
241 |
static Object[] randomArgs(Class<?>... params) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
242 |
Object[] args = new Object[params.length]; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
243 |
for (int i = 0; i < args.length; i++) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
244 |
args[i] = randomArg(params[i]); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
245 |
return args; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
246 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
247 |
static Object[] randomArgs(int nargs, Class<?> param) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
248 |
Object[] args = new Object[nargs]; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
249 |
for (int i = 0; i < args.length; i++) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
250 |
args[i] = randomArg(param); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
251 |
return args; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
252 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
253 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
254 |
static final Object ANON_OBJ = new Object(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
255 |
static Object zeroArg(Class<?> param) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
256 |
Object x = castToWrapperOrNull(0L, param); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
257 |
if (x != null) return x; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
258 |
if (param.isInterface() || param.isAssignableFrom(String.class)) return "\"\""; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
259 |
if (param == Object.class) return ANON_OBJ; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
260 |
if (param.getComponentType() != null) return Array.newInstance(param.getComponentType(), 0); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
261 |
return null; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
262 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
263 |
static Object[] zeroArgs(Class<?>... params) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
264 |
Object[] args = new Object[params.length]; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
265 |
for (int i = 0; i < args.length; i++) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
266 |
args[i] = zeroArg(params[i]); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
267 |
return args; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
268 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
269 |
static Object[] zeroArgs(List<Class<?>> params) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
270 |
return zeroArgs(params.toArray(new Class<?>[0])); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
271 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
272 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
273 |
static <T, E extends T> T[] array(Class<T[]> atype, E... a) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
274 |
return Arrays.copyOf(a, a.length, atype); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
275 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
276 |
static <T> T[] cat(T[] a, T... b) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
277 |
int alen = a.length, blen = b.length; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
278 |
if (blen == 0) return a; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
279 |
T[] c = Arrays.copyOf(a, alen + blen); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
280 |
System.arraycopy(b, 0, c, alen, blen); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
281 |
return c; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
282 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
283 |
static Integer[] boxAll(int... vx) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
284 |
Integer[] res = new Integer[vx.length]; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
285 |
for (int i = 0; i < res.length; i++) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
286 |
res[i] = vx[i]; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
287 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
288 |
return res; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
289 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
290 |
static Object getClasses(Object x) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
291 |
if (x == null) return x; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
292 |
if (x instanceof String) return x; // keep the name |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
293 |
if (x instanceof List) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
294 |
// recursively report classes of the list elements |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
295 |
Object[] xa = ((List)x).toArray(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
296 |
for (int i = 0; i < xa.length; i++) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
297 |
xa[i] = getClasses(xa[i]); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
298 |
return Arrays.asList(xa); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
299 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
300 |
return x.getClass().getSimpleName(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
301 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
302 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
303 |
static MethodHandle changeArgTypes(MethodHandle target, Class<?> argType) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
304 |
return changeArgTypes(target, 0, 999, argType); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
305 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
306 |
static MethodHandle changeArgTypes(MethodHandle target, |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
307 |
int beg, int end, Class<?> argType) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
308 |
MethodType targetType = target.type(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
309 |
end = Math.min(end, targetType.parameterCount()); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
310 |
ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(targetType.parameterList()); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
311 |
Collections.fill(argTypes.subList(beg, end), argType); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
312 |
MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
313 |
return MethodHandles.convertArguments(target, ttype2); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
314 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
315 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
316 |
// This lookup is good for all members in and under InvokeGenericTest. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
317 |
static final Lookup LOOKUP = MethodHandles.lookup(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
318 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
319 |
Map<List<Class<?>>, MethodHandle> CALLABLES = new HashMap<List<Class<?>>, MethodHandle>(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
320 |
MethodHandle callable(List<Class<?>> params) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
321 |
MethodHandle mh = CALLABLES.get(params); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
322 |
if (mh == null) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7556
diff
changeset
|
323 |
mh = collector_MH.asType(methodType(Object.class, params)); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
324 |
CALLABLES.put(params, mh); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
325 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
326 |
return mh; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
327 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
328 |
MethodHandle callable(Class<?>... params) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
329 |
return callable(Arrays.asList(params)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
330 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
331 |
private static Object collector(Object... args) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
332 |
return Arrays.asList(args); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
333 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
334 |
private static final MethodHandle collector_MH; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
335 |
static { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
336 |
try { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
337 |
collector_MH |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
338 |
= LOOKUP.findStatic(LOOKUP.lookupClass(), |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
339 |
"collector", |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
340 |
methodType(Object.class, Object[].class)); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
341 |
} catch (ReflectiveOperationException ex) { |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
342 |
throw new RuntimeException(ex); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
343 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
344 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
345 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
346 |
@Test |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
347 |
public void testSimple() throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
348 |
startTest("testSimple"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
349 |
countTest(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
350 |
String[] args = { "one", "two" }; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
351 |
MethodHandle mh = callable(Object.class, String.class); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
352 |
Object res; List resl; |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
353 |
res = resl = (List) mh.invoke((String)args[0], (Object)args[1]); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
354 |
//System.out.println(res); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
355 |
assertEquals(Arrays.asList(args), res); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
356 |
} |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
357 |
|
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
358 |
@Test |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
359 |
public void testAlternateName() throws Throwable { |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
360 |
startTest("testAlternateName"); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
361 |
countTest(); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
362 |
String[] args = { "one", "two" }; |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
363 |
MethodHandle mh = callable(Object.class, String.class); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
364 |
Object res; List resl; |
7556
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
365 |
res = resl = (List) mh.invokeGeneric((String)args[0], (Object)args[1]); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
366 |
//System.out.println(res); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
367 |
assertEquals(Arrays.asList(args), res); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
368 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
369 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
370 |
@Test |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
371 |
public void testWrongArgumentCount() throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
372 |
startTest("testWrongArgumentCount"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
373 |
for (int i = 0; i <= 10; i++) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
374 |
testWrongArgumentCount(Collections.<Class<?>>nCopies(i, Integer.class)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
375 |
if (i <= 4) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
376 |
testWrongArgumentCount(Collections.<Class<?>>nCopies(i, int.class)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
377 |
testWrongArgumentCount(Collections.<Class<?>>nCopies(i, long.class)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
378 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
379 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
380 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
381 |
public void testWrongArgumentCount(List<Class<?>> params) throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
382 |
int max = params.size(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
383 |
for (int i = 0; i < max; i++) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
384 |
List<Class<?>> params2 = params.subList(0, i); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
385 |
for (int k = 0; k <= 2; k++) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
386 |
if (k == 1) params = methodType(Object.class, params).generic().parameterList(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
387 |
if (k == 2) params2 = methodType(Object.class, params2).generic().parameterList(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
388 |
testWrongArgumentCount(params, params2); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
389 |
testWrongArgumentCount(params2, params); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
390 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
391 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
392 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
393 |
public void testWrongArgumentCount(List<Class<?>> expect, List<Class<?>> observe) throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
394 |
countTest(false); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
395 |
if (expect.equals(observe)) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
396 |
assert(false); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
397 |
MethodHandle target = callable(expect); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
398 |
Object[] args = zeroArgs(observe); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
399 |
Object junk; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
400 |
try { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
401 |
switch (args.length) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
402 |
case 0: |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
403 |
junk = target.invoke(); break; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
404 |
case 1: |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
405 |
junk = target.invoke(args[0]); break; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
406 |
case 2: |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
407 |
junk = target.invoke(args[0], args[1]); break; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
408 |
case 3: |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
409 |
junk = target.invoke(args[0], args[1], args[2]); break; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
410 |
case 4: |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
411 |
junk = target.invoke(args[0], args[1], args[2], args[3]); break; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
412 |
default: |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
413 |
junk = target.invokeWithArguments(args); break; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
414 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
415 |
} catch (WrongMethodTypeException ex) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
416 |
return; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
417 |
} catch (Exception ex) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
418 |
throw new RuntimeException("wrong exception calling "+target+target.type()+" on "+Arrays.asList(args)+" : "+ex); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
419 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
420 |
throw new RuntimeException("bad success calling "+target+target.type()+" on "+Arrays.asList(args)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
421 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
422 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
423 |
/** Make a list of all combinations of the given types, with the given arities. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
424 |
* A void return type is possible iff the first type is void.class. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
425 |
*/ |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
426 |
static List<MethodType> allMethodTypes(int minargc, int maxargc, Class<?>... types) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
427 |
ArrayList<MethodType> result = new ArrayList<MethodType>(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
428 |
if (types.length > 0) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
429 |
ArrayList<MethodType> argcTypes = new ArrayList<MethodType>(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
430 |
// build arity-zero types first |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
431 |
for (Class<?> rtype : types) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
432 |
argcTypes.add(MethodType.methodType(rtype)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
433 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
434 |
if (types[0] == void.class) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
435 |
// void is not an argument type |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
436 |
types = Arrays.copyOfRange(types, 1, types.length); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
437 |
for (int argc = 0; argc <= maxargc; argc++) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
438 |
if (argc >= minargc) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
439 |
result.addAll(argcTypes); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
440 |
if (argc >= maxargc) |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
441 |
break; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
442 |
ArrayList<MethodType> prevTypes = argcTypes; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
443 |
argcTypes = new ArrayList<MethodType>(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
444 |
for (MethodType prevType : prevTypes) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
445 |
for (Class<?> ptype : types) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
446 |
argcTypes.add(prevType.insertParameterTypes(argc, ptype)); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
447 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
448 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
449 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
450 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
451 |
return Collections.unmodifiableList(result); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
452 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
453 |
static List<MethodType> allMethodTypes(int argc, Class<?>... types) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
454 |
return allMethodTypes(argc, argc, types); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
455 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
456 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
457 |
interface RandomInterface { } |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
458 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
459 |
MethodHandle toString_MH; |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
460 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
461 |
@Test |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
462 |
public void testReferenceConversions() throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
463 |
startTest("testReferenceConversions"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
464 |
toString_MH = LOOKUP. |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
465 |
findVirtual(Object.class, "toString", MethodType.methodType(String.class)); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
466 |
Object[] args = { "one", "two" }; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
467 |
for (MethodType type : allMethodTypes(2, Object.class, String.class, RandomInterface.class)) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
468 |
testReferenceConversions(type, args); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
469 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
470 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
471 |
public void testReferenceConversions(MethodType type, Object... args) throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
472 |
countTest(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
473 |
if (verbosity > 3) System.out.println("target type: "+type); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
474 |
MethodHandle mh = callable(type.parameterList()); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
475 |
MethodHandle tsdrop = MethodHandles.dropArguments(toString_MH, 1, type.parameterList()); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
476 |
mh = MethodHandles.foldArguments(tsdrop, mh); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
477 |
mh = mh.asType(type); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
478 |
Object res = mh.invoke((String)args[0], (Object)args[1]); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
479 |
//System.out.println(res); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
480 |
assertEquals(Arrays.asList(args).toString(), res); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
481 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
482 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
483 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
484 |
@Test @Ignore("known failure pending 6939861") |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
485 |
public void testBoxConversions() throws Throwable { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
486 |
startTest("testBoxConversions"); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
487 |
countTest(); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
488 |
Object[] args = { 1, 2 }; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
489 |
MethodHandle mh = callable(Object.class, int.class); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
490 |
Object res; List resl; |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
491 |
res = resl = (List) mh.invoke((int)args[0], (Object)args[1]); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
492 |
//System.out.println(res); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
493 |
assertEquals(Arrays.asList(args), res); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
494 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
495 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
diff
changeset
|
496 |
} |