Path: blob/master/test/jdk/java/lang/invoke/MethodHandleConstants.java
41149 views
/*1* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 802206625* @summary smoke test for method handle constants26* @build indify.Indify27* @compile MethodHandleConstants.java28* @run main/othervm29* indify.Indify30* --verify-specifier-count=431* --expand-properties --classpath ${test.classes}32* --java test.java.lang.invoke.MethodHandleConstants --check-output33* @run main/othervm34* -Djava.security.manager=allow35* indify.Indify36* --expand-properties --classpath ${test.classes}37* --java test.java.lang.invoke.MethodHandleConstants --security-manager38*/3940package test.java.lang.invoke;4142import java.util.*;43import java.io.*;44import java.lang.invoke.*;45import java.security.*;4647import static java.lang.invoke.MethodHandles.*;48import static java.lang.invoke.MethodType.*;4950public class MethodHandleConstants {51public static void main(String... av) throws Throwable {52if (av.length > 0 && av[0].equals("--check-output")) openBuf();53if (av.length > 0 && av[0].equals("--security-manager")) setSM();54System.out.println("Obtaining method handle constants:");55testCase(MH_String_replace_C2(), String.class, "replace", String.class, String.class, char.class, char.class);56testCase(MH_MethodHandle_invokeExact_SC2(), MethodHandle.class, "invokeExact", String.class, MethodHandle.class, String.class, char.class, char.class);57testCase(MH_MethodHandle_invoke_SC2(), MethodHandle.class, "invoke", String.class, MethodHandle.class, String.class, char.class, char.class);58testCase(MH_Class_forName_S(), Class.class, "forName", Class.class, String.class);59testCase(MH_Class_forName_SbCL(), Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class);60System.out.println("Done.");61closeBuf();62}6364private static void testCase(MethodHandle mh, Class<?> defc, String name, Class<?> rtype, Class<?>... ptypes) throws Throwable {65System.out.println(mh);66// we include defc, because we assume it is a non-static MH:67MethodType mt = methodType(rtype, ptypes);68assertEquals(mh.type(), mt);69// FIXME: Use revealDirect to find out more70}71private static void assertEquals(Object exp, Object act) {72if (exp == act || (exp != null && exp.equals(act))) return;73throw new AssertionError("not equal: "+exp+", "+act);74}7576private static void setSM() {77Policy.setPolicy(new TestPolicy());78System.setSecurityManager(new SecurityManager());79}8081private static PrintStream oldOut;82private static ByteArrayOutputStream buf;83private static void openBuf() {84oldOut = System.out;85buf = new ByteArrayOutputStream();86System.setOut(new PrintStream(buf));87}88private static void closeBuf() {89if (buf == null) return;90System.out.flush();91System.setOut(oldOut);92String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");93for (String line : haveLines) System.out.println(line);94Iterator<String> iter = Arrays.asList(haveLines).iterator();95for (String want : EXPECT_OUTPUT) {96String have = iter.hasNext() ? iter.next() : "[EOF]";97if (want.equals(have)) continue;98System.err.println("want line: "+want);99System.err.println("have line: "+have);100throw new AssertionError("unexpected output: "+have);101}102if (iter.hasNext())103throw new AssertionError("unexpected output: "+iter.next());104}105private 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};114115// String.replace(String, char, char)116private static MethodType MT_String_replace_C2() {117shouldNotCallThis();118return methodType(String.class, char.class, char.class);119}120private static MethodHandle MH_String_replace_C2() throws ReflectiveOperationException {121shouldNotCallThis();122return lookup().findVirtual(String.class, "replace", MT_String_replace_C2());123}124125// MethodHandle.invokeExact(...)126private static MethodType MT_MethodHandle_invokeExact_SC2() {127shouldNotCallThis();128return methodType(String.class, String.class, char.class, char.class);129}130private static MethodHandle MH_MethodHandle_invokeExact_SC2() throws ReflectiveOperationException {131shouldNotCallThis();132return lookup().findVirtual(MethodHandle.class, "invokeExact", MT_MethodHandle_invokeExact_SC2());133}134135// MethodHandle.invoke(...)136private static MethodType MT_MethodHandle_invoke_SC2() {137shouldNotCallThis();138return methodType(String.class, String.class, char.class, char.class);139}140private static MethodHandle MH_MethodHandle_invoke_SC2() throws ReflectiveOperationException {141shouldNotCallThis();142return lookup().findVirtual(MethodHandle.class, "invoke", MT_MethodHandle_invoke_SC2());143}144145// Class.forName(String)146private static MethodType MT_Class_forName_S() {147shouldNotCallThis();148return methodType(Class.class, String.class);149}150private static MethodHandle MH_Class_forName_S() throws ReflectiveOperationException {151shouldNotCallThis();152return lookup().findStatic(Class.class, "forName", MT_Class_forName_S());153}154155// Class.forName(String, boolean, ClassLoader)156private static MethodType MT_Class_forName_SbCL() {157shouldNotCallThis();158return methodType(Class.class, String.class, boolean.class, ClassLoader.class);159}160private static MethodHandle MH_Class_forName_SbCL() throws ReflectiveOperationException {161shouldNotCallThis();162return lookup().findStatic(Class.class, "forName", MT_Class_forName_SbCL());163}164165private static void shouldNotCallThis() {166// if this gets called, the transformation has not taken place167if (System.getProperty("MethodHandleConstants.allow-untransformed") != null) return;168throw new AssertionError("this code should be statically transformed away by Indify");169}170171static class TestPolicy extends Policy {172static final Policy DEFAULT_POLICY = Policy.getPolicy();173174final PermissionCollection permissions = new Permissions();175TestPolicy() {176permissions.add(new java.io.FilePermission("<<ALL FILES>>", "read"));177}178public PermissionCollection getPermissions(ProtectionDomain domain) {179return permissions;180}181182public PermissionCollection getPermissions(CodeSource codesource) {183return permissions;184}185186public boolean implies(ProtectionDomain domain, Permission perm) {187return permissions.implies(perm) || DEFAULT_POLICY.implies(domain, perm);188}189}190}191192193