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