Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/MethodHandleConstants.java
47216 views
/*1* Copyright (c) 2010, 2013, 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=031* --expand-properties --classpath ${test.classes}32* --java test.java.lang.invoke.MethodHandleConstants --check-output33* @run main/othervm34* indify.Indify35* --expand-properties --classpath ${test.classes}36* --java test.java.lang.invoke.MethodHandleConstants --security-manager37*/3839package test.java.lang.invoke;4041import java.util.*;42import java.io.*;43import java.lang.invoke.*;44import java.security.*;4546import static java.lang.invoke.MethodHandles.*;47import static java.lang.invoke.MethodType.*;4849public class MethodHandleConstants {50public static void main(String... av) throws Throwable {51if (av.length > 0 && av[0].equals("--check-output")) openBuf();52if (av.length > 0 && av[0].equals("--security-manager")) setSM();53System.out.println("Obtaining method handle constants:");54testCase(MH_String_replace_C2(), String.class, "replace", String.class, String.class, char.class, char.class);55testCase(MH_MethodHandle_invokeExact_SC2(), MethodHandle.class, "invokeExact", String.class, MethodHandle.class, String.class, char.class, char.class);56testCase(MH_MethodHandle_invoke_SC2(), MethodHandle.class, "invoke", String.class, MethodHandle.class, String.class, char.class, char.class);57testCase(MH_Class_forName_S(), Class.class, "forName", Class.class, String.class);58testCase(MH_Class_forName_SbCL(), Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class);59System.out.println("Done.");60closeBuf();61}6263private static void testCase(MethodHandle mh, Class<?> defc, String name, Class<?> rtype, Class<?>... ptypes) throws Throwable {64System.out.println(mh);65// we include defc, because we assume it is a non-static MH:66MethodType mt = methodType(rtype, ptypes);67assertEquals(mh.type(), mt);68// FIXME: Use revealDirect to find out more69}70private static void assertEquals(Object exp, Object act) {71if (exp == act || (exp != null && exp.equals(act))) return;72throw new AssertionError("not equal: "+exp+", "+act);73}7475private static void setSM() {76Policy.setPolicy(new TestPolicy());77System.setSecurityManager(new SecurityManager());78}7980private static PrintStream oldOut;81private static ByteArrayOutputStream buf;82private static void openBuf() {83oldOut = System.out;84buf = new ByteArrayOutputStream();85System.setOut(new PrintStream(buf));86}87private static void closeBuf() {88if (buf == null) return;89System.out.flush();90System.setOut(oldOut);91String[] haveLines = new String(buf.toByteArray()).split("[\n\r]+");92for (String line : haveLines) System.out.println(line);93Iterator<String> iter = Arrays.asList(haveLines).iterator();94for (String want : EXPECT_OUTPUT) {95String have = iter.hasNext() ? iter.next() : "[EOF]";96if (want.equals(have)) continue;97System.err.println("want line: "+want);98System.err.println("have line: "+have);99throw new AssertionError("unexpected output: "+have);100}101if (iter.hasNext())102throw new AssertionError("unexpected output: "+iter.next());103}104private static final String[] EXPECT_OUTPUT = {105"Obtaining method handle constants:",106"MethodHandle(String,char,char)String",107"MethodHandle(MethodHandle,String,char,char)String",108"MethodHandle(MethodHandle,String,char,char)String",109"MethodHandle(String)Class",110"MethodHandle(String,boolean,ClassLoader)Class",111"Done."112};113114// String.replace(String, char, char)115private static MethodType MT_String_replace_C2() {116shouldNotCallThis();117return methodType(String.class, char.class, char.class);118}119private static MethodHandle MH_String_replace_C2() throws ReflectiveOperationException {120shouldNotCallThis();121return lookup().findVirtual(String.class, "replace", MT_String_replace_C2());122}123124// MethodHandle.invokeExact(...)125private static MethodType MT_MethodHandle_invokeExact_SC2() {126shouldNotCallThis();127return methodType(String.class, String.class, char.class, char.class);128}129private static MethodHandle MH_MethodHandle_invokeExact_SC2() throws ReflectiveOperationException {130shouldNotCallThis();131return lookup().findVirtual(MethodHandle.class, "invokeExact", MT_MethodHandle_invokeExact_SC2());132}133134// MethodHandle.invoke(...)135private static MethodType MT_MethodHandle_invoke_SC2() {136shouldNotCallThis();137return methodType(String.class, String.class, char.class, char.class);138}139private static MethodHandle MH_MethodHandle_invoke_SC2() throws ReflectiveOperationException {140shouldNotCallThis();141return lookup().findVirtual(MethodHandle.class, "invoke", MT_MethodHandle_invoke_SC2());142}143144// Class.forName(String)145private static MethodType MT_Class_forName_S() {146shouldNotCallThis();147return methodType(Class.class, String.class);148}149private static MethodHandle MH_Class_forName_S() throws ReflectiveOperationException {150shouldNotCallThis();151return lookup().findStatic(Class.class, "forName", MT_Class_forName_S());152}153154// Class.forName(String, boolean, ClassLoader)155private static MethodType MT_Class_forName_SbCL() {156shouldNotCallThis();157return methodType(Class.class, String.class, boolean.class, ClassLoader.class);158}159private static MethodHandle MH_Class_forName_SbCL() throws ReflectiveOperationException {160shouldNotCallThis();161return lookup().findStatic(Class.class, "forName", MT_Class_forName_SbCL());162}163164private static void shouldNotCallThis() {165// if this gets called, the transformation has not taken place166if (System.getProperty("MethodHandleConstants.allow-untransformed") != null) return;167throw new AssertionError("this code should be statically transformed away by Indify");168}169170static class TestPolicy extends Policy {171final PermissionCollection permissions = new Permissions();172TestPolicy() {173permissions.add(new java.io.FilePermission("<<ALL FILES>>", "read"));174}175public PermissionCollection getPermissions(ProtectionDomain domain) {176return permissions;177}178179public PermissionCollection getPermissions(CodeSource codesource) {180return permissions;181}182183public boolean implies(ProtectionDomain domain, Permission perm) {184return permissions.implies(perm);185}186}187}188189190