InvokeDynamicPrintArgs.java revision 3793
3243N/A/*
3793N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3243N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3243N/A *
3243N/A * This code is free software; you can redistribute it and/or modify it
3243N/A * under the terms of the GNU General Public License version 2 only, as
3243N/A * published by the Free Software Foundation.
3243N/A *
3243N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3243N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3243N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3243N/A * version 2 for more details (a copy is included in the LICENSE file that
3243N/A * accompanied this code).
3243N/A *
3243N/A * You should have received a copy of the GNU General Public License version
3243N/A * 2 along with this work; if not, write to the Free Software Foundation,
3243N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3243N/A *
3243N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3243N/A * or visit www.oracle.com if you need additional information or have any
3243N/A * questions.
3243N/A */
3243N/A
3243N/A/* @test
3243N/A * @summary smoke test for invokedynamic instructions
3528N/A * @build indify.Indify
3243N/A * @compile InvokeDynamicPrintArgs.java
3243N/A * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic
3243N/A * indify.Indify
3243N/A * --verify-specifier-count=3 --transitionalJSR292=false
3243N/A * --expand-properties --classpath ${test.classes}
3793N/A * --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output
3243N/A */
3243N/A
3793N/Apackage test.java.lang.invoke;
3528N/A
3528N/Aimport org.junit.Test;
3528N/A
3243N/Aimport java.util.*;
3243N/Aimport java.io.*;
3243N/A
3793N/Aimport java.lang.invoke.*;
3793N/Aimport static java.lang.invoke.MethodHandles.*;
3793N/Aimport static java.lang.invoke.MethodType.*;
3243N/A
3243N/Apublic class InvokeDynamicPrintArgs {
3243N/A public static void main(String... av) throws Throwable {
3243N/A if (av.length > 0) openBuf(); // --check-output mode
3243N/A System.out.println("Printing some argument lists, starting with a empty one:");
3243N/A INDY_nothing().invokeExact(); // BSM specifier #0 = {bsm}
3243N/A INDY_bar().invokeExact("bar arg", 1); // BSM specifier #1 = {bsm2, Void.class, "void type"}
3243N/A INDY_bar2().invokeExact("bar2 arg", 222); // BSM specifier #1 = (same)
3243N/A INDY_baz().invokeExact("baz arg", 2, 3.14); // BSM specifier #2 = {bsm2, 1234.5}
3243N/A INDY_foo().invokeExact("foo arg"); // BSM specifier #0 = (same)
3243N/A // Hence, BSM specifier count should be 3. See "--verify-specifier-count=3" above.
3243N/A System.out.println("Done printing argument lists.");
3243N/A closeBuf();
3243N/A }
3243N/A
3528N/A @Test
3528N/A public void testInvokeDynamicPrintArgs() throws IOException {
3528N/A System.err.println(System.getProperties());
3528N/A String testClassPath = System.getProperty("build.test.classes.dir");
3528N/A if (testClassPath == null) throw new RuntimeException();
3528N/A String[] args = new String[]{
3528N/A "--verify-specifier-count=3", "--transitionalJSR292=false",
3528N/A "--expand-properties", "--classpath", testClassPath,
3793N/A "--java", "test.java.lang.invoke.InvokeDynamicPrintArgs", "--check-output"
3528N/A };
3528N/A System.err.println("Indify: "+Arrays.toString(args));
3528N/A indify.Indify.main(args);
3528N/A }
3528N/A
3243N/A private static PrintStream oldOut;
3243N/A private static ByteArrayOutputStream buf;
3243N/A private static void openBuf() {
3243N/A oldOut = System.out;
3243N/A buf = new ByteArrayOutputStream();
3243N/A System.setOut(new PrintStream(buf));
3243N/A }
3243N/A private static void closeBuf() {
3243N/A if (buf == null) return;
3243N/A System.out.flush();
3243N/A System.setOut(oldOut);
3243N/A String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");
3243N/A for (String line : haveLines) System.out.println(line);
3243N/A Iterator<String> iter = Arrays.asList(haveLines).iterator();
3243N/A for (String want : EXPECT_OUTPUT) {
3243N/A String have = iter.hasNext() ? iter.next() : "[EOF]";
3243N/A if (want.equals(have)) continue;
3243N/A System.err.println("want line: "+want);
3243N/A System.err.println("have line: "+have);
3243N/A throw new AssertionError("unexpected output: "+have);
3243N/A }
3243N/A if (iter.hasNext())
3243N/A throw new AssertionError("unexpected output: "+iter.next());
3243N/A }
3243N/A private static final String[] EXPECT_OUTPUT = {
3243N/A "Printing some argument lists, starting with a empty one:",
3793N/A "[test.java.lang.invoke.InvokeDynamicPrintArgs, nothing, ()void][]",
3793N/A "[test.java.lang.invoke.InvokeDynamicPrintArgs, bar, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]",
3793N/A "[test.java.lang.invoke.InvokeDynamicPrintArgs, bar2, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]",
3793N/A "[test.java.lang.invoke.InvokeDynamicPrintArgs, baz, (String,int,double)void, 1234.5][baz arg, 2, 3.14]",
3793N/A "[test.java.lang.invoke.InvokeDynamicPrintArgs, foo, (String)void][foo arg]",
3243N/A "Done printing argument lists."
3243N/A };
3243N/A
3243N/A private static void printArgs(Object bsmInfo, Object... args) {
3243N/A System.out.println(bsmInfo+Arrays.deepToString(args));
3243N/A }
3243N/A private static MethodHandle MH_printArgs() throws ReflectiveOperationException {
3243N/A shouldNotCallThis();
3243N/A return lookup().findStatic(lookup().lookupClass(),
3243N/A "printArgs", methodType(void.class, Object.class, Object[].class));
3243N/A }
3243N/A
3243N/A private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
3243N/A // ignore caller and name, but match the type:
3243N/A Object bsmInfo = Arrays.asList(caller, name, type);
3247N/A return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type));
3243N/A }
3243N/A private static MethodType MT_bsm() {
3243N/A shouldNotCallThis();
3243N/A return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);
3243N/A }
3243N/A private static MethodHandle MH_bsm() throws ReflectiveOperationException {
3243N/A shouldNotCallThis();
3243N/A return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
3243N/A }
3243N/A
3528N/A private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws ReflectiveOperationException {
3243N/A // ignore caller and name, but match the type:
3243N/A List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type));
3528N/A bsmInfo.addAll(Arrays.asList((Object[])arg));
3247N/A return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type));
3243N/A }
3243N/A private static MethodType MT_bsm2() {
3243N/A shouldNotCallThis();
3528N/A return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object[].class);
3243N/A }
3243N/A private static MethodHandle MH_bsm2() throws ReflectiveOperationException {
3243N/A shouldNotCallThis();
3243N/A return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2());
3243N/A }
3243N/A
3243N/A private static MethodHandle INDY_nothing() throws Throwable {
3243N/A shouldNotCallThis();
3243N/A return ((CallSite) MH_bsm().invokeGeneric(lookup(),
3243N/A "nothing", methodType(void.class)
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_foo() throws Throwable {
3243N/A shouldNotCallThis();
3243N/A return ((CallSite) MH_bsm().invokeGeneric(lookup(),
3243N/A "foo", methodType(void.class, String.class)
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_bar() throws Throwable {
3243N/A shouldNotCallThis();
3243N/A return ((CallSite) MH_bsm2().invokeGeneric(lookup(),
3243N/A "bar", methodType(void.class, String.class, int.class)
3243N/A , new Object[] { Void.class, "void type!",
3243N/A 1, 234.5F, 67.5, (long)89 }
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_bar2() throws Throwable {
3243N/A shouldNotCallThis();
3243N/A return ((CallSite) MH_bsm2().invokeGeneric(lookup(),
3243N/A "bar2", methodType(void.class, String.class, int.class)
3243N/A , new Object[] { Void.class, "void type!",
3243N/A 1, 234.5F, 67.5, (long)89 }
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_baz() throws Throwable {
3243N/A shouldNotCallThis();
3243N/A return ((CallSite) MH_bsm2().invokeGeneric(lookup(),
3243N/A "baz", methodType(void.class, String.class, int.class, double.class)
3243N/A , 1234.5
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A
3243N/A private static void shouldNotCallThis() {
3243N/A // if this gets called, the transformation has not taken place
3243N/A if (System.getProperty("InvokeDynamicPrintArgs.allow-untransformed") != null) return;
3243N/A throw new AssertionError("this code should be statically transformed away by Indify");
3243N/A }
3243N/A}