hotspot/test/runtime/finalStatic/FinalStatic.java
changeset 22534 520f7a937396
child 29678 dd2f3932c21e
equal deleted inserted replaced
22532:1fc87ea15795 22534:520f7a937396
       
     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 /*
       
    25  * @test
       
    26  * @bug 8028553
       
    27  * @summary Test that VerifyError is not thrown when 'overriding' a static method.
       
    28  * @run main FinalStatic
       
    29  */
       
    30 
       
    31 import java.lang.reflect.*;
       
    32 import jdk.internal.org.objectweb.asm.ClassWriter;
       
    33 import jdk.internal.org.objectweb.asm.MethodVisitor;
       
    34 import jdk.internal.org.objectweb.asm.Opcodes;
       
    35 
       
    36 /*
       
    37  *  class A { static final int m() {return FAIL; } }
       
    38  *  class B extends A { int m() { return PASS; } }
       
    39  *  class FinalStatic {
       
    40  *      public static void main () {
       
    41  *          Object b = new B();
       
    42  *          b.m();
       
    43  *      }
       
    44  *  }
       
    45  */
       
    46 public class FinalStatic {
       
    47 
       
    48     static final String CLASS_NAME_A = "A";
       
    49     static final String CLASS_NAME_B = "B";
       
    50     static final int FAILED = 0;
       
    51     static final int EXPECTED = 1234;
       
    52 
       
    53     static class TestClassLoader extends ClassLoader implements Opcodes {
       
    54 
       
    55         @Override
       
    56         public Class findClass(String name) throws ClassNotFoundException {
       
    57             byte[] b;
       
    58             try {
       
    59                 b = loadClassData(name);
       
    60             } catch (Throwable th) {
       
    61                 // th.printStackTrace();
       
    62                 throw new ClassNotFoundException("Loading error", th);
       
    63             }
       
    64             return defineClass(name, b, 0, b.length);
       
    65         }
       
    66 
       
    67         private byte[] loadClassData(String name) throws Exception {
       
    68             ClassWriter cw = new ClassWriter(0);
       
    69             MethodVisitor mv;
       
    70             switch (name) {
       
    71                case CLASS_NAME_A:
       
    72                     cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME_A, null, "java/lang/Object", null);
       
    73                     {
       
    74                         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
       
    75                         mv.visitCode();
       
    76                         mv.visitVarInsn(ALOAD, 0);
       
    77                         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
       
    78                         mv.visitInsn(RETURN);
       
    79                         mv.visitMaxs(1, 1);
       
    80                         mv.visitEnd();
       
    81 
       
    82                         mv = cw.visitMethod(ACC_FINAL | ACC_STATIC, "m", "()I", null, null);
       
    83                         mv.visitCode();
       
    84                         mv.visitLdcInsn(FAILED);
       
    85                         mv.visitInsn(IRETURN);
       
    86                         mv.visitMaxs(1, 1);
       
    87                         mv.visitEnd();
       
    88                     }
       
    89                     break;
       
    90                 case CLASS_NAME_B:
       
    91                     cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME_B, null, CLASS_NAME_A, null);
       
    92                     {
       
    93                         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
       
    94                         mv.visitCode();
       
    95                         mv.visitVarInsn(ALOAD, 0);
       
    96                         mv.visitMethodInsn(INVOKESPECIAL, CLASS_NAME_A, "<init>", "()V");
       
    97                         mv.visitInsn(RETURN);
       
    98                         mv.visitMaxs(1, 1);
       
    99                         mv.visitEnd();
       
   100 
       
   101                         mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
       
   102                         mv.visitCode();
       
   103                         mv.visitLdcInsn(EXPECTED);
       
   104                         mv.visitInsn(IRETURN);
       
   105                         mv.visitMaxs(1, 1);
       
   106                         mv.visitEnd();
       
   107 
       
   108                     }
       
   109                     break;
       
   110                 default:
       
   111                     break;
       
   112             }
       
   113             cw.visitEnd();
       
   114 
       
   115             return cw.toByteArray();
       
   116         }
       
   117     }
       
   118 
       
   119     public static void main(String[] args) throws Exception {
       
   120         TestClassLoader tcl = new TestClassLoader();
       
   121         Class<?> a = tcl.loadClass(CLASS_NAME_A);
       
   122         Class<?> b = tcl.loadClass(CLASS_NAME_B);
       
   123         Object inst = b.newInstance();
       
   124         Method[] meths = b.getDeclaredMethods();
       
   125 
       
   126         Method m = meths[0];
       
   127         int mod = m.getModifiers();
       
   128         if ((mod & Modifier.FINAL) != 0) {
       
   129             throw new Exception("FAILED: " + m + " is FINAL");
       
   130         }
       
   131         if ((mod & Modifier.STATIC) != 0) {
       
   132             throw new Exception("FAILED: " + m + " is STATIC");
       
   133         }
       
   134 
       
   135         m.setAccessible(true);
       
   136         if (!m.invoke(inst).equals(EXPECTED)) {
       
   137               throw new Exception("FAILED: " + EXPECTED + " from " + m);
       
   138         }
       
   139 
       
   140         System.out.println("Passed.");
       
   141     }
       
   142 }