jdk/test/java/lang/invoke/InvokeDynamicPrintArgs.java
author jrose
Thu, 26 May 2011 17:37:36 -0700
changeset 9752 88ab34b6da6d
parent 9033 a88f5656f05d
child 9780 6fc3b49cfee4
permissions -rw-r--r--
7032323: code changes for JSR 292 EG adjustments to API, through Public Review Summary: API code changes and javadoc changes following JSR 292 Public Review comments, through PFD Reviewed-by: never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     1
/*
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     4
 *
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     8
 *
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    13
 * accompanied this code).
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    14
 *
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    18
 *
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    21
 * questions.
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    22
 */
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    23
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    24
/* @test
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    25
 * @summary smoke test for invokedynamic instructions
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    26
 * @build indify.Indify
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    27
 * @compile InvokeDynamicPrintArgs.java
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    28
 * @run main/othervm
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    29
 *      indify.Indify
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    30
 *      --verify-specifier-count=3
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    31
 *      --expand-properties --classpath ${test.classes}
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
    32
 *      --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    33
 */
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    34
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
    35
package test.java.lang.invoke;
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    36
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    37
import org.junit.Test;
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    38
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    39
import java.util.*;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    40
import java.io.*;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    41
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
    42
import java.lang.invoke.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
    43
import static java.lang.invoke.MethodHandles.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
    44
import static java.lang.invoke.MethodType.*;
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    45
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    46
public class InvokeDynamicPrintArgs {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    47
    public static void main(String... av) throws Throwable {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    48
        if (av.length > 0)  openBuf();  // --check-output mode
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    49
        System.out.println("Printing some argument lists, starting with a empty one:");
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    50
        INDY_nothing().invokeExact();                 // BSM specifier #0 = {bsm}
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    51
        INDY_bar().invokeExact("bar arg", 1);         // BSM specifier #1 = {bsm2, Void.class, "void type"}
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    52
        INDY_bar2().invokeExact("bar2 arg", 222);     // BSM specifier #1 = (same)
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    53
        INDY_baz().invokeExact("baz arg", 2, 3.14);   // BSM specifier #2 = {bsm2, 1234.5}
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    54
        INDY_foo().invokeExact("foo arg");            // BSM specifier #0 = (same)
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    55
        // Hence, BSM specifier count should be 3.  See "--verify-specifier-count=3" above.
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    56
        System.out.println("Done printing argument lists.");
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    57
        closeBuf();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    58
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    59
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    60
    @Test
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    61
    public void testInvokeDynamicPrintArgs() throws IOException {
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    62
        System.err.println(System.getProperties());
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    63
        String testClassPath = System.getProperty("build.test.classes.dir");
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    64
        if (testClassPath == null)  throw new RuntimeException();
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    65
        String[] args = new String[]{
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    66
            "--verify-specifier-count=3",
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
    67
            "--verbose",
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    68
            "--expand-properties", "--classpath", testClassPath,
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
    69
            "--java", "test.java.lang.invoke.InvokeDynamicPrintArgs", "--check-output"
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    70
        };
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    71
        System.err.println("Indify: "+Arrays.toString(args));
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    72
        indify.Indify.main(args);
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    73
    }
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
    74
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    75
    private static PrintStream oldOut;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    76
    private static ByteArrayOutputStream buf;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    77
    private static void openBuf() {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    78
        oldOut = System.out;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    79
        buf = new ByteArrayOutputStream();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    80
        System.setOut(new PrintStream(buf));
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    81
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    82
    private static void closeBuf() {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    83
        if (buf == null)  return;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    84
        System.out.flush();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    85
        System.setOut(oldOut);
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    86
        String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    87
        for (String line : haveLines)  System.out.println(line);
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    88
        Iterator<String> iter = Arrays.asList(haveLines).iterator();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    89
        for (String want : EXPECT_OUTPUT) {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    90
            String have = iter.hasNext() ? iter.next() : "[EOF]";
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    91
            if (want.equals(have))  continue;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    92
            System.err.println("want line: "+want);
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    93
            System.err.println("have line: "+have);
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    94
            throw new AssertionError("unexpected output: "+have);
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    95
        }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    96
        if (iter.hasNext())
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    97
            throw new AssertionError("unexpected output: "+iter.next());
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    98
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
    99
    private static final String[] EXPECT_OUTPUT = {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   100
        "Printing some argument lists, starting with a empty one:",
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
   101
        "[test.java.lang.invoke.InvokeDynamicPrintArgs, nothing, ()void][]",
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
   102
        "[test.java.lang.invoke.InvokeDynamicPrintArgs, bar, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]",
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
   103
        "[test.java.lang.invoke.InvokeDynamicPrintArgs, bar2, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]",
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
   104
        "[test.java.lang.invoke.InvokeDynamicPrintArgs, baz, (String,int,double)void, 1234.5][baz arg, 2, 3.14]",
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8345
diff changeset
   105
        "[test.java.lang.invoke.InvokeDynamicPrintArgs, foo, (String)void][foo arg]",
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   106
        "Done printing argument lists."
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   107
    };
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   108
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   109
    private static boolean doPrint = true;
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   110
    private static void printArgs(Object bsmInfo, Object... args) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   111
        String message = bsmInfo+Arrays.deepToString(args);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   112
        if (doPrint)  System.out.println(message);
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   113
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   114
    private static MethodHandle MH_printArgs() throws ReflectiveOperationException {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   115
        shouldNotCallThis();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   116
        return lookup().findStatic(lookup().lookupClass(),
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   117
                                   "printArgs", methodType(void.class, Object.class, Object[].class));
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   118
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   119
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   120
    private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   121
        // ignore caller and name, but match the type:
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   122
        Object bsmInfo = Arrays.asList(caller, name, type);
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7558
diff changeset
   123
        return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type));
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   124
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   125
    private static MethodType MT_bsm() {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   126
        shouldNotCallThis();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   127
        return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   128
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   129
    private static MethodHandle MH_bsm() throws ReflectiveOperationException {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   130
        shouldNotCallThis();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   131
        return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   132
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   133
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   134
    /* Example of a constant call site with user-data.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   135
     * In this case, the user data is exactly the BSM data.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   136
     * Note that a CCS with user data must use the "hooked" constructor
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   137
     * to bind the CCS itself into the resulting target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   138
     * A normal constructor would not allow a circular relation
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   139
     * between the CCS and its target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   140
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   141
    public static class PrintingCallSite extends ConstantCallSite {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   142
        final Lookup caller;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   143
        final String name;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   144
        final Object[] staticArgs;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   145
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   146
        PrintingCallSite(Lookup caller, String name, MethodType type, Object... staticArgs) throws Throwable {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   147
            super(type, MH_createTarget());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   148
            this.caller = caller;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   149
            this.name = name;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   150
            this.staticArgs = staticArgs;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   151
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   152
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   153
        public MethodHandle createTarget() {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   154
            try {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   155
                return lookup().bind(this, "runTarget", genericMethodType(0, true)).asType(type());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   156
            } catch (ReflectiveOperationException ex) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   157
                throw new RuntimeException(ex);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   158
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   159
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   160
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   161
        public Object runTarget(Object... dynamicArgs) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   162
            List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type()));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   163
            bsmInfo.addAll(Arrays.asList(staticArgs));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   164
            printArgs(bsmInfo, dynamicArgs);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   165
            return null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   166
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   167
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   168
        private static MethodHandle MH_createTarget() throws ReflectiveOperationException {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   169
            shouldNotCallThis();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   170
            return lookup().findVirtual(lookup().lookupClass(), "createTarget", methodType(MethodHandle.class));
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   171
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   172
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   173
    private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws Throwable {
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   174
        // ignore caller and name, but match the type:
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   175
        return new PrintingCallSite(caller, name, type, arg);
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   176
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   177
    private static MethodType MT_bsm2() {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   178
        shouldNotCallThis();
8345
9e2483e6cfab 7013417: JSR 292 needs to support variadic method handle calls
jrose
parents: 7562
diff changeset
   179
        return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object[].class);
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   180
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   181
    private static MethodHandle MH_bsm2() throws ReflectiveOperationException {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   182
        shouldNotCallThis();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   183
        return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2());
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   184
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   185
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   186
    private static MethodHandle INDY_nothing() throws Throwable {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   187
        shouldNotCallThis();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   188
        return ((CallSite) MH_bsm().invoke(lookup(),
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   189
                                                  "nothing", methodType(void.class)
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   190
                                                  )).dynamicInvoker();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   191
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   192
    private static MethodHandle INDY_foo() throws Throwable {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   193
        shouldNotCallThis();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   194
        return ((CallSite) MH_bsm().invoke(lookup(),
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   195
                                                  "foo", methodType(void.class, String.class)
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   196
                                                  )).dynamicInvoker();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   197
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   198
    private static MethodHandle INDY_bar() throws Throwable {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   199
        shouldNotCallThis();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   200
        return ((CallSite) MH_bsm2().invoke(lookup(),
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   201
                                                  "bar", methodType(void.class, String.class, int.class)
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
   202
                                                  , Void.class, "void type!", 1, 234.5F, 67.5, (long)89
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   203
                                                  )).dynamicInvoker();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   204
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   205
    private static MethodHandle INDY_bar2() throws Throwable {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   206
        shouldNotCallThis();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   207
        return ((CallSite) MH_bsm2().invoke(lookup(),
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   208
                                                  "bar2", methodType(void.class, String.class, int.class)
9033
a88f5656f05d 6817525: turn on method handle functionality by default for JSR 292
jrose
parents: 8822
diff changeset
   209
                                                  , Void.class, "void type!", 1, 234.5F, 67.5, (long)89
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   210
                                                  )).dynamicInvoker();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   211
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   212
    private static MethodHandle INDY_baz() throws Throwable {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   213
        shouldNotCallThis();
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9033
diff changeset
   214
        return ((CallSite) MH_bsm2().invoke(lookup(),
7558
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   215
                                                  "baz", methodType(void.class, String.class, int.class, double.class)
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   216
                                                  , 1234.5
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   217
                                                  )).dynamicInvoker();
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   218
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   219
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   220
    private static void shouldNotCallThis() {
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   221
        // if this gets called, the transformation has not taken place
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   222
        if (System.getProperty("InvokeDynamicPrintArgs.allow-untransformed") != null)  return;
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   223
        throw new AssertionError("this code should be statically transformed away by Indify");
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   224
    }
e7a391a3bcfe 7001423: JSR 292 bytecode enhancements need unit tests
jrose
parents:
diff changeset
   225
}