jdk/test/java/lang/invoke/JavaDocExamplesTest.java
author jrose
Wed, 23 Mar 2011 23:02:31 -0700
changeset 8822 8145ab9f5f86
parent 8347 jdk/test/java/dyn/JavaDocExamplesTest.java@e5daa5772ffd
child 9033 a88f5656f05d
permissions -rw-r--r--
7012648: move JSR 292 to package java.lang.invoke and adjust names Summary: package and class renaming only; delete unused methods and classes; add @since tags; no code changes Reviewed-by: twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    28
 * @compile -XDallowTransitionalJSR292=no JavaDocExamplesTest.java
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    29
 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles 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 \
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    37
   -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles \
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    38
   -Dtest.java.lang.invoke.JavaDocExamplesTest.verbosity=1 \
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    39
     test.java.lang.invoke.JavaDocExamplesTest
7052
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
*/
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    42
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    43
package test.java.lang.invoke;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    44
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    45
import java.lang.invoke.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    46
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    47
import static java.lang.invoke.MethodType.*;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    48
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    49
import java.lang.reflect.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    50
import java.util.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    51
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    52
import org.junit.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    53
import static org.junit.Assert.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    54
import static org.junit.Assume.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    55
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    56
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    57
/**
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    58
 * @author jrose
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    59
 */
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    60
public class JavaDocExamplesTest {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    61
    /** Wrapper for running the JUnit tests in this module.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    62
     *  Put JUnit on the classpath!
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    63
     */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    64
    public static void main(String... ignore) {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    65
        org.junit.runner.JUnitCore.runClasses(JavaDocExamplesTest.class);
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    66
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    67
    // How much output?
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    68
    static int verbosity = Integer.getInteger("test.java.lang.invoke.JavaDocExamplesTest.verbosity", 0);
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    69
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    70
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    71
static final private Lookup LOOKUP = lookup();
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    72
// static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    73
//     "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    74
// static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    75
//     "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    76
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
    77
// form required if ReflectiveOperationException is intercepted:
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    78
static final private MethodHandle CONCAT_2, HASHCODE_2;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    79
static {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    80
  try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    81
    CONCAT_2 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    82
      "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    83
    HASHCODE_2 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    84
      "hashCode", methodType(int.class));
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
    85
   } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    86
     throw new RuntimeException(ex);
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
}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    89
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    90
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    91
    @Test public void testFindVirtual() throws Throwable {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    92
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    93
MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    94
  "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    95
MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    96
  "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    97
//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
    98
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
    99
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
   100
//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
   101
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
   102
assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   103
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   104
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   105
    @Test public void testDropArguments() throws Throwable {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   106
        {{
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   107
{} /// JAVADOC
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   108
MethodHandle cat = lookup().findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   109
  "concat", methodType(String.class, String.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   110
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
   111
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
   112
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
   113
assertEquals(bigType, d0.type());
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   114
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
   115
            }}
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   116
        {{
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   117
{} /// JAVADOC
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   118
MethodHandle cat = lookup().findVirtual(String.class,
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   119
  "concat", methodType(String.class, String.class));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   120
assertEquals("xy", (String) cat.invokeExact("x", "y"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   121
MethodHandle d0 = dropArguments(cat, 0, String.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   122
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   123
MethodHandle d1 = dropArguments(cat, 1, String.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   124
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   125
MethodHandle d2 = dropArguments(cat, 2, String.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   126
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   127
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   128
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   129
            }}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   130
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   131
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   132
    @Test public void testFilterArguments() throws Throwable {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   133
        {{
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   134
{} /// JAVADOC
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   135
MethodHandle cat = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   136
  "concat", methodType(String.class, String.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   137
MethodHandle upcase = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   138
  "toUpperCase", methodType(String.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   139
assertEquals("xy", (String) cat.invokeExact("x", "y"));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   140
MethodHandle f0 = filterArguments(cat, 0, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   141
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   142
MethodHandle f1 = filterArguments(cat, 1, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   143
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   144
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   145
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   146
            }}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   147
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   148
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   149
    static void assertEquals(Object exp, Object act) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   150
        if (verbosity > 0)
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   151
            System.out.println("result: "+act);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   152
        Assert.assertEquals(exp, act);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   153
    }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   154
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   155
    @Test public void testMethodHandlesSummary() throws Throwable {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   156
        {{
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   157
{} /// JAVADOC
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   158
Object x, y; String s; int i;
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   159
MethodType mt; MethodHandle mh;
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   160
MethodHandles.Lookup lookup = MethodHandles.lookup();
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   161
// mt is (char,char)String
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   162
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
   163
mh = lookup.findVirtual(String.class, "replace", mt);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   164
s = (String) mh.invokeExact("daddy",'d','n');
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   165
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   166
assert(s.equals("nanny"));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   167
// weakly typed invocation (using MHs.invoke)
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   168
s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   169
assert(s.equals("savvy"));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   170
// mt is (Object[])List
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   171
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
   172
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
   173
assert(mh.isVarargsCollector());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   174
x = mh.invokeGeneric("one", "two");
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   175
// invokeGeneric(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   176
assert(x.equals(java.util.Arrays.asList("one","two")));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   177
// mt is (Object,Object,Object)Object
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   178
mt = MethodType.genericMethodType(3);
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   179
mh = mh.asType(mt);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   180
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
   181
// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   182
assert(x.equals(java.util.Arrays.asList(1,2,3)));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   183
// mt is { => int}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   184
mt = MethodType.methodType(int.class);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   185
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
   186
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
   187
// invokeExact(Ljava/util/List;)I
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   188
assert(i == 3);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   189
mt = MethodType.methodType(void.class, String.class);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   190
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
   191
mh.invokeExact(System.out, "Hello, world.");
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   192
// 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
   193
{}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   194
            }}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   195
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   196
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   197
    @Test public void testAsVarargsCollector() throws Throwable {
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   198
        {{
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   199
{} /// JAVADOC
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   200
MethodHandle asList = publicLookup()
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   201
  .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
   202
  .asVarargsCollector(Object[].class);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   203
assertEquals("[]", asList.invokeGeneric().toString());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   204
assertEquals("[1]", asList.invokeGeneric(1).toString());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   205
assertEquals("[two, too]", asList.invokeGeneric("two", "too").toString());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   206
Object[] argv = { "three", "thee", "tee" };
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   207
assertEquals("[three, thee, tee]", asList.invokeGeneric(argv).toString());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   208
List ls = (List) asList.invokeGeneric((Object)argv);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   209
assertEquals(1, ls.size());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   210
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
   211
            }}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   212
    }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   213
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   214
    @Test public void testVarargsCollectorSuppression() throws Throwable {
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   215
        {{
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   216
{} /// JAVADOC
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   217
MethodHandle vamh = publicLookup()
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   218
  .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
   219
  .asVarargsCollector(Object[].class);
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   220
MethodHandle mh = MethodHandles.exactInvoker(vamh.type()).bindTo(vamh);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   221
assert(vamh.type().equals(mh.type()));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   222
assertEquals("[1, 2, 3]", vamh.invokeGeneric(1,2,3).toString());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   223
boolean failed = false;
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   224
try { mh.invokeGeneric(1,2,3); }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   225
catch (WrongMethodTypeException ex) { failed = true; }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   226
assert(failed);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   227
{}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   228
            }}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   229
    }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   230
}