jdk/test/java/lang/invoke/JavaDocExamplesTest.java
author mhaupt
Wed, 28 Sep 2016 14:02:21 +0200
changeset 41207 813a335bcb0c
parent 37347 f6abe5d4bb1d
permissions -rw-r--r--
8151179: address issues raised by JCK team on JEP 274 API Reviewed-by: jrose, redestad, psandoz
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
/*
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
     2
 * Copyright (c) 2009, 2016, 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
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 20533
diff changeset
     7
 * published by the Free Software Foundation.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     8
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     9
 * 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
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * accompanied this code).
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    14
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 * 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
    18
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    19
 * 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
    20
 * 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
    21
 * questions.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    22
 */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    23
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    24
/* @test
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    25
 * @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
    26
 * @compile JavaDocExamplesTest.java
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    27
 * @run testng/othervm test.java.lang.invoke.JavaDocExamplesTest
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    28
 */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    29
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    30
package test.java.lang.invoke;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    31
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
    32
import java.io.StringWriter;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    33
import java.lang.invoke.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    34
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
    35
import static java.lang.invoke.MethodType.*;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    36
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    37
import java.util.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    38
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    39
import org.testng.*;
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    40
import org.testng.annotations.*;
7052
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
/**
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    43
 * @author jrose
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    44
 */
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
    45
public class JavaDocExamplesTest {
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    46
    /** Wrapper for running the TestNG tests in this module.
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    47
     *  Put TestNG on the classpath!
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    48
     */
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    49
    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
    50
        new JavaDocExamplesTest().run();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    51
    }
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    52
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    53
    public void run() throws Throwable {
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
    54
        testMisc();
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
    55
        testFindStatic();
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
    56
        testFindConstructor();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    57
        testFindVirtual();
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
    58
        testFindSpecial();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    59
        testPermuteArguments();
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
    60
        testZero();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    61
        testDropArguments();
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
    62
        testDropArgumentsToMatch();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    63
        testFilterArguments();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    64
        testFoldArguments();
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
    65
        testFoldArguments2();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    66
        testMethodHandlesSummary();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    67
        testAsSpreader();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    68
        testAsCollector();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    69
        testAsVarargsCollector();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    70
        testAsFixedArity();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    71
        testAsTypeCornerCases();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    72
        testMutableCallSite();
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    73
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    74
    // How much output?
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    75
    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
    76
    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
    77
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    78
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    79
{}
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    80
private static final Lookup LOOKUP = lookup();
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    81
// static final private MethodHandle CONCAT_1 = 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
// static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    84
//     "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    85
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 8346
diff changeset
    86
// form required if ReflectiveOperationException is intercepted:
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    87
    private static final MethodHandle CONCAT_2, HASHCODE_2, ADD_2, SUB_2;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    88
static {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    89
  try {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    90
    Class<?> THIS_CLASS = LOOKUP.lookupClass();
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    91
    CONCAT_2 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    92
      "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    93
    HASHCODE_2 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    94
      "hashCode", methodType(int.class));
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
    95
    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
    96
    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
    97
   } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    98
     throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    99
   }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   100
}
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   101
    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
   102
    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
   103
7052
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
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   106
    @Test public void testMisc() throws Throwable {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   107
// Extra tests, not from javadoc:
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   108
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   109
MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   110
  "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   111
MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   112
  "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   113
//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
   114
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
   115
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
   116
//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
   117
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
   118
assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   119
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   120
    }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   121
20531
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   122
    @Test public void testFindStatic() throws Throwable {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   123
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   124
MethodHandle MH_asList = publicLookup().findStatic(Arrays.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   125
  "asList", methodType(List.class, Object[].class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   126
assertEquals("[x, y]", MH_asList.invoke("x", "y").toString());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   127
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   128
    }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   129
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   130
    @Test public void testFindVirtual() throws Throwable {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   131
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   132
MethodHandle MH_concat = publicLookup().findVirtual(String.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   133
  "concat", methodType(String.class, String.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   134
MethodHandle MH_hashCode = publicLookup().findVirtual(Object.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   135
  "hashCode", methodType(int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   136
MethodHandle MH_hashCode_String = publicLookup().findVirtual(String.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   137
  "hashCode", methodType(int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   138
assertEquals("xy", (String) MH_concat.invokeExact("x", "y"));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   139
assertEquals("xy".hashCode(), (int) MH_hashCode.invokeExact((Object)"xy"));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   140
assertEquals("xy".hashCode(), (int) MH_hashCode_String.invokeExact("xy"));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   141
// interface method:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   142
MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   143
  "subSequence", methodType(CharSequence.class, int.class, int.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   144
assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   145
// constructor "internal method" must be accessed differently:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   146
MethodType MT_newString = methodType(void.class); //()V for new String()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   147
try { assertEquals("impossible", lookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   148
        .findVirtual(String.class, "<init>", MT_newString));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   149
 } catch (NoSuchMethodException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   150
MethodHandle MH_newString = publicLookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   151
  .findConstructor(String.class, MT_newString);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   152
assertEquals("", (String) MH_newString.invokeExact());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   153
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   154
    }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   155
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   156
    @Test public void testFindConstructor() throws Throwable {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   157
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   158
MethodHandle MH_newArrayList = publicLookup().findConstructor(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   159
  ArrayList.class, methodType(void.class, Collection.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   160
Collection orig = Arrays.asList("x", "y");
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   161
Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   162
assert(orig != copy);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   163
assertEquals(orig, copy);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   164
// a variable-arity constructor:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   165
MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   166
  ProcessBuilder.class, methodType(void.class, String[].class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   167
ProcessBuilder pb = (ProcessBuilder)
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   168
  MH_newProcessBuilder.invoke("x", "y", "z");
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   169
assertEquals("[x, y, z]", pb.command().toString());
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   170
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   171
    }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   172
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   173
// for testFindSpecial
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   174
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   175
static class Listie extends ArrayList {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   176
  public String toString() { return "[wee Listie]"; }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   177
  static Lookup lookup() { return MethodHandles.lookup(); }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   178
}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   179
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   180
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   181
    @Test public void testFindSpecial() throws Throwable {
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   182
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   183
// no access to constructor via invokeSpecial:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   184
MethodHandle MH_newListie = Listie.lookup()
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   185
  .findConstructor(Listie.class, methodType(void.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   186
Listie l = (Listie) MH_newListie.invokeExact();
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   187
try { assertEquals("impossible", Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   188
        Listie.class, "<init>", methodType(void.class), Listie.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   189
 } catch (NoSuchMethodException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   190
// access to super and self methods via invokeSpecial:
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   191
MethodHandle MH_super = Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   192
  ArrayList.class, "toString" , methodType(String.class), Listie.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   193
MethodHandle MH_this = Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   194
  Listie.class, "toString" , methodType(String.class), Listie.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   195
MethodHandle MH_duper = Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   196
  Object.class, "toString" , methodType(String.class), Listie.class);
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   197
assertEquals("[]", (String) MH_super.invokeExact(l));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   198
assertEquals(""+l, (String) MH_this.invokeExact(l));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   199
assertEquals("[]", (String) MH_duper.invokeExact(l)); // ArrayList method
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   200
try { assertEquals("inaccessible", Listie.lookup().findSpecial(
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   201
        String.class, "toString", methodType(String.class), Listie.class));
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   202
 } catch (IllegalAccessException ex) { } // OK
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   203
Listie subl = new Listie() { public String toString() { return "[subclass]"; } };
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   204
assertEquals(""+l, (String) MH_this.invokeExact(subl)); // Listie method
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   205
{}
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   206
    }
14c2a04f21e6 8001108: an attempt to use "<init>" as a method name should elicit NoSuchMethodException
jrose
parents: 20530
diff changeset
   207
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   208
    @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
   209
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   210
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   211
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
   212
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
   213
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
   214
assert(sub.type().equals(intfn2));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   215
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
   216
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
   217
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
   218
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
   219
assert(add.type().equals(intfn2));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   220
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
   221
assert(twice.type().equals(intfn1));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   222
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
   223
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   224
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   225
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   226
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
   227
  "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
   228
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
   229
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
   230
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
   231
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
   232
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
   233
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
   234
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
   235
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
   236
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
   237
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   238
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   239
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   240
@Test public void testZero() throws Throwable {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   241
        {{
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   242
{} /// JAVADOC
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   243
Class<?> type = Double.class;
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   244
MethodHandle mh1 = MethodHandles.explicitCastArguments(MethodHandles.constant(Object.class, null), methodType(type));
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   245
assertEquals("()Double", mh1.type().toString());
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   246
MethodHandle mh2 = MethodHandles.empty(methodType(type));
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   247
assertEquals("()Double", mh2.type().toString());
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   248
        }}
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   249
   }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   250
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   251
    @Test public void testDropArguments() throws Throwable {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   252
        {{
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   253
{} /// JAVADOC
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   254
MethodHandle cat = lookup().findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   255
  "concat", methodType(String.class, String.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   256
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
   257
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
   258
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
   259
assertEquals(bigType, d0.type());
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   260
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
   261
            }}
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   262
        {{
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   263
{} /// JAVADOC
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   264
MethodHandle cat = lookup().findVirtual(String.class,
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   265
  "concat", methodType(String.class, String.class));
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8347
diff changeset
   266
assertEquals("xy", (String) cat.invokeExact("x", "y"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   267
MethodHandle d0 = dropArguments(cat, 0, String.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   268
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   269
MethodHandle d1 = dropArguments(cat, 1, String.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   270
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   271
MethodHandle d2 = dropArguments(cat, 2, String.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   272
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   273
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   274
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   275
            }}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   276
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   277
37347
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   278
    @Test public void testDropArgumentsToMatch() throws Throwable {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   279
        {{
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   280
{} /// JAVADOC
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   281
MethodHandle h0= constant(boolean.class, true);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   282
MethodHandle h1 = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   283
MethodType bigType = h1.type().insertParameterTypes(1, String.class, int.class);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   284
MethodHandle h2 = dropArguments(h1, 0, bigType.parameterList());
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   285
if (h1.type().parameterCount() < h2.type().parameterCount()) {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   286
    h1 = dropArgumentsToMatch(h1, 0, h2.type().parameterList(), 0);  // lengthen h1
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   287
}
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   288
else {
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   289
    h2 = dropArgumentsToMatch(h2, 0, h1.type().parameterList(), 0);    // lengthen h2
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   290
}
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   291
MethodHandle h3 = guardWithTest(h0, h1, h2);
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   292
assertEquals("xy", h3.invoke("x", "y", 1, "a", "b", "c"));
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   293
        }}
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   294
    }
f6abe5d4bb1d 8150829: Enhanced drop-args, identity and default constant, varargs adjustment
srastogi
parents: 36218
diff changeset
   295
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   296
    @Test public void testFilterArguments() throws Throwable {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   297
        {{
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   298
{} /// JAVADOC
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   299
MethodHandle cat = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   300
  "concat", methodType(String.class, String.class));
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   301
MethodHandle upcase = lookup().findVirtual(String.class,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   302
  "toUpperCase", methodType(String.class));
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   303
assertEquals("xy", (String) cat.invokeExact("x", "y"));
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   304
MethodHandle f0 = filterArguments(cat, 0, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   305
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   306
MethodHandle f1 = filterArguments(cat, 1, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   307
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   308
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7556
diff changeset
   309
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   310
            }}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   311
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   312
20533
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   313
    @Test public void testCollectArguments() throws Throwable {
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   314
        {{
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   315
{} /// JAVADOC
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   316
MethodHandle deepToString = publicLookup()
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   317
  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   318
MethodHandle ts1 = deepToString.asCollector(String[].class, 1);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   319
assertEquals("[strange]", (String) ts1.invokeExact("strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   320
MethodHandle ts2 = deepToString.asCollector(String[].class, 2);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   321
assertEquals("[up, down]", (String) ts2.invokeExact("up", "down"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   322
MethodHandle ts3 = deepToString.asCollector(String[].class, 3);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   323
MethodHandle ts3_ts2 = collectArguments(ts3, 1, ts2);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   324
assertEquals("[top, [up, down], strange]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   325
             (String) ts3_ts2.invokeExact("top", "up", "down", "strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   326
MethodHandle ts3_ts2_ts1 = collectArguments(ts3_ts2, 3, ts1);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   327
assertEquals("[top, [up, down], [strange]]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   328
             (String) ts3_ts2_ts1.invokeExact("top", "up", "down", "strange"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   329
MethodHandle ts3_ts2_ts3 = collectArguments(ts3_ts2, 1, ts3);
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   330
assertEquals("[top, [[up, down, strange], charm], bottom]",
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   331
             (String) ts3_ts2_ts3.invokeExact("top", "up", "down", "strange", "charm", "bottom"));
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   332
            }}
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   333
    }
bee974bc42ac 8001110: method handles should have a collectArguments transform, generalizing asCollector
jrose
parents: 20531
diff changeset
   334
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   335
    @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
   336
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   337
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   338
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
   339
  "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
   340
    .bindTo(System.out);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   341
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
   342
  "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
   343
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
   344
MethodHandle catTrace = foldArguments(cat, trace);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   345
// also prints "boo":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   346
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
   347
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   348
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   349
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   350
    static void assertEquals(Object exp, Object act) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   351
        if (verbosity > 0)
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   352
            System.out.println("result: "+act);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   353
        Assert.assertEquals(exp, act);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   354
    }
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   355
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   356
    static void assertTrue(boolean b) {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   357
        if (verbosity > 0) {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   358
            System.out.println("result: " + b);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   359
        }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   360
        Assert.assertTrue(b);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   361
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   362
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   363
    @Test public void testMethodHandlesSummary() throws Throwable {
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   364
        {{
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   365
{} /// JAVADOC
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   366
Object x, y; String s; int i;
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   367
MethodType mt; MethodHandle mh;
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   368
MethodHandles.Lookup lookup = MethodHandles.lookup();
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   369
// mt is (char,char)String
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   370
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
   371
mh = lookup.findVirtual(String.class, "replace", mt);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   372
s = (String) mh.invokeExact("daddy",'d','n');
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   373
// 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
   374
assertEquals(s, "nanny");
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   375
// weakly typed invocation (using MHs.invoke)
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   376
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
   377
assertEquals(s, "savvy");
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   378
// mt is (Object[])List
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   379
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
   380
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
   381
assert(mh.isVarargsCollector());
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   382
x = mh.invoke("one", "two");
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   383
// 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
   384
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
   385
// mt is (Object,Object,Object)Object
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   386
mt = MethodType.genericMethodType(3);
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   387
mh = mh.asType(mt);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   388
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
   389
// 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
   390
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
   391
// mt is ()int
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   392
mt = MethodType.methodType(int.class);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   393
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
   394
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
   395
// invokeExact(Ljava/util/List;)I
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   396
assert(i == 3);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   397
mt = MethodType.methodType(void.class, String.class);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   398
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
   399
mh.invokeExact(System.out, "Hello, world.");
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 8345
diff changeset
   400
// 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
   401
{}
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   402
            }}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   403
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   404
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   405
    @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
   406
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   407
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   408
MethodHandle equals = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   409
  .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
   410
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
   411
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
   412
// 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
   413
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
   414
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
   415
assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" }));
20530
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 19388
diff changeset
   416
// try to spread from anything but a 2-array:
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 19388
diff changeset
   417
for (int n = 0; n <= 10; n++) {
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 19388
diff changeset
   418
  Object[] badArityArgs = (n == 2 ? null : new Object[n]);
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 19388
diff changeset
   419
  try { assert((boolean) eq2.invokeExact(badArityArgs) && false); }
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 19388
diff changeset
   420
  catch (IllegalArgumentException ex) { } // OK
b54a1f5cd35f 8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents: 19388
diff changeset
   421
}
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   422
// 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
   423
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
   424
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
   425
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
   426
// 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
   427
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
   428
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
   429
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
   430
// 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
   431
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
   432
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
   433
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
   434
// 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
   435
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
   436
    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
   437
        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
   438
        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
   439
        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
   440
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   441
}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   442
MethodHandle caToString = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   443
  .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
   444
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
   445
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
   446
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
   447
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
   448
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
   449
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   450
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   451
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   452
    @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
   453
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   454
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   455
MethodHandle deepToString = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   456
  .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
   457
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
   458
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
   459
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
   460
//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
   461
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
   462
// 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
   463
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
   464
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
   465
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
   466
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
   467
assertEquals("[]", (String) ts0.invokeExact());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   468
// 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
   469
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
   470
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
   471
// 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
   472
MethodHandle bytesToString = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   473
  .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
   474
  .asCollector(byte[].class, 3);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   475
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
   476
MethodHandle longsToString = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   477
  .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
   478
  .asCollector(long[].class, 1);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   479
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
   480
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   481
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   482
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10081
diff changeset
   483
    @SuppressWarnings("rawtypes")
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   484
    @Test public void testAsVarargsCollector() throws Throwable {
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   485
        {{
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   486
{} /// JAVADOC
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   487
MethodHandle deepToString = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   488
  .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
   489
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
   490
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
   491
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
   492
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
   493
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
   494
// 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
   495
MethodHandle asList = publicLookup()
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   496
  .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
   497
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
   498
assert(asList.isVarargsCollector());
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   499
assertEquals("[]", asList.invoke().toString());
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   500
assertEquals("[1]", asList.invoke(1).toString());
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   501
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
   502
String[] argv = { "three", "thee", "tee" };
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 9033
diff changeset
   503
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
   504
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
   505
List ls = (List) asList.invoke((Object)argv);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   506
assertEquals(1, ls.size());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   507
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
   508
            }}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   509
    }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   510
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   511
    @Test public void testAsFixedArity() throws Throwable {
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   512
        {{
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   513
{} /// JAVADOC
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   514
MethodHandle asListVar = publicLookup()
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   515
  .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
   516
  .asVarargsCollector(Object[].class);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   517
MethodHandle asListFix = asListVar.asFixedArity();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   518
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
   519
Exception caught = null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   520
try { asListFix.invoke((Object)1); }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   521
catch (Exception ex) { caught = ex; }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   522
assert(caught instanceof ClassCastException);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   523
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
   524
try { asListFix.invoke("two", "too"); }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   525
catch (Exception ex) { caught = ex; }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   526
assert(caught instanceof WrongMethodTypeException);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   527
Object[] argv = { "three", "thee", "tee" };
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   528
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
   529
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
   530
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
   531
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
   532
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   533
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   534
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   535
    @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
   536
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   537
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   538
MethodHandle i2s = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   539
  .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
   540
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
   541
MethodHandle l2s = publicLookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   542
  .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
   543
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
   544
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   545
Exception caught = null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   546
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
   547
catch (Exception ex) { caught = ex; }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   548
assert(caught instanceof WrongMethodTypeException);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   549
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   550
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
   551
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
   552
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
   553
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
   554
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
   555
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
   556
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
   557
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
   558
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
   559
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   560
caught = null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   561
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
   562
catch (Exception ex) { caught = ex; }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   563
assert(caught instanceof WrongMethodTypeException);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   564
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   565
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
   566
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
   567
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   568
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
   569
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
   570
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
   571
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
   572
l2sGen.invoke(0x420000000L);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   573
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   574
caught = null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   575
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
   576
catch (Exception ex) { caught = ex; }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   577
assert(caught instanceof ClassCastException);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   578
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   579
caught = null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   580
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
   581
catch (Exception ex) { caught = ex; }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   582
assert(caught instanceof ClassCastException);
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   583
{}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   584
            }}
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   585
    }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   586
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   587
    @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
   588
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   589
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   590
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
   591
MethodHandle MH_name = name.dynamicInvoker();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   592
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
   593
MethodHandle MH_upcase = MethodHandles.lookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   594
    .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
   595
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
   596
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
   597
assertEquals("ROCKY", (String) worker1.invokeExact());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   598
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
   599
assertEquals("FRED", (String) worker1.invokeExact());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   600
// (mutation can be continued indefinitely)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   601
/*
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   602
 * </pre></blockquote>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   603
 * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   604
 * 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
   605
 * <blockquote><pre>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   606
 */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   607
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
   608
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
   609
  "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
   610
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
   611
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
   612
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
   613
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
   614
assertEquals("WILMA", (String) worker1.invokeExact());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   615
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
   616
{}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   617
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   618
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   619
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   620
    @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
   621
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   622
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   623
MethodHandle MH_strcat = MethodHandles.lookup()
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   624
    .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
   625
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
   626
assert(!spt.hasBeenInvalidated());
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   627
// 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
   628
MethodHandle worker1 = MH_strcat;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   629
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
   630
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
   631
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
   632
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
   633
assert(spt.hasBeenInvalidated());
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   634
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
   635
{}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   636
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   637
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   638
10081
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   639
    @Test public void testFoldArguments2() throws Throwable {
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   640
        {{
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   641
{} /// JAVADOC
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   642
// argument-based dispatch for methods of the form boolean x.___(y: String)
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   643
Lookup lookup = publicLookup();
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   644
// first, a tracing hack:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   645
MethodHandle println = lookup.findVirtual(java.io.PrintStream.class, "println", methodType(void.class, String.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   646
MethodHandle arrayToString = lookup.findStatic(Arrays.class, "toString", methodType(String.class, Object[].class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   647
MethodHandle concat = lookup.findVirtual(String.class, "concat", methodType(String.class, String.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   648
MethodHandle arrayToString_DIS = filterReturnValue(arrayToString, concat.bindTo("DIS:"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   649
MethodHandle arrayToString_INV = filterReturnValue(arrayToString, concat.bindTo("INV:"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   650
MethodHandle printArgs_DIS = filterReturnValue(arrayToString_DIS, println.bindTo(System.out)).asVarargsCollector(Object[].class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   651
MethodHandle printArgs_INV = filterReturnValue(arrayToString_INV, println.bindTo(System.out)).asVarargsCollector(Object[].class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   652
// metaobject protocol:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   653
MethodType mtype = methodType(boolean.class, String.class);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   654
MethodHandle findVirtual = lookup.findVirtual(Lookup.class,
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   655
  "findVirtual", methodType(MethodHandle.class, Class.class, String.class, MethodType.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   656
MethodHandle getClass = lookup.findVirtual(Object.class,
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   657
  "getClass", methodType(Class.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   658
MethodHandle dispatch = findVirtual;
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   659
dispatch = filterArguments(dispatch, 1, getClass);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   660
dispatch = insertArguments(dispatch, 3, mtype);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   661
dispatch = dispatch.bindTo(lookup);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   662
assertEquals(methodType(MethodHandle.class, Object.class, String.class), dispatch.type());
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   663
MethodHandle invoker = invoker(mtype.insertParameterTypes(0, Object.class));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   664
// wrap tracing around the dispatch and invoke steps:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   665
dispatch = foldArguments(dispatch, printArgs_DIS.asType(dispatch.type().changeReturnType(void.class)));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   666
invoker = foldArguments(invoker, printArgs_INV.asType(invoker.type().changeReturnType(void.class)));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   667
invoker = dropArguments(invoker, 2, String.class);  // ignore selector
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   668
// compose the dispatcher and the invoker:
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   669
MethodHandle invokeDispatched = foldArguments(invoker, dispatch);
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   670
Object x = "football", y = new java.util.Scanner("bar");
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   671
assert( (boolean) invokeDispatched.invokeExact(x, "startsWith", "foo"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   672
assert(!(boolean) invokeDispatched.invokeExact(x, "startsWith", "#"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   673
assert( (boolean) invokeDispatched.invokeExact(x, "endsWith", "all"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   674
assert(!(boolean) invokeDispatched.invokeExact(x, "endsWith", "foo"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   675
assert( (boolean) invokeDispatched.invokeExact(y, "hasNext", "[abc]+[rst]"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   676
assert(!(boolean) invokeDispatched.invokeExact(y, "hasNext", "[123]+[789]"));
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   677
            }}
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   678
    }
0f7b9636aa64 7058651: JSR 292 unit tests need a refresh
jrose
parents: 9782
diff changeset
   679
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   680
    static int one(int k) { return 1; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   681
    static int inc(int i, int acc, int k) { return i + 1; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   682
    static int mult(int i, int acc, int k) { return i * acc; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   683
    static boolean pred(int i, int acc, int k) { return i < k; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   684
    static int fin(int i, int acc, int k) { return acc; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   685
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   686
    @Test public void testLoop() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   687
        MethodHandle MH_inc, MH_one, MH_mult, MH_pred, MH_fin;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   688
        Class<?> I = int.class;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   689
        MH_inc = LOOKUP.findStatic(THIS_CLASS, "inc", methodType(I, I, I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   690
        MH_one = LOOKUP.findStatic(THIS_CLASS, "one", methodType(I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   691
        MH_mult = LOOKUP.findStatic(THIS_CLASS, "mult", methodType(I, I, I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   692
        MH_pred = LOOKUP.findStatic(THIS_CLASS, "pred", methodType(boolean.class, I, I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   693
        MH_fin = LOOKUP.findStatic(THIS_CLASS, "fin", methodType(I, I, I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   694
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   695
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   696
// iterative implementation of the factorial function as a loop handle
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   697
// null initializer for counter, should initialize to 0
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   698
MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   699
MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   700
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   701
assertEquals(120, loop.invoke(5));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   702
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   703
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   704
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   705
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   706
    static int inc(int i) { return i + 1; } // drop acc, k
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   707
    static int mult(int i, int acc) { return i * acc; } //drop k
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   708
    static boolean cmp(int i, int k) { return i < k; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   709
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   710
    @Test public void testSimplerLoop() throws Throwable {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   711
        MethodHandle MH_inc, MH_mult, MH_cmp;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   712
        Class<?> I = int.class;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   713
        MH_inc = LOOKUP.findStatic(THIS_CLASS, "inc", methodType(I, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   714
        MH_mult = LOOKUP.findStatic(THIS_CLASS, "mult", methodType(I, I, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   715
        MH_cmp = LOOKUP.findStatic(THIS_CLASS, "cmp", methodType(boolean.class, I, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   716
        {{
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   717
{} /// JAVADOC
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   718
// simplified implementation of the factorial function as a loop handle
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   719
// null initializer for counter, should initialize to 0
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   720
MethodHandle MH_one = MethodHandles.constant(int.class, 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   721
MethodHandle MH_pred = MethodHandles.dropArguments(MH_cmp, 1, int.class); // drop acc
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   722
MethodHandle MH_fin = MethodHandles.dropArguments(MethodHandles.identity(int.class), 0, int.class); // drop i
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   723
MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   724
MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   725
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   726
assertEquals(720, loop.invoke(6));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   727
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   728
        }}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   729
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   730
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   731
    // for testFacLoop
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   732
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   733
static class FacLoop {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   734
  final int k;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   735
  FacLoop(int k) { this.k = k; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   736
  int inc(int i) { return i + 1; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   737
  int mult(int i, int acc) { return i * acc; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   738
  boolean pred(int i) { return i < k; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   739
  int fin(int i, int acc) { return acc; }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   740
}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   741
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   742
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   743
    // assume MH_inc, MH_mult, and MH_pred are handles to the above methods
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   744
    @Test public void testFacLoop() throws Throwable {
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   745
        MethodHandle MH_FacLoop, MH_inc, MH_mult, MH_pred, MH_fin;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   746
        Class<?> I = int.class;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   747
        MH_FacLoop = LOOKUP.findConstructor(FacLoop.class, methodType(void.class, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   748
        MH_inc = LOOKUP.findVirtual(FacLoop.class, "inc", methodType(I, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   749
        MH_mult = LOOKUP.findVirtual(FacLoop.class, "mult", methodType(I, I, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   750
        MH_pred = LOOKUP.findVirtual(FacLoop.class, "pred", methodType(boolean.class, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   751
        MH_fin = LOOKUP.findVirtual(FacLoop.class, "fin", methodType(I, I, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   752
        {{
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   753
{} /// JAVADOC
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   754
// instance-based implementation of the factorial function as a loop handle
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   755
// null initializer for counter, should initialize to 0
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   756
MethodHandle MH_one = MethodHandles.constant(int.class, 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   757
MethodHandle[] instanceClause = new MethodHandle[]{MH_FacLoop};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   758
MethodHandle[] counterClause = new MethodHandle[]{null, MH_inc};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   759
MethodHandle[] accumulatorClause = new MethodHandle[]{MH_one, MH_mult, MH_pred, MH_fin};
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   760
MethodHandle loop = MethodHandles.loop(instanceClause, counterClause, accumulatorClause);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   761
assertEquals(5040, loop.invoke(7));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   762
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   763
        }}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   764
    }
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   765
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   766
    static List<String> initZip(Iterator<String> a, Iterator<String> b) { return new ArrayList<>(); }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   767
    static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) { return a.hasNext() && b.hasNext(); }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   768
    static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   769
        zip.add(a.next());
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   770
        zip.add(b.next());
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   771
        return zip;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   772
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   773
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   774
    @Test public void testWhileLoop() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   775
        MethodHandle MH_initZip, MH_zipPred, MH_zipStep;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   776
        Class<?> IT = Iterator.class;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   777
        Class<?> L = List.class;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   778
        MH_initZip = LOOKUP.findStatic(THIS_CLASS, "initZip", methodType(L, IT, IT));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   779
        MH_zipPred = LOOKUP.findStatic(THIS_CLASS, "zipPred", methodType(boolean.class, L, IT, IT));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   780
        MH_zipStep = LOOKUP.findStatic(THIS_CLASS, "zipStep", methodType(L, L, IT, IT));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   781
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   782
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   783
// implement the zip function for lists as a loop handle
36218
f02215b8d857 8150953: j.l.i.MethodHandles: example section in whileLoop(...) provides example for doWhileLoop
mhaupt
parents: 34430
diff changeset
   784
MethodHandle loop = MethodHandles.whileLoop(MH_initZip, MH_zipPred, MH_zipStep);
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   785
List<String> a = Arrays.asList("a", "b", "c", "d");
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   786
List<String> b = Arrays.asList("e", "f", "g", "h");
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   787
List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   788
assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   789
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   790
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   791
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   792
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   793
    static int zero(int limit) { return 0; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   794
    static int step(int i, int limit) { return i + 1; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   795
    static boolean pred(int i, int limit) { return i < limit; }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   796
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   797
    @Test public void testDoWhileLoop() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   798
        MethodHandle MH_zero, MH_step, MH_pred;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   799
        Class<?> I = int.class;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   800
        MH_zero = LOOKUP.findStatic(THIS_CLASS, "zero", methodType(I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   801
        MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(I, I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   802
        MH_pred = LOOKUP.findStatic(THIS_CLASS, "pred", methodType(boolean.class, I, I));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   803
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   804
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   805
// int i = 0; while (i < limit) { ++i; } return i; => limit
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   806
MethodHandle loop = MethodHandles.doWhileLoop(MH_zero, MH_step, MH_pred);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   807
assertEquals(23, loop.invoke(23));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   808
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   809
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   810
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   811
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   812
    static String step(String v, int counter, String start_) { return "na " + v; }  //#0
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   813
    static String step(String v, int counter ) { return "na " + v; } //#1
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   814
    static String step(String v, int counter, int iterations_, String pre, String start_) { return pre + " " + v; } //#2
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   815
    static String step3(String v, int counter, String pre) { return pre + " " + v; } //#3
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   816
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   817
    @Test public void testCountedLoop() throws Throwable {
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   818
        MethodHandle MH_step;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   819
        Class<?> S = String.class, I = int.class;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   820
        // Theme:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   821
        MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(S, S, I, S));
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   822
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   823
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   824
// String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   825
// => a variation on a well known theme
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   826
MethodHandle fit13 = MethodHandles.constant(int.class, 13);
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   827
MethodHandle start = MethodHandles.identity(String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   828
MethodHandle loop = MethodHandles.countedLoop(fit13, start, MH_step);  // (v, i, _) -> "na " + v
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   829
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   830
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   831
        }}
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   832
        // Variation #1:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   833
        MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(S, S, I));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   834
        {{
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   835
{} /// JAVADOC
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   836
// String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   837
// => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   838
MethodHandle count = MethodHandles.dropArguments(MethodHandles.identity(int.class), 1, String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   839
MethodHandle start = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0, int.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   840
MethodHandle loop = MethodHandles.countedLoop(count, start, MH_step);  // (v, i) -> "na " + v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   841
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke(13, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   842
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   843
            assertEquals("na na Lambdaman!", loop.invoke(2, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   844
            assertEquals("Lambdaman!", loop.invoke(0, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   845
            assertEquals("Lambdaman!", loop.invoke(-1, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   846
            assertEquals("Lambdaman!", loop.invoke(Integer.MIN_VALUE, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   847
        }}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   848
        // Variation #2:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   849
        MH_step = LOOKUP.findStatic(THIS_CLASS, "step", methodType(S, S, I, I, S, S));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   850
        {{
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   851
{} /// JAVADOC
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   852
// String s = "Lambdaman!", t = "na"; for (int i = 0; i < 13; ++i) { s = t + " " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   853
// => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   854
MethodHandle count = MethodHandles.identity(int.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   855
MethodHandle start = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0, int.class, String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   856
MethodHandle loop = MethodHandles.countedLoop(count, start, MH_step);  // (v, i, _, pre, _) -> pre + " " + v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   857
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke(13, "na", "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   858
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   859
        }}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   860
        // Variation #3:
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   861
        MH_step = LOOKUP.findStatic(THIS_CLASS, "step3", methodType(S, S, I, S));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   862
        {{
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   863
{} /// JAVADOC
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   864
// String s = "Lambdaman!", t = "na"; for (int i = 0; i < 13; ++i) { s = t + " " + s; } return s;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   865
// => a variation on a well known theme
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   866
MethodType loopType = methodType(String.class, String.class, int.class, String.class);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   867
MethodHandle count = MethodHandles.dropArgumentsToMatch(MethodHandles.identity(int.class),    0, loopType.parameterList(), 1);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   868
MethodHandle start = MethodHandles.dropArgumentsToMatch(MethodHandles.identity(String.class), 0, loopType.parameterList(), 2);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   869
MethodHandle body  = MethodHandles.dropArgumentsToMatch(MH_step,                              2, loopType.parameterList(), 0);
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   870
MethodHandle loop = MethodHandles.countedLoop(count, start, body);  // (v, i, pre, _, _) -> pre + " " + v
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   871
assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("na", 13, "Lambdaman!"));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   872
{}
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   873
        }}
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   874
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   875
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   876
    static List<String> reverseStep(List<String> r, String e) {
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   877
        r.add(0, e);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   878
        return r;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   879
    }
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   880
    static List<String> newArrayList() { return new ArrayList<>(); }
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   881
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   882
    @Test public void testIteratedLoop() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   883
        MethodHandle MH_newArrayList, MH_reverseStep;
41207
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   884
        Class<?> L = List.class, S = String.class;
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   885
        MH_newArrayList = LOOKUP.findStatic(THIS_CLASS, "newArrayList", methodType(L));
813a335bcb0c 8151179: address issues raised by JCK team on JEP 274 API
mhaupt
parents: 37347
diff changeset
   886
        MH_reverseStep = LOOKUP.findStatic(THIS_CLASS, "reverseStep", methodType(L, L, S));
34430
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   887
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   888
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   889
// reverse a list
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   890
MethodHandle loop = MethodHandles.iteratedLoop(null, MH_newArrayList, MH_reverseStep);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   891
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   892
List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   893
assertEquals(reversedList, (List<String>) loop.invoke(list));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   894
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   895
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   896
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   897
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   898
    @Test public void testFoldArguments3() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   899
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   900
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   901
MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   902
        "println", methodType(void.class, String.class))
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   903
        .bindTo(System.out);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   904
MethodHandle cat = lookup().findVirtual(String.class,
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   905
        "concat", methodType(String.class, String.class));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   906
assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   907
MethodHandle catTrace = foldArguments(cat, 1, trace);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   908
// also prints "jum":
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   909
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   910
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   911
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   912
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   913
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   914
    @Test public void testAsCollector2() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   915
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   916
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   917
StringWriter swr = new StringWriter();
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   918
MethodHandle swWrite = LOOKUP.findVirtual(StringWriter.class, "write", methodType(void.class, char[].class, int.class, int.class)).bindTo(swr);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   919
MethodHandle swWrite4 = swWrite.asCollector(0, char[].class, 4);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   920
swWrite4.invoke('A', 'B', 'C', 'D', 1, 2);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   921
assertEquals("BC", swr.toString());
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   922
swWrite4.invoke('P', 'Q', 'R', 'S', 0, 4);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   923
assertEquals("BCPQRS", swr.toString());
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   924
swWrite4.invoke('W', 'X', 'Y', 'Z', 3, 1);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   925
assertEquals("BCPQRSZ", swr.toString());
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   926
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   927
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   928
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   929
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   930
    @Test public void testAsSpreader2() throws Throwable {
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   931
        {{
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   932
{} /// JAVADOC
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   933
MethodHandle compare = LOOKUP.findStatic(Objects.class, "compare", methodType(int.class, Object.class, Object.class, Comparator.class));
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   934
MethodHandle compare2FromArray = compare.asSpreader(0, Object[].class, 2);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   935
Object[] ints = new Object[]{3, 9, 7, 7};
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   936
Comparator<Integer> cmp = (a, b) -> a - b;
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   937
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 0, 2), cmp) < 0);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   938
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 1, 3), cmp) > 0);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   939
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 2, 4), cmp) == 0);
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   940
{}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   941
        }}
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   942
    }
8e29bd2b5e0e 8143343: add JEP 274 Javadoc tests to JavaDocExamplesTest
mhaupt
parents: 32649
diff changeset
   943
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   944
    /* ---- TEMPLATE ----
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   945
    @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
   946
        {{
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   947
{} /// JAVADOC
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   948
{}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   949
            }}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   950
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9645
diff changeset
   951
    */
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   952
}