aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8032010 aoqi@0: * @summary method lookup on an abstract method in a concrete class should be successful aoqi@0: * @run main TestConcreteClassWithAbstractMethod aoqi@0: */ aoqi@0: aoqi@0: import jdk.internal.org.objectweb.asm.ClassWriter; aoqi@0: import jdk.internal.org.objectweb.asm.MethodVisitor; aoqi@0: aoqi@0: import static jdk.internal.org.objectweb.asm.Opcodes.*; aoqi@0: aoqi@0: /* aoqi@0: * class T1 { public int m() {} } aoqi@0: * class T2 { public abstract int m(); } aoqi@0: * class T3 { public int m() {} } aoqi@0: * aoqi@0: * Call site: T3.test() { invokevirtual T2.m() } aoqi@0: * T3.m() should be invoked aoqi@0: */ aoqi@0: public class TestConcreteClassWithAbstractMethod { aoqi@0: static final String classT1 = "p1.T1"; aoqi@0: static final String classT2 = "p1.T2"; aoqi@0: static final String classT3 = "p1.T3"; aoqi@0: aoqi@0: static final String callerName = classT3; aoqi@0: aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: ClassLoader cl = new ClassLoader() { aoqi@0: public Class loadClass(String name) throws ClassNotFoundException { aoqi@0: if (findLoadedClass(name) != null) { aoqi@0: return findLoadedClass(name); aoqi@0: } aoqi@0: aoqi@0: if (classT1.equals(name)) { aoqi@0: byte[] classFile = dumpT1(); aoqi@0: return defineClass(classT1, classFile, 0, classFile.length); aoqi@0: } aoqi@0: if (classT2.equals(name)) { aoqi@0: byte[] classFile = dumpT2(); aoqi@0: return defineClass(classT2, classFile, 0, classFile.length); aoqi@0: } aoqi@0: if (classT3.equals(name)) { aoqi@0: byte[] classFile = dumpT3(); aoqi@0: return defineClass(classT3, classFile, 0, classFile.length); aoqi@0: } aoqi@0: aoqi@0: return super.loadClass(name); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: cl.loadClass(classT1); aoqi@0: cl.loadClass(classT2); aoqi@0: cl.loadClass(classT3); aoqi@0: aoqi@0: //cl.loadClass(callerName).getDeclaredMethod("m"); aoqi@0: cl.loadClass(callerName).newInstance(); aoqi@0: aoqi@0: int result = (Integer)cl.loadClass(callerName).getDeclaredMethod("test").invoke(null); aoqi@0: System.out.println(""+result); aoqi@0: } aoqi@0: aoqi@0: public static byte[] dumpT1() { aoqi@0: ClassWriter cw = new ClassWriter(0); aoqi@0: MethodVisitor mv; aoqi@0: aoqi@0: cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T1", null, "java/lang/Object", null); aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); aoqi@0: mv.visitCode(); aoqi@0: mv.visitVarInsn(ALOAD, 0); aoqi@0: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V", false); aoqi@0: mv.visitInsn(RETURN); aoqi@0: mv.visitMaxs(1, 1); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null); aoqi@0: mv.visitCode(); aoqi@0: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); aoqi@0: mv.visitLdcInsn("p1/T1.m()"); aoqi@0: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); aoqi@0: mv.visitIntInsn(BIPUSH, 3); aoqi@0: mv.visitInsn(IRETURN); aoqi@0: mv.visitMaxs(2, 1); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: cw.visitEnd(); aoqi@0: aoqi@0: return cw.toByteArray(); aoqi@0: } aoqi@0: aoqi@0: public static byte[] dumpT2() { aoqi@0: ClassWriter cw = new ClassWriter(0); aoqi@0: MethodVisitor mv; aoqi@0: aoqi@0: cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T2", null, "p1/T1", null); aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); aoqi@0: mv.visitCode(); aoqi@0: mv.visitVarInsn(ALOAD, 0); aoqi@0: mv.visitMethodInsn(INVOKESPECIAL, "p1/T1", "", "()V", false); aoqi@0: mv.visitInsn(RETURN); aoqi@0: mv.visitMaxs(1, 1); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: cw.visitEnd(); aoqi@0: aoqi@0: return cw.toByteArray(); aoqi@0: } aoqi@0: aoqi@0: public static byte[] dumpT3() { aoqi@0: ClassWriter cw = new ClassWriter(0); aoqi@0: MethodVisitor mv; aoqi@0: aoqi@0: cw.visit(52, ACC_PUBLIC + ACC_SUPER, "p1/T3", null, "p1/T2", null); aoqi@0: aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); aoqi@0: mv.visitCode(); aoqi@0: mv.visitVarInsn(ALOAD, 0); aoqi@0: mv.visitMethodInsn(INVOKESPECIAL, "p1/T2", "", "()V", false); aoqi@0: mv.visitInsn(RETURN); aoqi@0: mv.visitMaxs(1, 1); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null); aoqi@0: mv.visitCode(); aoqi@0: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); aoqi@0: mv.visitLdcInsn("p1/T3.m()"); aoqi@0: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); aoqi@0: mv.visitIntInsn(BIPUSH, 2); aoqi@0: mv.visitInsn(IRETURN); aoqi@0: mv.visitMaxs(2, 1); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: { aoqi@0: mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null); aoqi@0: mv.visitCode(); aoqi@0: mv.visitTypeInsn(NEW, "p1/T3"); aoqi@0: mv.visitInsn(DUP); aoqi@0: mv.visitMethodInsn(INVOKESPECIAL, "p1/T3", "", "()V", false); aoqi@0: mv.visitMethodInsn(INVOKEVIRTUAL, "p1/T2", "m", "()I", false); aoqi@0: mv.visitInsn(IRETURN); aoqi@0: mv.visitMaxs(3, 2); aoqi@0: mv.visitEnd(); aoqi@0: } aoqi@0: cw.visitEnd(); aoqi@0: aoqi@0: return cw.toByteArray(); aoqi@0: } aoqi@0: }