1 /*
   2  * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8022066
  26  * @summary smoke test for method handle constants
  27  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
  28  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
  29  * @run main test.java.lang.invoke.MethodHandleConstants --check-output
  30  * @run main/othervm test.java.lang.invoke.MethodHandleConstants --security-manager
  31  */
  32 
  33 package test.java.lang.invoke;
  34 
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.PrintStream;
  37 import java.lang.invoke.MethodHandle;
  38 import java.lang.invoke.MethodHandleInfo;
  39 import java.lang.invoke.MethodHandles;
  40 import java.lang.invoke.MethodType;
  41 import java.security.CodeSource;
  42 import java.security.Permission;
  43 import java.security.PermissionCollection;
  44 import java.security.Permissions;
  45 import java.security.Policy;
  46 import java.security.ProtectionDomain;
  47 import java.util.Arrays;
  48 import java.util.Iterator;
  49 
  50 import static java.lang.invoke.MethodHandles.lookup;
  51 import static java.lang.invoke.MethodType.methodType;
  52 import static test.java.lang.invoke.lib.InstructionHelper.ldcMethodHandle;
  53 
  54 public class MethodHandleConstants {
  55     private static final MethodHandle MH_String_replace_C2;
  56     private static final MethodHandle MH_MethodHandle_invokeExact_SC2;
  57     private static final MethodHandle MH_MethodHandle_invoke_SC2;
  58     private static final MethodHandle MH_Class_forName_S;
  59     private static final MethodHandle MH_Class_forName_SbCL;
  60     static {
  61         try {
  62             MethodHandles.Lookup l = lookup();
  63 
  64             MH_String_replace_C2 = ldcMethodHandle(l,
  65                     MethodHandleInfo.REF_invokeVirtual, String.class, "replace",
  66                     methodType(String.class, char.class, char.class));
  67             MH_MethodHandle_invokeExact_SC2 = ldcMethodHandle(l,
  68                     MethodHandleInfo.REF_invokeVirtual, MethodHandle.class, "invokeExact",
  69                     methodType(String.class, String.class, char.class, char.class));
  70             MH_MethodHandle_invoke_SC2 = ldcMethodHandle(l,
  71                     MethodHandleInfo.REF_invokeVirtual, MethodHandle.class, "invoke",
  72                     methodType(String.class, String.class, char.class, char.class));
  73             MH_Class_forName_S = ldcMethodHandle(l,
  74                     MethodHandleInfo.REF_invokeStatic, Class.class, "forName",
  75                     methodType(Class.class, String.class));
  76             MH_Class_forName_SbCL = ldcMethodHandle(l,
  77                     MethodHandleInfo.REF_invokeStatic, Class.class, "forName",
  78                     methodType(Class.class, String.class, boolean.class, ClassLoader.class));
  79         }
  80         catch (Exception ex) {
  81             ex.printStackTrace();
  82             throw new InternalError(ex);
  83         }
  84     }
  85 
  86     public static void main(String... av) throws Throwable {
  87         if (av.length > 0 && av[0].equals("--check-output"))  openBuf();
  88         if (av.length > 0 && av[0].equals("--security-manager"))  setSM();
  89         System.out.println("Obtaining method handle constants:");
  90         testCase((MethodHandle) MH_String_replace_C2.invokeExact(), String.class, "replace", String.class, String.class, char.class, char.class);
  91         testCase((MethodHandle) MH_MethodHandle_invokeExact_SC2.invokeExact(), MethodHandle.class, "invokeExact", String.class, MethodHandle.class, String.class, char.class, char.class);
  92         testCase((MethodHandle) MH_MethodHandle_invoke_SC2.invokeExact(), MethodHandle.class, "invoke", String.class, MethodHandle.class, String.class, char.class, char.class);
  93         testCase((MethodHandle) MH_Class_forName_S.invokeExact(), Class.class, "forName", Class.class, String.class);
  94         testCase((MethodHandle) MH_Class_forName_SbCL.invokeExact(), Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class);
  95         System.out.println("Done.");
  96         closeBuf();
  97     }
  98 
  99     private static void testCase(MethodHandle mh, Class<?> defc, String name, Class<?> rtype, Class<?>... ptypes) throws Throwable {
 100         System.out.println(mh);
 101         // we include defc, because we assume it is a non-static MH:
 102         MethodType mt = methodType(rtype, ptypes);
 103         assertEquals(mh.type(), mt);
 104         // FIXME: Use revealDirect to find out more
 105     }
 106     private static void assertEquals(Object exp, Object act) {
 107         if (exp == act || (exp != null && exp.equals(act)))  return;
 108         throw new AssertionError("not equal: "+exp+", "+act);
 109     }
 110 
 111     private static void setSM() {
 112         Policy.setPolicy(new TestPolicy());
 113         System.setSecurityManager(new SecurityManager());
 114     }
 115 
 116     private static PrintStream oldOut;
 117     private static ByteArrayOutputStream buf;
 118     private static void openBuf() {
 119         oldOut = System.out;
 120         buf = new ByteArrayOutputStream();
 121         System.setOut(new PrintStream(buf));
 122     }
 123     private static void closeBuf() {
 124         if (buf == null)  return;
 125         System.out.flush();
 126         System.setOut(oldOut);
 127         String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");
 128         for (String line : haveLines)  System.out.println(line);
 129         Iterator<String> iter = Arrays.asList(haveLines).iterator();
 130         for (String want : EXPECT_OUTPUT) {
 131             String have = iter.hasNext() ? iter.next() : "[EOF]";
 132             if (want.equals(have))  continue;
 133             System.err.println("want line: "+want);
 134             System.err.println("have line: "+have);
 135             throw new AssertionError("unexpected output: "+have);
 136         }
 137         if (iter.hasNext())
 138             throw new AssertionError("unexpected output: "+iter.next());
 139     }
 140     private static final String[] EXPECT_OUTPUT = {
 141         "Obtaining method handle constants:",
 142         "MethodHandle(String,char,char)String",
 143         "MethodHandle(MethodHandle,String,char,char)String",
 144         "MethodHandle(MethodHandle,String,char,char)String",
 145         "MethodHandle(String)Class",
 146         "MethodHandle(String,boolean,ClassLoader)Class",
 147         "Done."
 148     };
 149 
 150     static class TestPolicy extends Policy {
 151         final PermissionCollection permissions = new Permissions();
 152         TestPolicy() {
 153             permissions.add(new java.io.FilePermission("<<ALL FILES>>", "read"));
 154         }
 155         public PermissionCollection getPermissions(ProtectionDomain domain) {
 156             return permissions;
 157         }
 158 
 159         public PermissionCollection getPermissions(CodeSource codesource) {
 160             return permissions;
 161         }
 162 
 163         public boolean implies(ProtectionDomain domain, Permission perm) {
 164             return permissions.implies(perm);
 165         }
 166     }
 167 }