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