/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import static jdk.internal.org.objectweb.asm.Opcodes.*; import jdk.internal.org.objectweb.asm.*; /* * @test TestInvokeSpecial * @bug 8009697 * @compile -XDignore.symbol.file TestInvokeSpecial.java * @run main TestInvokeSpecial */ /* class A { public void m() { System.out.println("A.m"); } } class C extends A { public static void test() { new A dup invokespecial A/()V invokespecial A/m()V } } public class TestInvokeSpecial { public static void main(String[] args) { C.test(); } } */ public class TestInvokeSpecial { static final String classAName = "A"; static final String classCName = "C"; public static void main(String[] args) throws Exception { ClassLoader cl = new ClassLoader() { public Class loadClass(String name) throws ClassNotFoundException { if (findLoadedClass(name) != null) { return findLoadedClass(name); } if (classAName.equals(name)) { byte[] classFile = dumpA(); return defineClass(classAName, classFile, 0, classFile.length); } if (classCName.equals(name)) { byte[] classFile = dumpC(); return defineClass(classCName, classFile, 0, classFile.length); } return super.loadClass(name); } }; try { cl.loadClass(classCName).getDeclaredMethod("test").invoke(null); } catch (java.lang.VerifyError e) { System.out.println("\nTestInvokeSpecial: VerifyError caught, printing details\n\n"); e.printStackTrace(); } } public static byte[] dumpA() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); MethodVisitor mv; cw.visit(52, ACC_PUBLIC + ACC_SUPER, classAName, null, "java/lang/Object", null); { mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "m", "()V", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Inside of class A's m() method\n"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); } public static byte[] dumpC() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); MethodVisitor mv; cw.visit(52, ACC_PUBLIC + ACC_SUPER, classCName, null, classAName, null); { mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, classAName, "", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "test", "()V", null, null); mv.visitCode(); // Print "In C's test() method" message mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Inside of class C's test() method\n"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V"); // Create an object of type A, try invoking A's m() via an INVOKESPECIAL bytecode. mv.visitTypeInsn(NEW, classAName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, classAName, "", "()V"); mv.visitMethodInsn(INVOKESPECIAL, classAName, "m", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); } }