jdk/test/java/dyn/InvokeDynamicPrintArgs.java
changeset 8345 9e2483e6cfab
parent 7562 a0ad195efe2c
equal deleted inserted replaced
8126:c5df55701e91 8345:9e2483e6cfab
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @summary smoke test for invokedynamic instructions
    25  * @summary smoke test for invokedynamic instructions
    26  * @library indify
    26  * @build indify.Indify
    27  * @compile InvokeDynamicPrintArgs.java
    27  * @compile InvokeDynamicPrintArgs.java
    28  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic
    28  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic
    29  *      indify.Indify
    29  *      indify.Indify
    30  *      --verify-specifier-count=3 --transitionalJSR292=false
    30  *      --verify-specifier-count=3 --transitionalJSR292=false
    31  *      --expand-properties --classpath ${test.classes}
    31  *      --expand-properties --classpath ${test.classes}
    32  *      --java InvokeDynamicPrintArgs --check-output
    32  *      --java test.java.dyn.InvokeDynamicPrintArgs --check-output
    33  */
    33  */
       
    34 
       
    35 package test.java.dyn;
       
    36 
       
    37 import org.junit.Test;
    34 
    38 
    35 import java.util.*;
    39 import java.util.*;
    36 import java.io.*;
    40 import java.io.*;
    37 
    41 
    38 import java.dyn.*;
    42 import java.dyn.*;
    49         INDY_baz().invokeExact("baz arg", 2, 3.14);   // BSM specifier #2 = {bsm2, 1234.5}
    53         INDY_baz().invokeExact("baz arg", 2, 3.14);   // BSM specifier #2 = {bsm2, 1234.5}
    50         INDY_foo().invokeExact("foo arg");            // BSM specifier #0 = (same)
    54         INDY_foo().invokeExact("foo arg");            // BSM specifier #0 = (same)
    51         // Hence, BSM specifier count should be 3.  See "--verify-specifier-count=3" above.
    55         // Hence, BSM specifier count should be 3.  See "--verify-specifier-count=3" above.
    52         System.out.println("Done printing argument lists.");
    56         System.out.println("Done printing argument lists.");
    53         closeBuf();
    57         closeBuf();
       
    58     }
       
    59 
       
    60     @Test
       
    61     public void testInvokeDynamicPrintArgs() throws IOException {
       
    62         System.err.println(System.getProperties());
       
    63         String testClassPath = System.getProperty("build.test.classes.dir");
       
    64         if (testClassPath == null)  throw new RuntimeException();
       
    65         String[] args = new String[]{
       
    66             "--verify-specifier-count=3", "--transitionalJSR292=false",
       
    67             "--expand-properties", "--classpath", testClassPath,
       
    68             "--java", "test.java.dyn.InvokeDynamicPrintArgs", "--check-output"
       
    69         };
       
    70         System.err.println("Indify: "+Arrays.toString(args));
       
    71         indify.Indify.main(args);
    54     }
    72     }
    55 
    73 
    56     private static PrintStream oldOut;
    74     private static PrintStream oldOut;
    57     private static ByteArrayOutputStream buf;
    75     private static ByteArrayOutputStream buf;
    58     private static void openBuf() {
    76     private static void openBuf() {
    77         if (iter.hasNext())
    95         if (iter.hasNext())
    78             throw new AssertionError("unexpected output: "+iter.next());
    96             throw new AssertionError("unexpected output: "+iter.next());
    79     }
    97     }
    80     private static final String[] EXPECT_OUTPUT = {
    98     private static final String[] EXPECT_OUTPUT = {
    81         "Printing some argument lists, starting with a empty one:",
    99         "Printing some argument lists, starting with a empty one:",
    82         "[InvokeDynamicPrintArgs, nothing, ()void][]",
   100         "[test.java.dyn.InvokeDynamicPrintArgs, nothing, ()void][]",
    83         "[InvokeDynamicPrintArgs, bar, (java.lang.String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]",
   101         "[test.java.dyn.InvokeDynamicPrintArgs, bar, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar arg, 1]",
    84         "[InvokeDynamicPrintArgs, bar2, (java.lang.String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]",
   102         "[test.java.dyn.InvokeDynamicPrintArgs, bar2, (String,int)void, class java.lang.Void, void type!, 1, 234.5, 67.5, 89][bar2 arg, 222]",
    85         "[InvokeDynamicPrintArgs, baz, (java.lang.String,int,double)void, 1234.5][baz arg, 2, 3.14]",
   103         "[test.java.dyn.InvokeDynamicPrintArgs, baz, (String,int,double)void, 1234.5][baz arg, 2, 3.14]",
    86         "[InvokeDynamicPrintArgs, foo, (java.lang.String)void][foo arg]",
   104         "[test.java.dyn.InvokeDynamicPrintArgs, foo, (String)void][foo arg]",
    87         "Done printing argument lists."
   105         "Done printing argument lists."
    88     };
   106     };
    89 
   107 
    90     private static void printArgs(Object bsmInfo, Object... args) {
   108     private static void printArgs(Object bsmInfo, Object... args) {
    91         System.out.println(bsmInfo+Arrays.deepToString(args));
   109         System.out.println(bsmInfo+Arrays.deepToString(args));
   108     private static MethodHandle MH_bsm() throws ReflectiveOperationException {
   126     private static MethodHandle MH_bsm() throws ReflectiveOperationException {
   109         shouldNotCallThis();
   127         shouldNotCallThis();
   110         return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
   128         return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
   111     }
   129     }
   112 
   130 
   113     private static CallSite bsm2(Lookup caller, String name, MethodType type, Object arg) throws ReflectiveOperationException {
   131     private static CallSite bsm2(Lookup caller, String name, MethodType type, Object... arg) throws ReflectiveOperationException {
   114         // ignore caller and name, but match the type:
   132         // ignore caller and name, but match the type:
   115         List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type));
   133         List<Object> bsmInfo = new ArrayList<>(Arrays.asList(caller, name, type));
   116         if (arg instanceof Object[])
   134         bsmInfo.addAll(Arrays.asList((Object[])arg));
   117             bsmInfo.addAll(Arrays.asList((Object[])arg));
       
   118         else
       
   119             bsmInfo.add(arg);
       
   120         return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type));
   135         return new ConstantCallSite(MH_printArgs().bindTo(bsmInfo).asCollector(Object[].class, type.parameterCount()).asType(type));
   121     }
   136     }
   122     private static MethodType MT_bsm2() {
   137     private static MethodType MT_bsm2() {
   123         shouldNotCallThis();
   138         shouldNotCallThis();
   124         return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object.class);
   139         return methodType(CallSite.class, Lookup.class, String.class, MethodType.class, Object[].class);
   125     }
   140     }
   126     private static MethodHandle MH_bsm2() throws ReflectiveOperationException {
   141     private static MethodHandle MH_bsm2() throws ReflectiveOperationException {
   127         shouldNotCallThis();
   142         shouldNotCallThis();
   128         return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2());
   143         return lookup().findStatic(lookup().lookupClass(), "bsm2", MT_bsm2());
   129     }
   144     }