< prev index next >

test/jdk/java/lang/invoke/MethodHandleConstants.java

Print this page


   1 /*
   2  * Copyright (c) 2010, 2013, 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  * @build indify.Indify
  28  * @compile MethodHandleConstants.java
  29  * @run main/othervm
  30  *      indify.Indify
  31  *      --verify-specifier-count=4
  32  *      --expand-properties --classpath ${test.classes}
  33  *      --java test.java.lang.invoke.MethodHandleConstants --check-output
  34  * @run main/othervm
  35  *      indify.Indify
  36  *      --expand-properties --classpath ${test.classes}
  37  *      --java test.java.lang.invoke.MethodHandleConstants --security-manager
  38  */
  39 
  40 package test.java.lang.invoke;
  41 
  42 import java.util.*;
  43 import java.io.*;
  44 import java.lang.invoke.*;
  45 import java.security.*;
  46 
  47 import static java.lang.invoke.MethodHandles.*;
  48 import static java.lang.invoke.MethodType.*;











  49 
  50 public class MethodHandleConstants {































  51     public static void main(String... av) throws Throwable {
  52         if (av.length > 0 && av[0].equals("--check-output"))  openBuf();
  53         if (av.length > 0 && av[0].equals("--security-manager"))  setSM();
  54         System.out.println("Obtaining method handle constants:");
  55         testCase(MH_String_replace_C2(), String.class, "replace", String.class, String.class, char.class, char.class);
  56         testCase(MH_MethodHandle_invokeExact_SC2(), MethodHandle.class, "invokeExact", String.class, MethodHandle.class, String.class, char.class, char.class);
  57         testCase(MH_MethodHandle_invoke_SC2(), MethodHandle.class, "invoke", String.class, MethodHandle.class, String.class, char.class, char.class);
  58         testCase(MH_Class_forName_S(), Class.class, "forName", Class.class, String.class);
  59         testCase(MH_Class_forName_SbCL(), Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class);
  60         System.out.println("Done.");
  61         closeBuf();
  62     }
  63 
  64     private static void testCase(MethodHandle mh, Class<?> defc, String name, Class<?> rtype, Class<?>... ptypes) throws Throwable {
  65         System.out.println(mh);
  66         // we include defc, because we assume it is a non-static MH:
  67         MethodType mt = methodType(rtype, ptypes);
  68         assertEquals(mh.type(), mt);
  69         // FIXME: Use revealDirect to find out more
  70     }
  71     private static void assertEquals(Object exp, Object act) {
  72         if (exp == act || (exp != null && exp.equals(act)))  return;
  73         throw new AssertionError("not equal: "+exp+", "+act);
  74     }
  75 
  76     private static void setSM() {
  77         Policy.setPolicy(new TestPolicy());
  78         System.setSecurityManager(new SecurityManager());
  79     }


  94         Iterator<String> iter = Arrays.asList(haveLines).iterator();
  95         for (String want : EXPECT_OUTPUT) {
  96             String have = iter.hasNext() ? iter.next() : "[EOF]";
  97             if (want.equals(have))  continue;
  98             System.err.println("want line: "+want);
  99             System.err.println("have line: "+have);
 100             throw new AssertionError("unexpected output: "+have);
 101         }
 102         if (iter.hasNext())
 103             throw new AssertionError("unexpected output: "+iter.next());
 104     }
 105     private static final String[] EXPECT_OUTPUT = {
 106         "Obtaining method handle constants:",
 107         "MethodHandle(String,char,char)String",
 108         "MethodHandle(MethodHandle,String,char,char)String",
 109         "MethodHandle(MethodHandle,String,char,char)String",
 110         "MethodHandle(String)Class",
 111         "MethodHandle(String,boolean,ClassLoader)Class",
 112         "Done."
 113     };
 114 
 115     // String.replace(String, char, char)
 116     private static MethodType MT_String_replace_C2() {
 117         shouldNotCallThis();
 118         return methodType(String.class, char.class, char.class);
 119     }
 120     private static MethodHandle MH_String_replace_C2() throws ReflectiveOperationException {
 121         shouldNotCallThis();
 122         return lookup().findVirtual(String.class, "replace", MT_String_replace_C2());
 123     }
 124 
 125     // MethodHandle.invokeExact(...)
 126     private static MethodType MT_MethodHandle_invokeExact_SC2() {
 127         shouldNotCallThis();
 128         return methodType(String.class, String.class, char.class, char.class);
 129     }
 130     private static MethodHandle MH_MethodHandle_invokeExact_SC2() throws ReflectiveOperationException {
 131         shouldNotCallThis();
 132         return lookup().findVirtual(MethodHandle.class, "invokeExact", MT_MethodHandle_invokeExact_SC2());
 133     }
 134 
 135     // MethodHandle.invoke(...)
 136     private static MethodType MT_MethodHandle_invoke_SC2() {
 137         shouldNotCallThis();
 138         return methodType(String.class, String.class, char.class, char.class);
 139     }
 140     private static MethodHandle MH_MethodHandle_invoke_SC2() throws ReflectiveOperationException {
 141         shouldNotCallThis();
 142         return lookup().findVirtual(MethodHandle.class, "invoke", MT_MethodHandle_invoke_SC2());
 143     }
 144 
 145     // Class.forName(String)
 146     private static MethodType MT_Class_forName_S() {
 147         shouldNotCallThis();
 148         return methodType(Class.class, String.class);
 149     }
 150     private static MethodHandle MH_Class_forName_S() throws ReflectiveOperationException {
 151         shouldNotCallThis();
 152         return lookup().findStatic(Class.class, "forName", MT_Class_forName_S());
 153     }
 154 
 155     // Class.forName(String, boolean, ClassLoader)
 156     private static MethodType MT_Class_forName_SbCL() {
 157         shouldNotCallThis();
 158         return methodType(Class.class, String.class, boolean.class, ClassLoader.class);
 159     }
 160     private static MethodHandle MH_Class_forName_SbCL() throws ReflectiveOperationException {
 161         shouldNotCallThis();
 162         return lookup().findStatic(Class.class, "forName", MT_Class_forName_SbCL());
 163     }
 164 
 165     private static void shouldNotCallThis() {
 166         // if this gets called, the transformation has not taken place
 167         if (System.getProperty("MethodHandleConstants.allow-untransformed") != null)  return;
 168         throw new AssertionError("this code should be statically transformed away by Indify");
 169     }
 170 
 171     static class TestPolicy extends Policy {
 172         final PermissionCollection permissions = new Permissions();
 173         TestPolicy() {
 174             permissions.add(new java.io.FilePermission("<<ALL FILES>>", "read"));
 175         }
 176         public PermissionCollection getPermissions(ProtectionDomain domain) {
 177             return permissions;
 178         }
 179 
 180         public PermissionCollection getPermissions(CodeSource codesource) {
 181             return permissions;
 182         }
 183 
 184         public boolean implies(ProtectionDomain domain, Permission perm) {
 185             return permissions.implies(perm);
 186         }
 187     }
 188 }
   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     }


 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 }
< prev index next >