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
3907N/A * @run main/othervm
3243N/A * indify.Indify
3907N/A * --verify-specifier-count=3
3243N/A * --expand-properties --classpath ${test.classes}
3793N/A * --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output
4278N/A * @run main/othervm
4278N/A * indify.Indify
4278N/A * --expand-properties --classpath ${test.classes}
4278N/A * --java test.java.lang.invoke.InvokeDynamicPrintArgs --security-manager
3243N/A */
3243N/A
3793N/Apackage test.java.lang.invoke;
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 {
4278N/A if (av.length > 0 && av[0].equals("--check-output")) openBuf();
4278N/A if (av.length > 0 && av[0].equals("--security-manager")) setSM();
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();
4278N/A checkConstantRefs();
4278N/A }
4278N/A
4278N/A private static void checkConstantRefs() throws Throwable {
4278N/A // check some constant references:
4278N/A assertEquals(MT_bsm(), MH_bsm().type());
4278N/A assertEquals(MT_bsm2(), MH_bsm2().type());
4278N/A try {
4278N/A assertEquals(MT_bsm(), non_MH_bsm().type());
4278N/A // if SM is installed, must throw before this point
4278N/A assertEquals(false, System.getSecurityManager() != null);
4278N/A } catch (SecurityException ex) {
4278N/A // if SM is installed, must throw to this point
4278N/A assertEquals(true, System.getSecurityManager() != null);
4278N/A }
4278N/A }
4278N/A private static void assertEquals(Object exp, Object act) {
4278N/A if (exp == act || (exp != null && exp.equals(act))) return;
4278N/A throw new AssertionError("not equal: "+exp+", "+act);
4278N/A }
4278N/A
4278N/A private static void setSM() {
4278N/A // Test for severe security manager interactions (7050328).
4278N/A class SM extends SecurityManager {
4278N/A public void checkPackageAccess(String pkg) {
4278N/A if (pkg.startsWith("test."))
4278N/A throw new SecurityException("checkPackageAccess "+pkg);
4278N/A }
4278N/A public void checkMemberAccess(Class<?> clazz, int which) {
4278N/A if (clazz == InvokeDynamicPrintArgs.class)
4278N/A throw new SecurityException("checkMemberAccess "+clazz.getName()+" #"+which);
4278N/A }
4278N/A // allow these others:
4278N/A public void checkPermission(java.security.Permission perm) {
4278N/A }
4278N/A }
4278N/A System.setSecurityManager(new SM());
3243N/A }
3243N/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
4250N/A private static boolean doPrint = true;
3243N/A private static void printArgs(Object bsmInfo, Object... args) {
4250N/A String message = bsmInfo+Arrays.deepToString(args);
4250N/A if (doPrint) System.out.println(message);
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 }
4278N/A private static MethodHandle non_MH_bsm() throws ReflectiveOperationException {
4278N/A return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
4278N/A }
3243N/A
4250N/A /* Example of a constant call site with user-data.
4250N/A * In this case, the user data is exactly the BSM data.
4250N/A * Note that a CCS with user data must use the "hooked" constructor
4250N/A * to bind the CCS itself into the resulting target.
4250N/A * A normal constructor would not allow a circular relation
4250N/A * between the CCS and its target.
4250N/A */
4250N/A public static class PrintingCallSite extends ConstantCallSite {
4250N/A final Lookup caller;
4250N/A final String name;
4250N/A final Object[] staticArgs;
4250N/A
4250N/A PrintingCallSite(Lookup caller, String name, MethodType type, Object... staticArgs) throws Throwable {
4250N/A super(type, MH_createTarget());
4250N/A this.caller = caller;
4250N/A this.name = name;
4250N/A this.staticArgs = staticArgs;
4250N/A }
4250N/A
4250N/A public MethodHandle createTarget() {
4250N/A try {
4250N/A return lookup().bind(this, "runTarget", genericMethodType(0, true)).asType(type());
4250N/A } catch (ReflectiveOperationException ex) {
4250N/A throw new RuntimeException(ex);
4250N/A }
4250N/A }
4250N/A
4250N/A public Object runTarget(Object... dynamicArgs) {
4250N/A List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type()));
4250N/A bsmInfo.addAll(Arrays.asList(staticArgs));
4250N/A printArgs(bsmInfo, dynamicArgs);
4250N/A return null;
4250N/A }
4250N/A
4250N/A private static MethodHandle MH_createTarget() throws ReflectiveOperationException {
4250N/A shouldNotCallThis();
4250N/A return lookup().findVirtual(lookup().lookupClass(), "createTarget", methodType(MethodHandle.class));
4250N/A }
4250N/A }
4250N/A private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws Throwable {
3243N/A // ignore caller and name, but match the type:
4250N/A return new PrintingCallSite(caller, name, type, arg);
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();
4250N/A return ((CallSite) MH_bsm().invoke(lookup(),
3243N/A "nothing", methodType(void.class)
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_foo() throws Throwable {
3243N/A shouldNotCallThis();
4250N/A return ((CallSite) MH_bsm().invoke(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();
4250N/A return ((CallSite) MH_bsm2().invoke(lookup(),
3243N/A "bar", methodType(void.class, String.class, int.class)
3907N/A , Void.class, "void type!", 1, 234.5F, 67.5, (long)89
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_bar2() throws Throwable {
3243N/A shouldNotCallThis();
4250N/A return ((CallSite) MH_bsm2().invoke(lookup(),
3243N/A "bar2", methodType(void.class, String.class, int.class)
3907N/A , Void.class, "void type!", 1, 234.5F, 67.5, (long)89
3243N/A )).dynamicInvoker();
3243N/A }
3243N/A private static MethodHandle INDY_baz() throws Throwable {
3243N/A shouldNotCallThis();
4250N/A return ((CallSite) MH_bsm2().invoke(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}