8026894: Improve VerifyError message about overriding a final method
authorhseigel
Thu, 03 Jul 2014 10:25:42 -0400
changeset 25470 4e69740b1547
parent 25469 3bcfa1db9717
child 25471 3ab9867d7786
8026894: Improve VerifyError message about overriding a final method Summary: Add full class name to error message. Reviewed-by: fparain, zgu, lfoltan
hotspot/src/share/vm/classfile/classFileParser.cpp
hotspot/test/runtime/verifier/OverriderMsg.java
--- a/hotspot/src/share/vm/classfile/classFileParser.cpp	Wed Jul 02 20:20:45 2014 -0400
+++ b/hotspot/src/share/vm/classfile/classFileParser.cpp	Thu Jul 03 10:25:42 2014 -0400
@@ -4590,8 +4590,9 @@
             Exceptions::fthrow(
               THREAD_AND_LOCATION,
               vmSymbols::java_lang_VerifyError(),
-              "class %s overrides final method %s.%s",
+              "class %s overrides final method %s.%s%s",
               this_klass->external_name(),
+              super_m->method_holder()->external_name(),
               name->as_C_string(),
               signature->as_C_string()
             );
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/verifier/OverriderMsg.java	Thu Jul 03 10:25:42 2014 -0400
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.io.FileOutputStream;
+import jdk.internal.org.objectweb.asm.ClassWriter;
+import jdk.internal.org.objectweb.asm.MethodVisitor;
+import static jdk.internal.org.objectweb.asm.Opcodes.*;
+import com.oracle.java.testlibrary.*;
+
+/*
+ * @test OverriderMsg
+ * @bug 8026894
+ * @library /testlibrary
+ * @compile -XDignore.symbol.file OverriderMsg.java
+ * @run main/othervm OverriderMsg
+ */
+
+// This test checks that the super class name is included in the message when
+// a method is detected overriding a final method in its super class.  The
+// asm part of the test creates these two classes:
+//
+//     public class HasFinal {
+//         public final void m(String s) { }
+//     }
+//
+//     public class Overrider extends HasFinal {
+//         public void m(String s) { }
+//         public static void main(String[] args) { }
+//     }
+//
+public class OverriderMsg {
+
+    public static void dump_HasFinal () throws Exception {
+
+        ClassWriter cw = new ClassWriter(0);
+        MethodVisitor mv;
+
+        cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "HasFinal", null, "java/lang/Object", null);
+
+        {
+            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+            mv.visitCode();
+            mv.visitVarInsn(ALOAD, 0);
+            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(1, 1);
+            mv.visitEnd();
+        }
+        {
+            mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "m", "(Ljava/lang/String;)V", null, null);
+            mv.visitCode();
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(0, 2);
+            mv.visitEnd();
+        }
+        cw.visitEnd();
+        try (FileOutputStream fos = new FileOutputStream(new File("HasFinal.class"))) {
+             fos.write(cw.toByteArray());
+        }
+    }
+
+
+    public static void dump_Overrider () throws Exception {
+
+        ClassWriter cw = new ClassWriter(0);
+        MethodVisitor mv;
+        cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "Overrider", null, "HasFinal", null);
+
+        {
+            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
+            mv.visitCode();
+            mv.visitVarInsn(ALOAD, 0);
+            mv.visitMethodInsn(INVOKESPECIAL, "HasFinal", "<init>", "()V");
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(1, 1);
+            mv.visitEnd();
+        }
+        {
+            mv = cw.visitMethod(ACC_PUBLIC, "m", "(Ljava/lang/String;)V", null, null);
+            mv.visitCode();
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(0, 2);
+            mv.visitEnd();
+        }
+        {
+            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
+            mv.visitCode();
+            mv.visitInsn(RETURN);
+            mv.visitMaxs(0, 1);
+            mv.visitEnd();
+        }
+        cw.visitEnd();
+
+        try (FileOutputStream fos = new FileOutputStream(new File("Overrider.class"))) {
+             fos.write(cw.toByteArray());
+        }
+    }
+
+
+    public static void main(String... args) throws Exception {
+        dump_HasFinal();
+        dump_Overrider();
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-cp", ".",  "Overrider");
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        output.shouldContain(
+            "java.lang.VerifyError: class Overrider overrides final method HasFinal.m(Ljava/lang/String;)V");
+        output.shouldHaveExitValue(1);
+    }
+
+}