1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 8163808 26 * @modules java.base/jdk.internal.org.objectweb.asm 27 * @run main TransitiveOverrideCFV50 28 */ 29 30import java.util.*; 31import java.io.File; 32import java.io.FileOutputStream; 33import jdk.internal.org.objectweb.asm.ClassWriter; 34import jdk.internal.org.objectweb.asm.MethodVisitor; 35import jdk.internal.org.objectweb.asm.AnnotationVisitor; 36import jdk.internal.org.objectweb.asm.Opcodes; 37 38/* 39 * Test mixed classfile version overriding handling. 40 * Key is to generate P2/C with an older classfile version <=50 41 * Correct response is B.m:2 for older classfiles 42 * This test was added to ensure no assertions in debug 43 * note: for P2/C classfile version >=51, correct answer becomes C.m:3 44 * public class P1.A { public int m() { return 1; } 45 * 46 * public class P1.B extends A { int m() { return 2; } 47 * 48 * public class P2.c extends P1.B { public int m() { return 3; } 49 */ 50 51public class TransitiveOverrideCFV50 implements Opcodes{ 52 static final String classP1A = "P1.A"; 53 static final String classP1B = "P1.B"; 54 static final String classP2C = "P2.C"; 55 56 static final String callerName = classP2C; 57 58 public static void main(String[] args) throws Exception { 59 ClassLoader cl = new ClassLoader() { 60 public Class<?> loadClass(String name) throws ClassNotFoundException { 61 if (findLoadedClass(name) != null) { 62 return findLoadedClass(name); 63 } 64 65 if (classP1A.equals(name)) { 66 byte[] classFile = dumpP1A(); 67 return defineClass(classP1A, classFile, 0, classFile.length); 68 } 69 if (classP1B.equals(name)) { 70 byte[] classFile = dumpP1B(); 71 return defineClass(classP1B, classFile, 0, classFile.length); 72 } 73 if (classP2C.equals(name)) { 74 byte[] classFile = dumpP2C(); 75 return defineClass(classP2C, classFile, 0, classFile.length); 76 } 77 78 return super.loadClass(name); 79 } 80 }; 81 82 cl.loadClass(classP1A); 83 cl.loadClass(classP1B); 84 cl.loadClass(classP2C); 85 86 //cl.loadClass(callerName).getDeclaredMethod("test"); 87 cl.loadClass(callerName).newInstance(); 88 89 int result = (Integer)cl.loadClass(callerName).getDeclaredMethod("test").invoke(null); 90 if (result != 2) { 91 throw new RuntimeException("expected B.m:2, got " + result); 92 } 93 } 94 95 public static byte[] dumpP1A() { 96 97 ClassWriter cw = new ClassWriter(0); 98 MethodVisitor mv; 99 100 cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "P1/A", null, "java/lang/Object", null); 101 102 { 103 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 104 mv.visitCode(); 105 mv.visitVarInsn(ALOAD, 0); 106 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 107 mv.visitInsn(RETURN); 108 mv.visitMaxs(1, 1); 109 mv.visitEnd(); 110 } 111 { 112 mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null); 113 mv.visitCode(); 114 mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 115 mv.visitLdcInsn("A.m"); 116 mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 117 mv.visitIntInsn(BIPUSH, 1); 118 mv.visitInsn(IRETURN); 119 mv.visitMaxs(2, 1); 120 mv.visitEnd(); 121 } 122 cw.visitEnd(); 123 124 return cw.toByteArray(); 125 } 126 public static byte[] dumpP1B() { 127 128 ClassWriter cw = new ClassWriter(0); 129 MethodVisitor mv; 130 131 cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "P1/B", null, "P1/A", null); 132 133 { 134 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 135 mv.visitCode(); 136 mv.visitVarInsn(ALOAD, 0); 137 mv.visitMethodInsn(INVOKESPECIAL, "P1/A", "<init>", "()V", false); 138 mv.visitInsn(RETURN); 139 mv.visitMaxs(1, 1); 140 mv.visitEnd(); 141 } 142 { 143 mv = cw.visitMethod(0, "m", "()I", null, null); 144 mv.visitCode(); 145 mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 146 mv.visitLdcInsn("B.m"); 147 mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 148 mv.visitIntInsn(BIPUSH, 2); 149 mv.visitInsn(IRETURN); 150 mv.visitMaxs(2, 1); 151 mv.visitEnd(); 152 } 153 cw.visitEnd(); 154 155 return cw.toByteArray(); 156 } 157 public static byte[] dumpP2C() { 158 159 ClassWriter cw = new ClassWriter(0); 160 MethodVisitor mv; 161 162 cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "P2/C", null, "P1/B", null); 163 164 { 165 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 166 mv.visitCode(); 167 mv.visitVarInsn(ALOAD, 0); 168 mv.visitMethodInsn(INVOKESPECIAL, "P1/B", "<init>", "()V", false); 169 mv.visitInsn(RETURN); 170 mv.visitMaxs(1, 1); 171 mv.visitEnd(); 172 } 173 { 174 mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null); 175 mv.visitCode(); 176 mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); 177 mv.visitLdcInsn("P2/C.m"); 178 mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 179 mv.visitIntInsn(BIPUSH, 3); 180 mv.visitInsn(IRETURN); 181 mv.visitMaxs(2, 1); 182 mv.visitEnd(); 183 } 184 { 185 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null); 186 mv.visitCode(); 187 mv.visitTypeInsn(NEW, "P2/C"); 188 mv.visitInsn(DUP); 189 mv.visitMethodInsn(INVOKESPECIAL, "P2/C", "<init>", "()V", false); 190 mv.visitMethodInsn(INVOKEVIRTUAL, "P1/A", "m", "()I", false); 191 mv.visitInsn(IRETURN); 192 mv.visitMaxs(3, 2); 193 mv.visitEnd(); 194 } 195 196 cw.visitEnd(); 197 198 return cw.toByteArray(); 199 } 200} 201