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