author | jrose |
Wed, 18 Jan 2012 17:34:29 -0800 | |
changeset 11534 | 9949ffb8eb3a |
parent 10081 | 0f7b9636aa64 |
child 13423 | 17843fff200d |
permissions | -rw-r--r-- |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
1 |
/* |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
4 |
* |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
10 |
* |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
15 |
* accompanied this code). |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
16 |
* |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
20 |
* |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
23 |
* questions. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
24 |
*/ |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
25 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
26 |
/* @test |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
27 |
* @summary example code used in javadoc for java.lang.invoke API |
9033
a88f5656f05d
6817525: turn on method handle functionality by default for JSR 292
jrose
parents:
8822
diff
changeset
|
28 |
* @compile JavaDocExamplesTest.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.JavaDocExamplesTest |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
30 |
*/ |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
31 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
32 |
/* |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
33 |
---- To run outside jtreg: |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
34 |
$ $JAVA7X_HOME/bin/javac -cp $JUNIT4_JAR -d /tmp/Classes \ |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
35 |
$DAVINCI/sources/jdk/test/java/lang/invoke/JavaDocExamplesTest.java |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
36 |
$ $JAVA7X_HOME/bin/java -cp $JUNIT4_JAR:/tmp/Classes \ |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
37 |
-DJavaDocExamplesTest.verbosity=1 \ |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
38 |
test.java.lang.invoke.JavaDocExamplesTest |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
39 |
---- |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
40 |
*/ |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
41 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
42 |
package test.java.lang.invoke; |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
43 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
44 |
import java.lang.invoke.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
45 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
46 |
import static java.lang.invoke.MethodType.*; |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
47 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
48 |
import java.util.*; |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
49 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
50 |
import org.junit.*; |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
51 |
import static org.junit.Assert.*; |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
52 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
53 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
54 |
/** |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
55 |
* @author jrose |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
56 |
*/ |
11534
9949ffb8eb3a
7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents:
10081
diff
changeset
|
57 |
@SuppressWarnings("LocalVariableHidesMemberVariable") |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
58 |
public class JavaDocExamplesTest { |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
59 |
/** Wrapper for running the JUnit tests in this module. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
60 |
* Put JUnit on the classpath! |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
61 |
*/ |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
62 |
public static void main(String... ignore) throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
63 |
System.out.println("can run this as:"); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
64 |
System.out.println("$ java org.junit.runner.JUnitCore "+JavaDocExamplesTest.class.getName()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
65 |
new JavaDocExamplesTest().run(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
66 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
67 |
public void run() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
68 |
testFindVirtual(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
69 |
testPermuteArguments(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
70 |
testDropArguments(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
71 |
testFilterArguments(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
72 |
testFoldArguments(); |
10081 | 73 |
testFoldArguments2(); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
74 |
testMethodHandlesSummary(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
75 |
testAsSpreader(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
76 |
testAsCollector(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
77 |
testAsVarargsCollector(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
78 |
testAsFixedArity(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
79 |
testAsTypeCornerCases(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
80 |
testMutableCallSite(); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
81 |
} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
82 |
// How much output? |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
83 |
static final Class<?> THIS_CLASS = JavaDocExamplesTest.class; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
84 |
static int verbosity = Integer.getInteger(THIS_CLASS.getSimpleName()+".verbosity", 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
85 |
|
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
86 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
87 |
{} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
88 |
static final private Lookup LOOKUP = lookup(); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
89 |
// static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
90 |
// "concat", methodType(String.class, String.class)); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
91 |
// static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
92 |
// "hashCode", methodType(int.class)); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
93 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
94 |
// form required if ReflectiveOperationException is intercepted: |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
95 |
static final private MethodHandle CONCAT_2, HASHCODE_2, ADD_2, SUB_2; |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
96 |
static { |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
97 |
try { |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
98 |
Class<?> THIS_CLASS = LOOKUP.lookupClass(); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
99 |
CONCAT_2 = LOOKUP.findVirtual(String.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
100 |
"concat", methodType(String.class, String.class)); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
101 |
HASHCODE_2 = LOOKUP.findVirtual(Object.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
102 |
"hashCode", methodType(int.class)); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
103 |
ADD_2 = LOOKUP.findStatic(THIS_CLASS, "add", methodType(int.class, int.class, int.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
104 |
SUB_2 = LOOKUP.findStatic(THIS_CLASS, "sub", methodType(int.class, int.class, int.class)); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
105 |
} catch (ReflectiveOperationException ex) { |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
106 |
throw new RuntimeException(ex); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
107 |
} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
108 |
} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
109 |
static int add(int x, int y) { return x + y; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
110 |
static int sub(int x, int y) { return x - y; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
111 |
|
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
112 |
{} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
113 |
|
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
114 |
@Test public void testFindVirtual() throws Throwable { |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
115 |
{} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
116 |
MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
117 |
"concat", methodType(String.class, String.class)); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
118 |
MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
119 |
"hashCode", methodType(int.class)); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
120 |
//assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y")); |
7556
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
121 |
assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y")); |
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
122 |
assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y")); |
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
123 |
//assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy")); |
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
124 |
assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy")); |
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
125 |
assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy")); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
126 |
{} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
127 |
} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
128 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
129 |
@Test public void testPermuteArguments() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
130 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
131 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
132 |
MethodType intfn1 = methodType(int.class, int.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
133 |
MethodType intfn2 = methodType(int.class, int.class, int.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
134 |
MethodHandle sub = SUB_2;// ... {int x, int y => x-y} ...; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
135 |
assert(sub.type().equals(intfn2)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
136 |
MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
137 |
MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
138 |
assert((int)rsub.invokeExact(1, 100) == 99); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
139 |
MethodHandle add = ADD_2;// ... {int x, int y => x+y} ...; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
140 |
assert(add.type().equals(intfn2)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
141 |
MethodHandle twice = permuteArguments(add, intfn1, 0, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
142 |
assert(twice.type().equals(intfn1)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
143 |
assert((int)twice.invokeExact(21) == 42); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
144 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
145 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
146 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
147 |
MethodHandle cat = lookup().findVirtual(String.class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
148 |
"concat", methodType(String.class, String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
149 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
150 |
MethodHandle d0 = dropArguments(cat, 0, String.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
151 |
assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
152 |
MethodHandle d1 = dropArguments(cat, 1, String.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
153 |
assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
154 |
MethodHandle d2 = dropArguments(cat, 2, String.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
155 |
assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
156 |
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
157 |
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
158 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
159 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
160 |
|
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
161 |
@Test public void testDropArguments() throws Throwable { |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
162 |
{{ |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
163 |
{} /// JAVADOC |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
164 |
MethodHandle cat = lookup().findVirtual(String.class, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
165 |
"concat", methodType(String.class, String.class)); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
166 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
167 |
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
168 |
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2)); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
169 |
assertEquals(bigType, d0.type()); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
170 |
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
171 |
}} |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
172 |
{{ |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
173 |
{} /// JAVADOC |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
174 |
MethodHandle cat = lookup().findVirtual(String.class, |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
175 |
"concat", methodType(String.class, String.class)); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8347
diff
changeset
|
176 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
177 |
MethodHandle d0 = dropArguments(cat, 0, String.class); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
178 |
assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
179 |
MethodHandle d1 = dropArguments(cat, 1, String.class); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
180 |
assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
181 |
MethodHandle d2 = dropArguments(cat, 2, String.class); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
182 |
assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
183 |
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
184 |
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
185 |
}} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
186 |
} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
187 |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
188 |
@Test public void testFilterArguments() throws Throwable { |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
189 |
{{ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
190 |
{} /// JAVADOC |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
191 |
MethodHandle cat = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
192 |
"concat", methodType(String.class, String.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
193 |
MethodHandle upcase = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
194 |
"toUpperCase", methodType(String.class)); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
195 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
196 |
MethodHandle f0 = filterArguments(cat, 0, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
197 |
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
198 |
MethodHandle f1 = filterArguments(cat, 1, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
199 |
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
200 |
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
201 |
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
202 |
}} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
203 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
204 |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
205 |
@Test public void testFoldArguments() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
206 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
207 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
208 |
MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
209 |
"println", methodType(void.class, String.class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
210 |
.bindTo(System.out); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
211 |
MethodHandle cat = lookup().findVirtual(String.class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
212 |
"concat", methodType(String.class, String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
213 |
assertEquals("boojum", (String) cat.invokeExact("boo", "jum")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
214 |
MethodHandle catTrace = foldArguments(cat, trace); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
215 |
// also prints "boo": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
216 |
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
217 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
218 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
219 |
|
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
220 |
static void assertEquals(Object exp, Object act) { |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
221 |
if (verbosity > 0) |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
222 |
System.out.println("result: "+act); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
223 |
Assert.assertEquals(exp, act); |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff
changeset
|
224 |
} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
225 |
|
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
226 |
@Test public void testMethodHandlesSummary() throws Throwable { |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
227 |
{{ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
228 |
{} /// JAVADOC |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
229 |
Object x, y; String s; int i; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
230 |
MethodType mt; MethodHandle mh; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
231 |
MethodHandles.Lookup lookup = MethodHandles.lookup(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
232 |
// mt is (char,char)String |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
233 |
mt = MethodType.methodType(String.class, char.class, char.class); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
234 |
mh = lookup.findVirtual(String.class, "replace", mt); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
235 |
s = (String) mh.invokeExact("daddy",'d','n'); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
236 |
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
237 |
assertEquals(s, "nanny"); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
238 |
// weakly typed invocation (using MHs.invoke) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
239 |
s = (String) mh.invokeWithArguments("sappy", 'p', 'v'); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
240 |
assertEquals(s, "savvy"); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
241 |
// mt is (Object[])List |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
242 |
mt = MethodType.methodType(java.util.List.class, Object[].class); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
243 |
mh = lookup.findStatic(java.util.Arrays.class, "asList", mt); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
244 |
assert(mh.isVarargsCollector()); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
245 |
x = mh.invoke("one", "two"); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
246 |
// invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
247 |
assertEquals(x, java.util.Arrays.asList("one","two")); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
248 |
// mt is (Object,Object,Object)Object |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
249 |
mt = MethodType.genericMethodType(3); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
250 |
mh = mh.asType(mt); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
251 |
x = mh.invokeExact((Object)1, (Object)2, (Object)3); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
252 |
// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
253 |
assertEquals(x, java.util.Arrays.asList(1,2,3)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
254 |
// mt is ()int |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
255 |
mt = MethodType.methodType(int.class); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
256 |
mh = lookup.findVirtual(java.util.List.class, "size", mt); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
257 |
i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3)); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
258 |
// invokeExact(Ljava/util/List;)I |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
259 |
assert(i == 3); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
260 |
mt = MethodType.methodType(void.class, String.class); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
261 |
mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
262 |
mh.invokeExact(System.out, "Hello, world."); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
263 |
// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
264 |
{} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
265 |
}} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
266 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
267 |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
268 |
@Test public void testAsSpreader() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
269 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
270 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
271 |
MethodHandle equals = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
272 |
.findVirtual(String.class, "equals", methodType(boolean.class, Object.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
273 |
assert( (boolean) equals.invokeExact("me", (Object)"me")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
274 |
assert(!(boolean) equals.invokeExact("me", (Object)"thee")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
275 |
// spread both arguments from a 2-array: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
276 |
MethodHandle eq2 = equals.asSpreader(Object[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
277 |
assert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
278 |
assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
279 |
// spread both arguments from a String array: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
280 |
MethodHandle eq2s = equals.asSpreader(String[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
281 |
assert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
282 |
assert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
283 |
// spread second arguments from a 1-array: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
284 |
MethodHandle eq1 = equals.asSpreader(Object[].class, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
285 |
assert( (boolean) eq1.invokeExact("me", new Object[]{ "me" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
286 |
assert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
287 |
// spread no arguments from a 0-array or null: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
288 |
MethodHandle eq0 = equals.asSpreader(Object[].class, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
289 |
assert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0])); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
290 |
assert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
291 |
// asSpreader and asCollector are approximate inverses: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
292 |
for (int n = 0; n <= 2; n++) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
293 |
for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
294 |
MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
295 |
assert( (boolean) equals2.invokeWithArguments("me", "me")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
296 |
assert(!(boolean) equals2.invokeWithArguments("me", "thee")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
297 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
298 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
299 |
MethodHandle caToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
300 |
.findStatic(Arrays.class, "toString", methodType(String.class, char[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
301 |
assertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray())); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
302 |
MethodHandle caString3 = caToString.asCollector(char[].class, 3); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
303 |
assertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C')); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
304 |
MethodHandle caToString2 = caString3.asSpreader(char[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
305 |
assertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray())); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
306 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
307 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
308 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
309 |
@Test public void testAsCollector() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
310 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
311 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
312 |
MethodHandle deepToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
313 |
.findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
314 |
assertEquals("[won]", (String) deepToString.invokeExact(new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
315 |
MethodHandle ts1 = deepToString.asCollector(Object[].class, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
316 |
assertEquals(methodType(String.class, Object.class), ts1.type()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
317 |
//assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); //FAIL |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
318 |
assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
319 |
// arrayType can be a subtype of Object[] |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
320 |
MethodHandle ts2 = deepToString.asCollector(String[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
321 |
assertEquals(methodType(String.class, String.class, String.class), ts2.type()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
322 |
assertEquals("[two, too]", (String) ts2.invokeExact("two", "too")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
323 |
MethodHandle ts0 = deepToString.asCollector(Object[].class, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
324 |
assertEquals("[]", (String) ts0.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
325 |
// collectors can be nested, Lisp-style |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
326 |
MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
327 |
assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D"))); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
328 |
// arrayType can be any primitive array type |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
329 |
MethodHandle bytesToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
330 |
.findStatic(Arrays.class, "toString", methodType(String.class, byte[].class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
331 |
.asCollector(byte[].class, 3); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
332 |
assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
333 |
MethodHandle longsToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
334 |
.findStatic(Arrays.class, "toString", methodType(String.class, long[].class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
335 |
.asCollector(long[].class, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
336 |
assertEquals("[123]", (String) longsToString.invokeExact((long)123)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
337 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
338 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
339 |
|
11534
9949ffb8eb3a
7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents:
10081
diff
changeset
|
340 |
@SuppressWarnings("rawtypes") |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
341 |
@Test public void testAsVarargsCollector() throws Throwable { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
342 |
{{ |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
343 |
{} /// JAVADOC |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
344 |
MethodHandle deepToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
345 |
.findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
346 |
MethodHandle ts1 = deepToString.asVarargsCollector(Object[].class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
347 |
assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
348 |
assertEquals("[won]", (String) ts1.invoke( new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
349 |
assertEquals("[won]", (String) ts1.invoke( "won" )); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
350 |
assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
351 |
// findStatic of Arrays.asList(...) produces a variable arity method handle: |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
352 |
MethodHandle asList = publicLookup() |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
353 |
.findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
354 |
assertEquals(methodType(List.class, Object[].class), asList.type()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
355 |
assert(asList.isVarargsCollector()); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
356 |
assertEquals("[]", asList.invoke().toString()); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
357 |
assertEquals("[1]", asList.invoke(1).toString()); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
358 |
assertEquals("[two, too]", asList.invoke("two", "too").toString()); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
359 |
String[] argv = { "three", "thee", "tee" }; |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
360 |
assertEquals("[three, thee, tee]", asList.invoke(argv).toString()); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
361 |
assertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString()); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9033
diff
changeset
|
362 |
List ls = (List) asList.invoke((Object)argv); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
363 |
assertEquals(1, ls.size()); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
364 |
assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0))); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
365 |
}} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
366 |
} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
367 |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
368 |
@Test public void testAsFixedArity() throws Throwable { |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
369 |
{{ |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
370 |
{} /// JAVADOC |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
371 |
MethodHandle asListVar = publicLookup() |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
372 |
.findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
373 |
.asVarargsCollector(Object[].class); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
374 |
MethodHandle asListFix = asListVar.asFixedArity(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
375 |
assertEquals("[1]", asListVar.invoke(1).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
376 |
Exception caught = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
377 |
try { asListFix.invoke((Object)1); } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
378 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
379 |
assert(caught instanceof ClassCastException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
380 |
assertEquals("[two, too]", asListVar.invoke("two", "too").toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
381 |
try { asListFix.invoke("two", "too"); } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
382 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
383 |
assert(caught instanceof WrongMethodTypeException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
384 |
Object[] argv = { "three", "thee", "tee" }; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
385 |
assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
386 |
assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
387 |
assertEquals(1, ((List) asListVar.invoke((Object)argv)).size()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
388 |
assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
389 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
390 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
391 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
392 |
@Test public void testAsTypeCornerCases() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
393 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
394 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
395 |
MethodHandle i2s = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
396 |
.findVirtual(Integer.class, "toString", methodType(String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
397 |
i2s = i2s.asType(i2s.type().unwrap()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
398 |
MethodHandle l2s = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
399 |
.findVirtual(Long.class, "toString", methodType(String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
400 |
l2s = l2s.asType(l2s.type().unwrap()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
401 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
402 |
Exception caught = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
403 |
try { i2s.asType(methodType(String.class, String.class)); } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
404 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
405 |
assert(caught instanceof WrongMethodTypeException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
406 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
407 |
i2s.asType(methodType(String.class, byte.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
408 |
i2s.asType(methodType(String.class, Byte.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
409 |
i2s.asType(methodType(String.class, Character.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
410 |
i2s.asType(methodType(String.class, Integer.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
411 |
l2s.asType(methodType(String.class, byte.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
412 |
l2s.asType(methodType(String.class, Byte.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
413 |
l2s.asType(methodType(String.class, Character.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
414 |
l2s.asType(methodType(String.class, Integer.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
415 |
l2s.asType(methodType(String.class, Long.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
416 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
417 |
caught = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
418 |
try { i2s.asType(methodType(String.class, Long.class)); } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
419 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
420 |
assert(caught instanceof WrongMethodTypeException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
421 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
422 |
MethodHandle i2sGen = i2s.asType(methodType(String.class, Object.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
423 |
MethodHandle l2sGen = l2s.asType(methodType(String.class, Object.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
424 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
425 |
i2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
426 |
i2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
427 |
l2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
428 |
l2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
429 |
l2sGen.invoke(0x420000000L); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
430 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
431 |
caught = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
432 |
try { i2sGen.invoke(0x420000000L); } // long -> Long -> Object -> Integer CCE |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
433 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
434 |
assert(caught instanceof ClassCastException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
435 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
436 |
caught = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
437 |
try { i2sGen.invoke("asdf"); } // String -> Object -> Integer CCE |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
438 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
439 |
assert(caught instanceof ClassCastException); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
440 |
{} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
441 |
}} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
442 |
} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
443 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
444 |
@Test public void testMutableCallSite() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
445 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
446 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
447 |
MutableCallSite name = new MutableCallSite(MethodType.methodType(String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
448 |
MethodHandle MH_name = name.dynamicInvoker(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
449 |
MethodType MT_str1 = MethodType.methodType(String.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
450 |
MethodHandle MH_upcase = MethodHandles.lookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
451 |
.findVirtual(String.class, "toUpperCase", MT_str1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
452 |
MethodHandle worker1 = MethodHandles.filterReturnValue(MH_name, MH_upcase); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
453 |
name.setTarget(MethodHandles.constant(String.class, "Rocky")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
454 |
assertEquals("ROCKY", (String) worker1.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
455 |
name.setTarget(MethodHandles.constant(String.class, "Fred")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
456 |
assertEquals("FRED", (String) worker1.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
457 |
// (mutation can be continued indefinitely) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
458 |
/* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
459 |
* </pre></blockquote> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
460 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
461 |
* The same call site may be used in several places at once. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
462 |
* <blockquote><pre> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
463 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
464 |
MethodType MT_str2 = MethodType.methodType(String.class, String.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
465 |
MethodHandle MH_cat = lookup().findVirtual(String.class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
466 |
"concat", methodType(String.class, String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
467 |
MethodHandle MH_dear = MethodHandles.insertArguments(MH_cat, 1, ", dear?"); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
468 |
MethodHandle worker2 = MethodHandles.filterReturnValue(MH_name, MH_dear); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
469 |
assertEquals("Fred, dear?", (String) worker2.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
470 |
name.setTarget(MethodHandles.constant(String.class, "Wilma")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
471 |
assertEquals("WILMA", (String) worker1.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
472 |
assertEquals("Wilma, dear?", (String) worker2.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
473 |
{} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
474 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
475 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
476 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
477 |
@Test public void testSwitchPoint() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
478 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
479 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
480 |
MethodHandle MH_strcat = MethodHandles.lookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
481 |
.findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
482 |
SwitchPoint spt = new SwitchPoint(); |
9782
973c21557e1a
7051206: JSR 292 method name SwitchPoint.isValid is misleading to unwary users; should be hasBeenInvalidated
jrose
parents:
9752
diff
changeset
|
483 |
assert(!spt.hasBeenInvalidated()); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
484 |
// the following steps may be repeated to re-use the same switch point: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
485 |
MethodHandle worker1 = MH_strcat; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
486 |
MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
487 |
MethodHandle worker = spt.guardWithTest(worker1, worker2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
488 |
assertEquals("method", (String) worker.invokeExact("met", "hod")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
489 |
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt }); |
9782
973c21557e1a
7051206: JSR 292 method name SwitchPoint.isValid is misleading to unwary users; should be hasBeenInvalidated
jrose
parents:
9752
diff
changeset
|
490 |
assert(spt.hasBeenInvalidated()); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
491 |
assertEquals("hodmet", (String) worker.invokeExact("met", "hod")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
492 |
{} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
493 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
494 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
495 |
|
10081 | 496 |
@Test public void testFoldArguments2() throws Throwable { |
497 |
{{ |
|
498 |
{} /// JAVADOC |
|
499 |
// argument-based dispatch for methods of the form boolean x.___(y: String) |
|
500 |
Lookup lookup = publicLookup(); |
|
501 |
// first, a tracing hack: |
|
502 |
MethodHandle println = lookup.findVirtual(java.io.PrintStream.class, "println", methodType(void.class, String.class)); |
|
503 |
MethodHandle arrayToString = lookup.findStatic(Arrays.class, "toString", methodType(String.class, Object[].class)); |
|
504 |
MethodHandle concat = lookup.findVirtual(String.class, "concat", methodType(String.class, String.class)); |
|
505 |
MethodHandle arrayToString_DIS = filterReturnValue(arrayToString, concat.bindTo("DIS:")); |
|
506 |
MethodHandle arrayToString_INV = filterReturnValue(arrayToString, concat.bindTo("INV:")); |
|
507 |
MethodHandle printArgs_DIS = filterReturnValue(arrayToString_DIS, println.bindTo(System.out)).asVarargsCollector(Object[].class); |
|
508 |
MethodHandle printArgs_INV = filterReturnValue(arrayToString_INV, println.bindTo(System.out)).asVarargsCollector(Object[].class); |
|
509 |
// metaobject protocol: |
|
510 |
MethodType mtype = methodType(boolean.class, String.class); |
|
511 |
MethodHandle findVirtual = lookup.findVirtual(Lookup.class, |
|
512 |
"findVirtual", methodType(MethodHandle.class, Class.class, String.class, MethodType.class)); |
|
513 |
MethodHandle getClass = lookup.findVirtual(Object.class, |
|
514 |
"getClass", methodType(Class.class)); |
|
515 |
MethodHandle dispatch = findVirtual; |
|
516 |
dispatch = filterArguments(dispatch, 1, getClass); |
|
517 |
dispatch = insertArguments(dispatch, 3, mtype); |
|
518 |
dispatch = dispatch.bindTo(lookup); |
|
519 |
assertEquals(methodType(MethodHandle.class, Object.class, String.class), dispatch.type()); |
|
520 |
MethodHandle invoker = invoker(mtype.insertParameterTypes(0, Object.class)); |
|
521 |
// wrap tracing around the dispatch and invoke steps: |
|
522 |
dispatch = foldArguments(dispatch, printArgs_DIS.asType(dispatch.type().changeReturnType(void.class))); |
|
523 |
invoker = foldArguments(invoker, printArgs_INV.asType(invoker.type().changeReturnType(void.class))); |
|
524 |
invoker = dropArguments(invoker, 2, String.class); // ignore selector |
|
525 |
// compose the dispatcher and the invoker: |
|
526 |
MethodHandle invokeDispatched = foldArguments(invoker, dispatch); |
|
527 |
Object x = "football", y = new java.util.Scanner("bar"); |
|
528 |
assert( (boolean) invokeDispatched.invokeExact(x, "startsWith", "foo")); |
|
529 |
assert(!(boolean) invokeDispatched.invokeExact(x, "startsWith", "#")); |
|
530 |
assert( (boolean) invokeDispatched.invokeExact(x, "endsWith", "all")); |
|
531 |
assert(!(boolean) invokeDispatched.invokeExact(x, "endsWith", "foo")); |
|
532 |
assert( (boolean) invokeDispatched.invokeExact(y, "hasNext", "[abc]+[rst]")); |
|
533 |
assert(!(boolean) invokeDispatched.invokeExact(y, "hasNext", "[123]+[789]")); |
|
534 |
}} |
|
535 |
} |
|
536 |
||
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
537 |
/* ---- TEMPLATE ---- |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
538 |
@Test public void testFoo() throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
539 |
{{ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
540 |
{} /// JAVADOC |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
541 |
{} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
542 |
}} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
543 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
544 |
*/ |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7052
diff
changeset
|
545 |
} |