1 /*
2  * Copyright (c) 2014, 2021, 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 import java.io.File;
25 import java.io.FileOutputStream;
26 import jdk.internal.org.objectweb.asm.ClassWriter;
27 import jdk.internal.org.objectweb.asm.MethodVisitor;
28 import static jdk.internal.org.objectweb.asm.Opcodes.*;
29 import jdk.test.lib.process.ProcessTools;
30 import 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 //
56 public class OverriderMsg {
57 
dump_HasFinal()58     public static void dump_HasFinal () throws Exception {
59 
60         ClassWriter cw = new ClassWriter(0);
61         MethodVisitor mv;
62 
63         cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "HasFinal", null, "java/lang/Object", null);
64 
65         {
66             mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
67             mv.visitCode();
68             mv.visitVarInsn(ALOAD, 0);
69             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
70             mv.visitInsn(RETURN);
71             mv.visitMaxs(1, 1);
72             mv.visitEnd();
73         }
74         {
75             mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "m", "(Ljava/lang/String;)V", null, null);
76             mv.visitCode();
77             mv.visitInsn(RETURN);
78             mv.visitMaxs(0, 2);
79             mv.visitEnd();
80         }
81         cw.visitEnd();
82         try (FileOutputStream fos = new FileOutputStream(new File("HasFinal.class"))) {
83              fos.write(cw.toByteArray());
84         }
85     }
86 
87 
dump_Overrider()88     public static void dump_Overrider () throws Exception {
89 
90         ClassWriter cw = new ClassWriter(0);
91         MethodVisitor mv;
92         cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "Overrider", null, "HasFinal", null);
93 
94         {
95             mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
96             mv.visitCode();
97             mv.visitVarInsn(ALOAD, 0);
98             mv.visitMethodInsn(INVOKESPECIAL, "HasFinal", "<init>", "()V");
99             mv.visitInsn(RETURN);
100             mv.visitMaxs(1, 1);
101             mv.visitEnd();
102         }
103         {
104             mv = cw.visitMethod(ACC_PUBLIC, "m", "(Ljava/lang/String;)V", null, null);
105             mv.visitCode();
106             mv.visitInsn(RETURN);
107             mv.visitMaxs(0, 2);
108             mv.visitEnd();
109         }
110         {
111             mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
112             mv.visitCode();
113             mv.visitInsn(RETURN);
114             mv.visitMaxs(0, 1);
115             mv.visitEnd();
116         }
117         cw.visitEnd();
118 
119         try (FileOutputStream fos = new FileOutputStream(new File("Overrider.class"))) {
120              fos.write(cw.toByteArray());
121         }
122     }
123 
124 
main(String... args)125     public static void main(String... args) throws Exception {
126         dump_HasFinal();
127         dump_Overrider();
128         ProcessBuilder pb = ProcessTools.createTestJvm("-cp", ".",  "Overrider");
129         OutputAnalyzer output = new OutputAnalyzer(pb.start());
130         output.shouldContain(
131             "java.lang.IncompatibleClassChangeError: class Overrider overrides final method HasFinal.m(Ljava/lang/String;)V");
132         output.shouldHaveExitValue(1);
133     }
134 
135 }
136