jdk
1/*
2* Copyright (c) 2014, 2023, 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
24import java.io.File;25import java.io.FileOutputStream;26import jdk.internal.org.objectweb.asm.ClassWriter;27import jdk.internal.org.objectweb.asm.MethodVisitor;28import static jdk.internal.org.objectweb.asm.Opcodes.*;29import jdk.test.lib.process.ProcessTools;30import jdk.test.lib.process.OutputAnalyzer;31
32/*
33* @test OverriderMsg
34* @bug 8026894
35* @library /test/lib
36* @modules java.base/jdk.internal.org.objectweb.asm
37* java.base/jdk.internal.misc
38* java.management
39* @compile -XDignore.symbol.file OverriderMsg.java
40* @run driver OverriderMsg
41*/
42
43// This test checks that the super class name is included in the message when
44// a method is detected overriding a final method in its super class. The
45// asm part of the test creates these two classes:
46//
47// public class HasFinal {
48// public final void m(String s) { }
49// }
50//
51// public class Overrider extends HasFinal {
52// public void m(String s) { }
53// public static void main(String[] args) { }
54// }
55//
56public class OverriderMsg {57
58public static void dump_HasFinal () throws Exception {59
60ClassWriter cw = new ClassWriter(0);61MethodVisitor mv;62
63cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "HasFinal", null, "java/lang/Object", null);64
65{66mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);67mv.visitCode();68mv.visitVarInsn(ALOAD, 0);69mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");70mv.visitInsn(RETURN);71mv.visitMaxs(1, 1);72mv.visitEnd();73}74{75mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "m", "(Ljava/lang/String;)V", null, null);76mv.visitCode();77mv.visitInsn(RETURN);78mv.visitMaxs(0, 2);79mv.visitEnd();80}81cw.visitEnd();82try (FileOutputStream fos = new FileOutputStream(new File("HasFinal.class"))) {83fos.write(cw.toByteArray());84}85}86
87
88public static void dump_Overrider () throws Exception {89
90ClassWriter cw = new ClassWriter(0);91MethodVisitor mv;92cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "Overrider", null, "HasFinal", null);93
94{95mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);96mv.visitCode();97mv.visitVarInsn(ALOAD, 0);98mv.visitMethodInsn(INVOKESPECIAL, "HasFinal", "<init>", "()V");99mv.visitInsn(RETURN);100mv.visitMaxs(1, 1);101mv.visitEnd();102}103{104mv = cw.visitMethod(ACC_PUBLIC, "m", "(Ljava/lang/String;)V", null, null);105mv.visitCode();106mv.visitInsn(RETURN);107mv.visitMaxs(0, 2);108mv.visitEnd();109}110{111mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);112mv.visitCode();113mv.visitInsn(RETURN);114mv.visitMaxs(0, 1);115mv.visitEnd();116}117cw.visitEnd();118
119try (FileOutputStream fos = new FileOutputStream(new File("Overrider.class"))) {120fos.write(cw.toByteArray());121}122}123
124
125public static void main(String... args) throws Exception {126dump_HasFinal();127dump_Overrider();128ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder("-cp", ".", "Overrider");129OutputAnalyzer output = new OutputAnalyzer(pb.start());130output.shouldContain(131"java.lang.IncompatibleClassChangeError: class Overrider overrides final method HasFinal.m(Ljava/lang/String;)V");132output.shouldHaveExitValue(1);133}134
135}
136