2010-09-09 01:40:23 +00:00
|
|
|
/*
|
2011-02-11 09:26:24 +00:00
|
|
|
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
|
2010-09-09 01:40:23 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* @test
|
2011-03-24 06:02:31 +00:00
|
|
|
* @summary example code used in javadoc for java.lang.invoke API
|
2011-04-08 05:07:06 +00:00
|
|
|
* @compile JavaDocExamplesTest.java
|
|
|
|
* @run junit/othervm test.java.lang.invoke.JavaDocExamplesTest
|
2010-09-09 01:40:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
---- To run outside jtreg:
|
|
|
|
$ $JAVA7X_HOME/bin/javac -cp $JUNIT4_JAR -d /tmp/Classes \
|
2011-03-24 06:02:31 +00:00
|
|
|
$DAVINCI/sources/jdk/test/java/lang/invoke/JavaDocExamplesTest.java
|
2010-09-09 01:40:23 +00:00
|
|
|
$ $JAVA7X_HOME/bin/java -cp $JUNIT4_JAR:/tmp/Classes \
|
2011-05-27 00:37:36 +00:00
|
|
|
-DJavaDocExamplesTest.verbosity=1 \
|
2011-03-24 06:02:31 +00:00
|
|
|
test.java.lang.invoke.JavaDocExamplesTest
|
2010-09-09 01:40:23 +00:00
|
|
|
----
|
|
|
|
*/
|
|
|
|
|
2011-03-24 06:02:31 +00:00
|
|
|
package test.java.lang.invoke;
|
2010-09-09 01:40:23 +00:00
|
|
|
|
2011-03-24 06:02:31 +00:00
|
|
|
import java.lang.invoke.*;
|
|
|
|
import static java.lang.invoke.MethodHandles.*;
|
|
|
|
import static java.lang.invoke.MethodType.*;
|
2010-09-09 01:40:23 +00:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.junit.*;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author jrose
|
|
|
|
*/
|
2010-10-31 04:08:23 +00:00
|
|
|
public class JavaDocExamplesTest {
|
2010-09-09 01:40:23 +00:00
|
|
|
/** Wrapper for running the JUnit tests in this module.
|
|
|
|
* Put JUnit on the classpath!
|
|
|
|
*/
|
2011-05-27 00:37:36 +00:00
|
|
|
public static void main(String... ignore) throws Throwable {
|
|
|
|
System.out.println("can run this as:");
|
|
|
|
System.out.println("$ java org.junit.runner.JUnitCore "+JavaDocExamplesTest.class.getName());
|
|
|
|
new JavaDocExamplesTest().run();
|
|
|
|
}
|
|
|
|
public void run() throws Throwable {
|
|
|
|
testFindVirtual();
|
|
|
|
testPermuteArguments();
|
|
|
|
testDropArguments();
|
|
|
|
testFilterArguments();
|
|
|
|
testFoldArguments();
|
|
|
|
testMethodHandlesSummary();
|
|
|
|
testAsSpreader();
|
|
|
|
testAsCollector();
|
|
|
|
testAsVarargsCollector();
|
|
|
|
testAsFixedArity();
|
|
|
|
testAsTypeCornerCases();
|
|
|
|
testMutableCallSite();
|
2010-09-09 01:40:23 +00:00
|
|
|
}
|
|
|
|
// How much output?
|
2011-05-27 00:37:36 +00:00
|
|
|
static final Class<?> THIS_CLASS = JavaDocExamplesTest.class;
|
|
|
|
static int verbosity = Integer.getInteger(THIS_CLASS.getSimpleName()+".verbosity", 0);
|
|
|
|
|
2010-09-09 01:40:23 +00:00
|
|
|
|
|
|
|
{}
|
|
|
|
static final private Lookup LOOKUP = lookup();
|
|
|
|
// static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class,
|
|
|
|
// "concat", methodType(String.class, String.class));
|
|
|
|
// static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class,
|
|
|
|
// "hashCode", methodType(int.class));
|
|
|
|
|
2011-02-11 09:26:32 +00:00
|
|
|
// form required if ReflectiveOperationException is intercepted:
|
2011-05-27 00:37:36 +00:00
|
|
|
static final private MethodHandle CONCAT_2, HASHCODE_2, ADD_2, SUB_2;
|
2010-09-09 01:40:23 +00:00
|
|
|
static {
|
|
|
|
try {
|
2011-05-27 00:37:36 +00:00
|
|
|
Class<?> THIS_CLASS = LOOKUP.lookupClass();
|
2010-09-09 01:40:23 +00:00
|
|
|
CONCAT_2 = LOOKUP.findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
HASHCODE_2 = LOOKUP.findVirtual(Object.class,
|
|
|
|
"hashCode", methodType(int.class));
|
2011-05-27 00:37:36 +00:00
|
|
|
ADD_2 = LOOKUP.findStatic(THIS_CLASS, "add", methodType(int.class, int.class, int.class));
|
|
|
|
SUB_2 = LOOKUP.findStatic(THIS_CLASS, "sub", methodType(int.class, int.class, int.class));
|
2011-02-11 09:26:32 +00:00
|
|
|
} catch (ReflectiveOperationException ex) {
|
2010-09-09 01:40:23 +00:00
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
|
|
|
}
|
2011-05-27 00:37:36 +00:00
|
|
|
static int add(int x, int y) { return x + y; }
|
|
|
|
static int sub(int x, int y) { return x - y; }
|
|
|
|
|
2010-09-09 01:40:23 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
@Test public void testFindVirtual() throws Throwable {
|
|
|
|
{}
|
|
|
|
MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
|
|
|
|
"hashCode", methodType(int.class));
|
|
|
|
//assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y"));
|
2010-11-23 06:41:31 +00:00
|
|
|
assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y"));
|
|
|
|
assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y"));
|
|
|
|
//assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy"));
|
|
|
|
assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy"));
|
|
|
|
assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy"));
|
2010-09-09 01:40:23 +00:00
|
|
|
{}
|
|
|
|
}
|
2011-05-27 00:37:36 +00:00
|
|
|
|
|
|
|
@Test public void testPermuteArguments() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodType intfn1 = methodType(int.class, int.class);
|
|
|
|
MethodType intfn2 = methodType(int.class, int.class, int.class);
|
|
|
|
MethodHandle sub = SUB_2;// ... {int x, int y => x-y} ...;
|
|
|
|
assert(sub.type().equals(intfn2));
|
|
|
|
MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1);
|
|
|
|
MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0);
|
|
|
|
assert((int)rsub.invokeExact(1, 100) == 99);
|
|
|
|
MethodHandle add = ADD_2;// ... {int x, int y => x+y} ...;
|
|
|
|
assert(add.type().equals(intfn2));
|
|
|
|
MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
|
|
|
|
assert(twice.type().equals(intfn1));
|
|
|
|
assert((int)twice.invokeExact(21) == 42);
|
|
|
|
}}
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
|
|
|
MethodHandle d0 = dropArguments(cat, 0, String.class);
|
|
|
|
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
|
|
|
|
MethodHandle d1 = dropArguments(cat, 1, String.class);
|
|
|
|
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
|
|
|
|
MethodHandle d2 = dropArguments(cat, 2, String.class);
|
|
|
|
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
|
|
|
|
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
|
|
|
|
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2010-09-09 01:40:23 +00:00
|
|
|
@Test public void testDropArguments() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
2011-03-24 06:02:31 +00:00
|
|
|
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
|
|
|
|
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
|
|
|
|
assertEquals(bigType, d0.type());
|
|
|
|
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
|
|
|
|
}}
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
2010-09-09 01:40:23 +00:00
|
|
|
MethodHandle d0 = dropArguments(cat, 0, String.class);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
|
2010-09-09 01:40:23 +00:00
|
|
|
MethodHandle d1 = dropArguments(cat, 1, String.class);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
|
2010-09-09 01:40:23 +00:00
|
|
|
MethodHandle d2 = dropArguments(cat, 2, String.class);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
|
2010-09-09 01:40:23 +00:00
|
|
|
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
|
2010-09-09 01:40:23 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2010-10-31 04:08:23 +00:00
|
|
|
@Test public void testFilterArguments() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
MethodHandle upcase = lookup().findVirtual(String.class,
|
|
|
|
"toUpperCase", methodType(String.class));
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("xy", (String) cat.invokeExact("x", "y"));
|
2010-10-31 04:08:23 +00:00
|
|
|
MethodHandle f0 = filterArguments(cat, 0, upcase);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
|
2010-10-31 04:08:23 +00:00
|
|
|
MethodHandle f1 = filterArguments(cat, 1, upcase);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
|
2010-10-31 04:08:23 +00:00
|
|
|
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
|
2010-12-16 23:59:27 +00:00
|
|
|
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
|
2010-10-31 04:08:23 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2011-05-27 00:37:36 +00:00
|
|
|
@Test public void testFoldArguments() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
|
|
|
|
"println", methodType(void.class, String.class))
|
|
|
|
.bindTo(System.out);
|
|
|
|
MethodHandle cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
|
|
|
|
MethodHandle catTrace = foldArguments(cat, trace);
|
|
|
|
// also prints "boo":
|
|
|
|
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2010-09-09 01:40:23 +00:00
|
|
|
static void assertEquals(Object exp, Object act) {
|
|
|
|
if (verbosity > 0)
|
|
|
|
System.out.println("result: "+act);
|
|
|
|
Assert.assertEquals(exp, act);
|
|
|
|
}
|
2010-10-31 04:08:23 +00:00
|
|
|
|
2011-02-11 09:26:24 +00:00
|
|
|
@Test public void testMethodHandlesSummary() throws Throwable {
|
2010-10-31 04:08:23 +00:00
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
2011-02-11 09:26:24 +00:00
|
|
|
Object x, y; String s; int i;
|
|
|
|
MethodType mt; MethodHandle mh;
|
|
|
|
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
|
|
|
// mt is (char,char)String
|
|
|
|
mt = MethodType.methodType(String.class, char.class, char.class);
|
|
|
|
mh = lookup.findVirtual(String.class, "replace", mt);
|
|
|
|
s = (String) mh.invokeExact("daddy",'d','n');
|
2011-02-11 09:26:28 +00:00
|
|
|
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
|
2011-05-27 00:37:36 +00:00
|
|
|
assertEquals(s, "nanny");
|
2011-02-11 09:26:24 +00:00
|
|
|
// weakly typed invocation (using MHs.invoke)
|
|
|
|
s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
|
2011-05-27 00:37:36 +00:00
|
|
|
assertEquals(s, "savvy");
|
2011-02-11 09:26:24 +00:00
|
|
|
// mt is (Object[])List
|
|
|
|
mt = MethodType.methodType(java.util.List.class, Object[].class);
|
|
|
|
mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
|
|
|
|
assert(mh.isVarargsCollector());
|
2011-05-13 02:27:33 +00:00
|
|
|
x = mh.invoke("one", "two");
|
|
|
|
// invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
|
2011-05-27 00:37:36 +00:00
|
|
|
assertEquals(x, java.util.Arrays.asList("one","two"));
|
2011-02-11 09:26:24 +00:00
|
|
|
// mt is (Object,Object,Object)Object
|
|
|
|
mt = MethodType.genericMethodType(3);
|
2011-02-11 09:26:28 +00:00
|
|
|
mh = mh.asType(mt);
|
2011-02-11 09:26:24 +00:00
|
|
|
x = mh.invokeExact((Object)1, (Object)2, (Object)3);
|
2011-02-11 09:26:28 +00:00
|
|
|
// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
|
2011-05-27 00:37:36 +00:00
|
|
|
assertEquals(x, java.util.Arrays.asList(1,2,3));
|
|
|
|
// mt is ()int
|
2011-02-11 09:26:24 +00:00
|
|
|
mt = MethodType.methodType(int.class);
|
|
|
|
mh = lookup.findVirtual(java.util.List.class, "size", mt);
|
|
|
|
i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3));
|
2011-02-11 09:26:28 +00:00
|
|
|
// invokeExact(Ljava/util/List;)I
|
2011-02-11 09:26:24 +00:00
|
|
|
assert(i == 3);
|
|
|
|
mt = MethodType.methodType(void.class, String.class);
|
|
|
|
mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
|
|
|
|
mh.invokeExact(System.out, "Hello, world.");
|
2011-02-11 09:26:28 +00:00
|
|
|
// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
|
2011-02-11 09:26:24 +00:00
|
|
|
{}
|
2010-10-31 04:08:23 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2011-05-27 00:37:36 +00:00
|
|
|
@Test public void testAsSpreader() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle equals = publicLookup()
|
|
|
|
.findVirtual(String.class, "equals", methodType(boolean.class, Object.class));
|
|
|
|
assert( (boolean) equals.invokeExact("me", (Object)"me"));
|
|
|
|
assert(!(boolean) equals.invokeExact("me", (Object)"thee"));
|
|
|
|
// spread both arguments from a 2-array:
|
|
|
|
MethodHandle eq2 = equals.asSpreader(Object[].class, 2);
|
|
|
|
assert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" }));
|
|
|
|
assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" }));
|
|
|
|
// spread both arguments from a String array:
|
|
|
|
MethodHandle eq2s = equals.asSpreader(String[].class, 2);
|
|
|
|
assert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" }));
|
|
|
|
assert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" }));
|
|
|
|
// spread second arguments from a 1-array:
|
|
|
|
MethodHandle eq1 = equals.asSpreader(Object[].class, 1);
|
|
|
|
assert( (boolean) eq1.invokeExact("me", new Object[]{ "me" }));
|
|
|
|
assert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" }));
|
|
|
|
// spread no arguments from a 0-array or null:
|
|
|
|
MethodHandle eq0 = equals.asSpreader(Object[].class, 0);
|
|
|
|
assert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0]));
|
|
|
|
assert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null));
|
|
|
|
// asSpreader and asCollector are approximate inverses:
|
|
|
|
for (int n = 0; n <= 2; n++) {
|
|
|
|
for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) {
|
|
|
|
MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n);
|
|
|
|
assert( (boolean) equals2.invokeWithArguments("me", "me"));
|
|
|
|
assert(!(boolean) equals2.invokeWithArguments("me", "thee"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MethodHandle caToString = publicLookup()
|
|
|
|
.findStatic(Arrays.class, "toString", methodType(String.class, char[].class));
|
|
|
|
assertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray()));
|
|
|
|
MethodHandle caString3 = caToString.asCollector(char[].class, 3);
|
|
|
|
assertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C'));
|
|
|
|
MethodHandle caToString2 = caString3.asSpreader(char[].class, 2);
|
|
|
|
assertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray()));
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test public void testAsCollector() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle deepToString = publicLookup()
|
|
|
|
.findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
|
|
|
|
assertEquals("[won]", (String) deepToString.invokeExact(new Object[]{"won"}));
|
|
|
|
MethodHandle ts1 = deepToString.asCollector(Object[].class, 1);
|
|
|
|
assertEquals(methodType(String.class, Object.class), ts1.type());
|
|
|
|
//assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); //FAIL
|
|
|
|
assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"}));
|
|
|
|
// arrayType can be a subtype of Object[]
|
|
|
|
MethodHandle ts2 = deepToString.asCollector(String[].class, 2);
|
|
|
|
assertEquals(methodType(String.class, String.class, String.class), ts2.type());
|
|
|
|
assertEquals("[two, too]", (String) ts2.invokeExact("two", "too"));
|
|
|
|
MethodHandle ts0 = deepToString.asCollector(Object[].class, 0);
|
|
|
|
assertEquals("[]", (String) ts0.invokeExact());
|
|
|
|
// collectors can be nested, Lisp-style
|
|
|
|
MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2);
|
|
|
|
assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D")));
|
|
|
|
// arrayType can be any primitive array type
|
|
|
|
MethodHandle bytesToString = publicLookup()
|
|
|
|
.findStatic(Arrays.class, "toString", methodType(String.class, byte[].class))
|
|
|
|
.asCollector(byte[].class, 3);
|
|
|
|
assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3));
|
|
|
|
MethodHandle longsToString = publicLookup()
|
|
|
|
.findStatic(Arrays.class, "toString", methodType(String.class, long[].class))
|
|
|
|
.asCollector(long[].class, 1);
|
|
|
|
assertEquals("[123]", (String) longsToString.invokeExact((long)123));
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2011-02-11 09:26:24 +00:00
|
|
|
@Test public void testAsVarargsCollector() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
2011-05-27 00:37:36 +00:00
|
|
|
MethodHandle deepToString = publicLookup()
|
|
|
|
.findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
|
|
|
|
MethodHandle ts1 = deepToString.asVarargsCollector(Object[].class);
|
|
|
|
assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"}));
|
|
|
|
assertEquals("[won]", (String) ts1.invoke( new Object[]{"won"}));
|
|
|
|
assertEquals("[won]", (String) ts1.invoke( "won" ));
|
|
|
|
assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"}));
|
|
|
|
// findStatic of Arrays.asList(...) produces a variable arity method handle:
|
2011-02-11 09:26:24 +00:00
|
|
|
MethodHandle asList = publicLookup()
|
2011-05-27 00:37:36 +00:00
|
|
|
.findStatic(Arrays.class, "asList", methodType(List.class, Object[].class));
|
|
|
|
assertEquals(methodType(List.class, Object[].class), asList.type());
|
|
|
|
assert(asList.isVarargsCollector());
|
2011-05-13 02:27:33 +00:00
|
|
|
assertEquals("[]", asList.invoke().toString());
|
|
|
|
assertEquals("[1]", asList.invoke(1).toString());
|
|
|
|
assertEquals("[two, too]", asList.invoke("two", "too").toString());
|
2011-05-27 00:37:36 +00:00
|
|
|
String[] argv = { "three", "thee", "tee" };
|
2011-05-13 02:27:33 +00:00
|
|
|
assertEquals("[three, thee, tee]", asList.invoke(argv).toString());
|
2011-05-27 00:37:36 +00:00
|
|
|
assertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString());
|
2011-05-13 02:27:33 +00:00
|
|
|
List ls = (List) asList.invoke((Object)argv);
|
2011-02-11 09:26:24 +00:00
|
|
|
assertEquals(1, ls.size());
|
|
|
|
assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
|
|
|
|
}}
|
|
|
|
}
|
2010-10-31 04:08:23 +00:00
|
|
|
|
2011-05-27 00:37:36 +00:00
|
|
|
@Test public void testAsFixedArity() throws Throwable {
|
2011-02-11 09:26:24 +00:00
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
2011-05-27 00:37:36 +00:00
|
|
|
MethodHandle asListVar = publicLookup()
|
2011-02-11 09:26:24 +00:00
|
|
|
.findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
|
|
|
|
.asVarargsCollector(Object[].class);
|
2011-05-27 00:37:36 +00:00
|
|
|
MethodHandle asListFix = asListVar.asFixedArity();
|
|
|
|
assertEquals("[1]", asListVar.invoke(1).toString());
|
|
|
|
Exception caught = null;
|
|
|
|
try { asListFix.invoke((Object)1); }
|
|
|
|
catch (Exception ex) { caught = ex; }
|
|
|
|
assert(caught instanceof ClassCastException);
|
|
|
|
assertEquals("[two, too]", asListVar.invoke("two", "too").toString());
|
|
|
|
try { asListFix.invoke("two", "too"); }
|
|
|
|
catch (Exception ex) { caught = ex; }
|
|
|
|
assert(caught instanceof WrongMethodTypeException);
|
|
|
|
Object[] argv = { "three", "thee", "tee" };
|
|
|
|
assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString());
|
|
|
|
assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString());
|
|
|
|
assertEquals(1, ((List) asListVar.invoke((Object)argv)).size());
|
|
|
|
assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString());
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test public void testAsTypeCornerCases() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle i2s = publicLookup()
|
|
|
|
.findVirtual(Integer.class, "toString", methodType(String.class));
|
|
|
|
i2s = i2s.asType(i2s.type().unwrap());
|
|
|
|
MethodHandle l2s = publicLookup()
|
|
|
|
.findVirtual(Long.class, "toString", methodType(String.class));
|
|
|
|
l2s = l2s.asType(l2s.type().unwrap());
|
|
|
|
|
|
|
|
Exception caught = null;
|
|
|
|
try { i2s.asType(methodType(String.class, String.class)); }
|
|
|
|
catch (Exception ex) { caught = ex; }
|
|
|
|
assert(caught instanceof WrongMethodTypeException);
|
|
|
|
|
|
|
|
i2s.asType(methodType(String.class, byte.class));
|
|
|
|
i2s.asType(methodType(String.class, Byte.class));
|
|
|
|
i2s.asType(methodType(String.class, Character.class));
|
|
|
|
i2s.asType(methodType(String.class, Integer.class));
|
|
|
|
l2s.asType(methodType(String.class, byte.class));
|
|
|
|
l2s.asType(methodType(String.class, Byte.class));
|
|
|
|
l2s.asType(methodType(String.class, Character.class));
|
|
|
|
l2s.asType(methodType(String.class, Integer.class));
|
|
|
|
l2s.asType(methodType(String.class, Long.class));
|
|
|
|
|
|
|
|
caught = null;
|
|
|
|
try { i2s.asType(methodType(String.class, Long.class)); }
|
|
|
|
catch (Exception ex) { caught = ex; }
|
|
|
|
assert(caught instanceof WrongMethodTypeException);
|
|
|
|
|
|
|
|
MethodHandle i2sGen = i2s.asType(methodType(String.class, Object.class));
|
|
|
|
MethodHandle l2sGen = l2s.asType(methodType(String.class, Object.class));
|
|
|
|
|
|
|
|
i2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int
|
|
|
|
i2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int
|
|
|
|
l2sGen.invoke(42); // int -> Integer -> Object -> Integer -> int
|
|
|
|
l2sGen.invoke((byte)4); // byte -> Byte -> Object -> Byte -> byte -> int
|
|
|
|
l2sGen.invoke(0x420000000L);
|
|
|
|
|
|
|
|
caught = null;
|
|
|
|
try { i2sGen.invoke(0x420000000L); } // long -> Long -> Object -> Integer CCE
|
|
|
|
catch (Exception ex) { caught = ex; }
|
|
|
|
assert(caught instanceof ClassCastException);
|
|
|
|
|
|
|
|
caught = null;
|
|
|
|
try { i2sGen.invoke("asdf"); } // String -> Object -> Integer CCE
|
|
|
|
catch (Exception ex) { caught = ex; }
|
|
|
|
assert(caught instanceof ClassCastException);
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test public void testMutableCallSite() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MutableCallSite name = new MutableCallSite(MethodType.methodType(String.class));
|
|
|
|
MethodHandle MH_name = name.dynamicInvoker();
|
|
|
|
MethodType MT_str1 = MethodType.methodType(String.class);
|
|
|
|
MethodHandle MH_upcase = MethodHandles.lookup()
|
|
|
|
.findVirtual(String.class, "toUpperCase", MT_str1);
|
|
|
|
MethodHandle worker1 = MethodHandles.filterReturnValue(MH_name, MH_upcase);
|
|
|
|
name.setTarget(MethodHandles.constant(String.class, "Rocky"));
|
|
|
|
assertEquals("ROCKY", (String) worker1.invokeExact());
|
|
|
|
name.setTarget(MethodHandles.constant(String.class, "Fred"));
|
|
|
|
assertEquals("FRED", (String) worker1.invokeExact());
|
|
|
|
// (mutation can be continued indefinitely)
|
|
|
|
/*
|
|
|
|
* </pre></blockquote>
|
|
|
|
* <p>
|
|
|
|
* The same call site may be used in several places at once.
|
|
|
|
* <blockquote><pre>
|
|
|
|
*/
|
|
|
|
MethodType MT_str2 = MethodType.methodType(String.class, String.class);
|
|
|
|
MethodHandle MH_cat = lookup().findVirtual(String.class,
|
|
|
|
"concat", methodType(String.class, String.class));
|
|
|
|
MethodHandle MH_dear = MethodHandles.insertArguments(MH_cat, 1, ", dear?");
|
|
|
|
MethodHandle worker2 = MethodHandles.filterReturnValue(MH_name, MH_dear);
|
|
|
|
assertEquals("Fred, dear?", (String) worker2.invokeExact());
|
|
|
|
name.setTarget(MethodHandles.constant(String.class, "Wilma"));
|
|
|
|
assertEquals("WILMA", (String) worker1.invokeExact());
|
|
|
|
assertEquals("Wilma, dear?", (String) worker2.invokeExact());
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test public void testSwitchPoint() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
|
|
|
MethodHandle MH_strcat = MethodHandles.lookup()
|
|
|
|
.findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
|
|
|
|
SwitchPoint spt = new SwitchPoint();
|
2011-06-03 18:20:20 +00:00
|
|
|
assert(!spt.hasBeenInvalidated());
|
2011-05-27 00:37:36 +00:00
|
|
|
// the following steps may be repeated to re-use the same switch point:
|
|
|
|
MethodHandle worker1 = MH_strcat;
|
|
|
|
MethodHandle worker2 = MethodHandles.permuteArguments(MH_strcat, MH_strcat.type(), 1, 0);
|
|
|
|
MethodHandle worker = spt.guardWithTest(worker1, worker2);
|
|
|
|
assertEquals("method", (String) worker.invokeExact("met", "hod"));
|
|
|
|
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
|
2011-06-03 18:20:20 +00:00
|
|
|
assert(spt.hasBeenInvalidated());
|
2011-05-27 00:37:36 +00:00
|
|
|
assertEquals("hodmet", (String) worker.invokeExact("met", "hod"));
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---- TEMPLATE ----
|
|
|
|
@Test public void testFoo() throws Throwable {
|
|
|
|
{{
|
|
|
|
{} /// JAVADOC
|
2011-02-11 09:26:24 +00:00
|
|
|
{}
|
|
|
|
}}
|
|
|
|
}
|
2011-05-27 00:37:36 +00:00
|
|
|
*/
|
2010-09-09 01:40:23 +00:00
|
|
|
}
|