Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/lookup/SpecialStatic.java
47490 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 803240025* @summary JSR292: invokeSpecial: InternalError attempting to lookup a method26* @compile -XDignore.symbol.file SpecialStatic.java27* @run junit test.java.lang.invoke.lookup.SpecialStatic28*/29package test.java.lang.invoke.lookup;3031import java.lang.invoke.MethodHandle;32import java.lang.invoke.MethodHandles;33import java.lang.invoke.MethodType;34import jdk.internal.org.objectweb.asm.*;35import org.junit.Test;36import static jdk.internal.org.objectweb.asm.Opcodes.*;37import static org.junit.Assert.*;3839/**40* Test case:41* class T1 { int m() { return 1; }}42* class T2 extends T1 { static int m() { return 2; }}43* class T3 extends T2 { int m() { return 3; }}44*45* T3::test { invokespecial T1.m() T3 } ==> T1::m46*/47public class SpecialStatic {48static class CustomClassLoader extends ClassLoader {49public Class<?> loadClass(String name) throws ClassNotFoundException {50if (findLoadedClass(name) != null) {51return findLoadedClass(name);52}5354if ("T1".equals(name)) {55byte[] classFile = dumpT1();56return defineClass("T1", classFile, 0, classFile.length);57}58if ("T2".equals(name)) {59byte[] classFile = dumpT2();60return defineClass("T2", classFile, 0, classFile.length);61}62if ("T3".equals(name)) {63byte[] classFile = dumpT3();64return defineClass("T3", classFile, 0, classFile.length);65}6667return super.loadClass(name);68}69}7071private static ClassLoader cl = new CustomClassLoader();72private static Class t1, t3;73static {74try {75t1 = cl.loadClass("T1");76t3 = cl.loadClass("T3");77} catch (ClassNotFoundException e) {78throw new Error(e);79}80}8182public static void main(String[] args) throws Throwable {83SpecialStatic test = new SpecialStatic();84test.testConstant();85test.testFindSpecial();86}8788@Test89public void testConstant() throws Throwable {90MethodHandle mh = (MethodHandle)t3.getDeclaredMethod("getMethodHandle").invoke(null);91int result = (int)mh.invoke(t3.newInstance());92assertEquals(result, 1); // T1.m should be invoked.93}9495@Test96public void testFindSpecial() throws Throwable {97MethodHandles.Lookup lookup = (MethodHandles.Lookup)t3.getDeclaredMethod("getLookup").invoke(null);98MethodHandle mh = lookup.findSpecial(t1, "m", MethodType.methodType(int.class), t3);99int result = (int)mh.invoke(t3.newInstance());100assertEquals(result, 1); // T1.m should be invoked.101}102103public static byte[] dumpT1() {104ClassWriter cw = new ClassWriter(0);105MethodVisitor mv;106107cw.visit(52, ACC_PUBLIC + ACC_SUPER, "T1", null, "java/lang/Object", null);108109mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);110mv.visitCode();111mv.visitVarInsn(ALOAD, 0);112mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);113mv.visitInsn(RETURN);114mv.visitMaxs(1, 1);115mv.visitEnd();116117mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);118mv.visitCode();119mv.visitIntInsn(BIPUSH, 1);120mv.visitInsn(IRETURN);121mv.visitMaxs(1, 1);122mv.visitEnd();123124cw.visitEnd();125return cw.toByteArray();126}127128public static byte[] dumpT2() {129ClassWriter cw = new ClassWriter(0);130MethodVisitor mv;131132cw.visit(52, ACC_PUBLIC + ACC_SUPER, "T2", null, "T1", null);133134mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);135mv.visitCode();136mv.visitVarInsn(ALOAD, 0);137mv.visitMethodInsn(INVOKESPECIAL, "T1", "<init>", "()V", false);138mv.visitInsn(RETURN);139mv.visitMaxs(1, 1);140mv.visitEnd();141142mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()I", null, null);143mv.visitCode();144mv.visitIntInsn(BIPUSH, 2);145mv.visitInsn(IRETURN);146mv.visitMaxs(1, 1);147mv.visitEnd();148149cw.visitEnd();150return cw.toByteArray();151}152153public static byte[] dumpT3() {154ClassWriter cw = new ClassWriter(0);155MethodVisitor mv;156157cw.visit(52, ACC_PUBLIC + ACC_SUPER, "T3", null, "T2", null);158159mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);160mv.visitCode();161mv.visitVarInsn(ALOAD, 0);162mv.visitMethodInsn(INVOKESPECIAL, "T2", "<init>", "()V", false);163mv.visitInsn(RETURN);164mv.visitMaxs(1, 1);165mv.visitEnd();166167mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);168mv.visitCode();169mv.visitIntInsn(BIPUSH, 3);170mv.visitInsn(IRETURN);171mv.visitMaxs(1, 1);172mv.visitEnd();173174// getMethodHandle175mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "getMethodHandle", "()Ljava/lang/invoke/MethodHandle;", null, null);176mv.visitCode();177mv.visitLdcInsn(new Handle(H_INVOKESPECIAL, "T1", "m", "()I"));178mv.visitInsn(ARETURN);179mv.visitMaxs(1, 0);180mv.visitEnd();181182// getLookup183mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "getLookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", null, null);184mv.visitCode();185mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false);186mv.visitInsn(ARETURN);187mv.visitMaxs(1, 0);188mv.visitEnd();189190cw.visitEnd();191return cw.toByteArray();192}193}194195196