jdk/test/java/lang/invoke/InvokeDynamicPrintArgs.java
changeset 9780 6fc3b49cfee4
parent 9752 88ab34b6da6d
child 11007 09d3dddac781
equal deleted inserted replaced
9779:e3a8fbcb21fb 9780:6fc3b49cfee4
    28  * @run main/othervm
    28  * @run main/othervm
    29  *      indify.Indify
    29  *      indify.Indify
    30  *      --verify-specifier-count=3
    30  *      --verify-specifier-count=3
    31  *      --expand-properties --classpath ${test.classes}
    31  *      --expand-properties --classpath ${test.classes}
    32  *      --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output
    32  *      --java test.java.lang.invoke.InvokeDynamicPrintArgs --check-output
       
    33  * @run main/othervm
       
    34  *      indify.Indify
       
    35  *      --expand-properties --classpath ${test.classes}
       
    36  *      --java test.java.lang.invoke.InvokeDynamicPrintArgs --security-manager
    33  */
    37  */
    34 
    38 
    35 package test.java.lang.invoke;
    39 package test.java.lang.invoke;
    36 
    40 
    37 import org.junit.Test;
    41 import org.junit.Test;
    43 import static java.lang.invoke.MethodHandles.*;
    47 import static java.lang.invoke.MethodHandles.*;
    44 import static java.lang.invoke.MethodType.*;
    48 import static java.lang.invoke.MethodType.*;
    45 
    49 
    46 public class InvokeDynamicPrintArgs {
    50 public class InvokeDynamicPrintArgs {
    47     public static void main(String... av) throws Throwable {
    51     public static void main(String... av) throws Throwable {
    48         if (av.length > 0)  openBuf();  // --check-output mode
    52         if (av.length > 0 && av[0].equals("--check-output"))  openBuf();
       
    53         if (av.length > 0 && av[0].equals("--security-manager"))  setSM();
    49         System.out.println("Printing some argument lists, starting with a empty one:");
    54         System.out.println("Printing some argument lists, starting with a empty one:");
    50         INDY_nothing().invokeExact();                 // BSM specifier #0 = {bsm}
    55         INDY_nothing().invokeExact();                 // BSM specifier #0 = {bsm}
    51         INDY_bar().invokeExact("bar arg", 1);         // BSM specifier #1 = {bsm2, Void.class, "void type"}
    56         INDY_bar().invokeExact("bar arg", 1);         // BSM specifier #1 = {bsm2, Void.class, "void type"}
    52         INDY_bar2().invokeExact("bar2 arg", 222);     // BSM specifier #1 = (same)
    57         INDY_bar2().invokeExact("bar2 arg", 222);     // BSM specifier #1 = (same)
    53         INDY_baz().invokeExact("baz arg", 2, 3.14);   // BSM specifier #2 = {bsm2, 1234.5}
    58         INDY_baz().invokeExact("baz arg", 2, 3.14);   // BSM specifier #2 = {bsm2, 1234.5}
    54         INDY_foo().invokeExact("foo arg");            // BSM specifier #0 = (same)
    59         INDY_foo().invokeExact("foo arg");            // BSM specifier #0 = (same)
    55         // Hence, BSM specifier count should be 3.  See "--verify-specifier-count=3" above.
    60         // Hence, BSM specifier count should be 3.  See "--verify-specifier-count=3" above.
    56         System.out.println("Done printing argument lists.");
    61         System.out.println("Done printing argument lists.");
    57         closeBuf();
    62         closeBuf();
       
    63         checkConstantRefs();
       
    64     }
       
    65 
       
    66     private static void checkConstantRefs() throws Throwable {
       
    67         // check some constant references:
       
    68         assertEquals(MT_bsm(), MH_bsm().type());
       
    69         assertEquals(MT_bsm2(), MH_bsm2().type());
       
    70         try {
       
    71             assertEquals(MT_bsm(), non_MH_bsm().type());
       
    72             // if SM is installed, must throw before this point
       
    73             assertEquals(false, System.getSecurityManager() != null);
       
    74         } catch (SecurityException ex) {
       
    75             // if SM is installed, must throw to this point
       
    76             assertEquals(true, System.getSecurityManager() != null);
       
    77         }
       
    78     }
       
    79     private static void assertEquals(Object exp, Object act) {
       
    80         if (exp == act || (exp != null && exp.equals(act)))  return;
       
    81         throw new AssertionError("not equal: "+exp+", "+act);
       
    82     }
       
    83 
       
    84     private static void setSM() {
       
    85         // Test for severe security manager interactions (7050328).
       
    86         class SM extends SecurityManager {
       
    87             public void checkPackageAccess(String pkg) {
       
    88                 if (pkg.startsWith("test."))
       
    89                     throw new SecurityException("checkPackageAccess "+pkg);
       
    90             }
       
    91             public void checkMemberAccess(Class<?> clazz, int which) {
       
    92                 if (clazz == InvokeDynamicPrintArgs.class)
       
    93                     throw new SecurityException("checkMemberAccess "+clazz.getName()+" #"+which);
       
    94             }
       
    95             // allow these others:
       
    96             public void checkPermission(java.security.Permission perm) {
       
    97             }
       
    98         }
       
    99         System.setSecurityManager(new SM());
    58     }
   100     }
    59 
   101 
    60     @Test
   102     @Test
    61     public void testInvokeDynamicPrintArgs() throws IOException {
   103     public void testInvokeDynamicPrintArgs() throws IOException {
    62         System.err.println(System.getProperties());
   104         System.err.println(System.getProperties());
   128     }
   170     }
   129     private static MethodHandle MH_bsm() throws ReflectiveOperationException {
   171     private static MethodHandle MH_bsm() throws ReflectiveOperationException {
   130         shouldNotCallThis();
   172         shouldNotCallThis();
   131         return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
   173         return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
   132     }
   174     }
       
   175     private static MethodHandle non_MH_bsm() throws ReflectiveOperationException {
       
   176         return lookup().findStatic(lookup().lookupClass(), "bsm", MT_bsm());
       
   177     }
   133 
   178 
   134     /* Example of a constant call site with user-data.
   179     /* Example of a constant call site with user-data.
   135      * In this case, the user data is exactly the BSM data.
   180      * In this case, the user data is exactly the BSM data.
   136      * Note that a CCS with user data must use the "hooked" constructor
   181      * Note that a CCS with user data must use the "hooked" constructor
   137      * to bind the CCS itself into the resulting target.
   182      * to bind the CCS itself into the resulting target.