Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/VMAnonymousClass.java
47209 views
/*1* Copyright (c) 2014, 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 804690325* @summary VM anonymous class members can't be statically invocable26* @run junit test.java.lang.invoke.VMAnonymousClass27*/28package test.java.lang.invoke;2930import java.lang.invoke.MethodHandle;31import java.lang.invoke.MethodHandles;32import java.lang.invoke.MethodType;33import java.lang.reflect.Field;34import org.junit.Test;35import sun.misc.Unsafe;36import jdk.internal.org.objectweb.asm.*;37import static jdk.internal.org.objectweb.asm.Opcodes.*;3839public class VMAnonymousClass {40public static void main(String[] args) throws Throwable {41VMAnonymousClass test = new VMAnonymousClass();42test.testJavaLang();43test.testJavaUtil();44test.testSunMisc();45test.testJavaLangInvoke();46System.out.println("TEST PASSED");47}4849// Test VM anonymous classes from different packages50// (see j.l.i.InvokerBytecodeGenerator::isStaticallyInvocable).51@Test public void testJavaLang() throws Throwable { test("java/lang"); }52@Test public void testJavaUtil() throws Throwable { test("java/util"); }53@Test public void testSunMisc() throws Throwable { test("sun/misc"); }54@Test public void testJavaLangInvoke() throws Throwable { test("java/lang/invoke"); }5556private static Unsafe unsafe = getUnsafe();5758private static void test(String pkg) throws Throwable {59byte[] bytes = dumpClass(pkg);60// Define VM anonymous class in privileged context (on BCP).61Class anonClass = unsafe.defineAnonymousClass(Object.class, bytes, null);6263MethodType t = MethodType.methodType(Object.class, int.class);64MethodHandle target = MethodHandles.lookup().findStatic(anonClass, "get", t);6566// Wrap target into LF (convert) to get "target" referenced from LF67MethodHandle wrappedMH = target.asType(MethodType.methodType(Object.class, Integer.class));6869// Invoke enough times to provoke LF compilation to bytecode.70for (int i = 0; i<100; i++) {71Object r = wrappedMH.invokeExact((Integer)1);72}73}7475/*76* Constructs bytecode for the following class:77* public class pkg.MyClass {78* MyClass() {}79* public Object get(int i) { return null; }80* }81*/82public static byte[] dumpClass(String pkg) {83ClassWriter cw = new ClassWriter(0);84MethodVisitor mv;8586cw.visit(52, ACC_SUPER | ACC_PUBLIC, pkg+"/MyClass", null, "java/lang/Object", null);87{88mv = cw.visitMethod(0, "<init>", "()V", null, null);89mv.visitCode();90mv.visitVarInsn(ALOAD, 0);91mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);92mv.visitInsn(RETURN);93mv.visitMaxs(1, 1);94mv.visitEnd();95}96{97mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "get", "(I)Ljava/lang/Object;", null, null);98mv.visitCode();99mv.visitInsn(ACONST_NULL);100mv.visitInsn(ARETURN);101mv.visitMaxs(1, 1);102mv.visitEnd();103}104cw.visitEnd();105return cw.toByteArray();106}107108private static synchronized Unsafe getUnsafe() {109try {110Field f = Unsafe.class.getDeclaredField("theUnsafe");111f.setAccessible(true);112return (Unsafe) f.get(null);113} catch (NoSuchFieldException | IllegalAccessException e) {114throw new RuntimeException("Unable to get Unsafe instance.", e);115}116}117}118119120