test/runtime/lambda-features/TestConcreteClassWithAbstractMethod.java

changeset 6308
a81bc2b2c4d3
parent 0
f90c822e73f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/runtime/lambda-features/TestConcreteClassWithAbstractMethod.java	Wed Feb 05 15:14:47 2014 -0800
     1.3 @@ -0,0 +1,181 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/*
    1.29 + * @test
    1.30 + * @bug 8032010
    1.31 + * @summary method lookup on an abstract method in a concrete class should be successful
    1.32 + * @run main TestConcreteClassWithAbstractMethod
    1.33 + */
    1.34 +
    1.35 +import jdk.internal.org.objectweb.asm.ClassWriter;
    1.36 +import jdk.internal.org.objectweb.asm.MethodVisitor;
    1.37 +
    1.38 +import static jdk.internal.org.objectweb.asm.Opcodes.*;
    1.39 +
    1.40 +/*
    1.41 + *   class T1 { public int m() {} }
    1.42 + *   class T2 { public abstract int m(); }
    1.43 + *   class T3 { public int m() {} }
    1.44 + *
    1.45 + *   Call site: T3.test() { invokevirtual T2.m() }
    1.46 + *   T3.m() should be invoked
    1.47 + */
    1.48 +public class TestConcreteClassWithAbstractMethod {
    1.49 +    static final String classT1 = "p1.T1";
    1.50 +    static final String classT2 = "p1.T2";
    1.51 +    static final String classT3 = "p1.T3";
    1.52 +
    1.53 +    static final String callerName = classT3;
    1.54 +
    1.55 +    public static void main(String[] args) throws Exception {
    1.56 +        ClassLoader cl = new ClassLoader() {
    1.57 +            public Class<?> loadClass(String name) throws ClassNotFoundException {
    1.58 +                if (findLoadedClass(name) != null) {
    1.59 +                    return findLoadedClass(name);
    1.60 +                }
    1.61 +
    1.62 +                if (classT1.equals(name)) {
    1.63 +                    byte[] classFile = dumpT1();
    1.64 +                    return defineClass(classT1, classFile, 0, classFile.length);
    1.65 +                }
    1.66 +                if (classT2.equals(name)) {
    1.67 +                    byte[] classFile = dumpT2();
    1.68 +                    return defineClass(classT2, classFile, 0, classFile.length);
    1.69 +                }
    1.70 +                if (classT3.equals(name)) {
    1.71 +                    byte[] classFile = dumpT3();
    1.72 +                    return defineClass(classT3, classFile, 0, classFile.length);
    1.73 +                }
    1.74 +
    1.75 +                return super.loadClass(name);
    1.76 +            }
    1.77 +        };
    1.78 +
    1.79 +        cl.loadClass(classT1);
    1.80 +        cl.loadClass(classT2);
    1.81 +        cl.loadClass(classT3);
    1.82 +
    1.83 +        //cl.loadClass(callerName).getDeclaredMethod("m");
    1.84 +        cl.loadClass(callerName).newInstance();
    1.85 +
    1.86 +        int result = (Integer)cl.loadClass(callerName).getDeclaredMethod("test").invoke(null);
    1.87 +        System.out.println(""+result);
    1.88 +    }
    1.89 +
    1.90 +    public static byte[] dumpT1() {
    1.91 +        ClassWriter cw = new ClassWriter(0);
    1.92 +        MethodVisitor mv;
    1.93 +
    1.94 +        cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T1", null, "java/lang/Object", null);
    1.95 +        {
    1.96 +            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    1.97 +            mv.visitCode();
    1.98 +            mv.visitVarInsn(ALOAD, 0);
    1.99 +            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
   1.100 +            mv.visitInsn(RETURN);
   1.101 +            mv.visitMaxs(1, 1);
   1.102 +            mv.visitEnd();
   1.103 +        }
   1.104 +        {
   1.105 +            mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
   1.106 +            mv.visitCode();
   1.107 +            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
   1.108 +            mv.visitLdcInsn("p1/T1.m()");
   1.109 +            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
   1.110 +            mv.visitIntInsn(BIPUSH, 3);
   1.111 +            mv.visitInsn(IRETURN);
   1.112 +            mv.visitMaxs(2, 1);
   1.113 +            mv.visitEnd();
   1.114 +        }
   1.115 +        cw.visitEnd();
   1.116 +
   1.117 +        return cw.toByteArray();
   1.118 +    }
   1.119 +
   1.120 +    public static byte[] dumpT2() {
   1.121 +        ClassWriter cw = new ClassWriter(0);
   1.122 +        MethodVisitor mv;
   1.123 +
   1.124 +        cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T2", null, "p1/T1", null);
   1.125 +        {
   1.126 +            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
   1.127 +            mv.visitCode();
   1.128 +            mv.visitVarInsn(ALOAD, 0);
   1.129 +            mv.visitMethodInsn(INVOKESPECIAL, "p1/T1", "<init>", "()V", false);
   1.130 +            mv.visitInsn(RETURN);
   1.131 +            mv.visitMaxs(1, 1);
   1.132 +            mv.visitEnd();
   1.133 +        }
   1.134 +        {
   1.135 +            mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null);
   1.136 +            mv.visitEnd();
   1.137 +        }
   1.138 +        cw.visitEnd();
   1.139 +
   1.140 +        return cw.toByteArray();
   1.141 +    }
   1.142 +
   1.143 +    public static byte[] dumpT3() {
   1.144 +        ClassWriter cw = new ClassWriter(0);
   1.145 +        MethodVisitor mv;
   1.146 +
   1.147 +        cw.visit(52, ACC_PUBLIC + ACC_SUPER, "p1/T3", null, "p1/T2", null);
   1.148 +
   1.149 +        {
   1.150 +            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
   1.151 +            mv.visitCode();
   1.152 +            mv.visitVarInsn(ALOAD, 0);
   1.153 +            mv.visitMethodInsn(INVOKESPECIAL, "p1/T2", "<init>", "()V", false);
   1.154 +            mv.visitInsn(RETURN);
   1.155 +            mv.visitMaxs(1, 1);
   1.156 +            mv.visitEnd();
   1.157 +        }
   1.158 +        {
   1.159 +            mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
   1.160 +            mv.visitCode();
   1.161 +            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
   1.162 +            mv.visitLdcInsn("p1/T3.m()");
   1.163 +            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
   1.164 +            mv.visitIntInsn(BIPUSH, 2);
   1.165 +            mv.visitInsn(IRETURN);
   1.166 +            mv.visitMaxs(2, 1);
   1.167 +            mv.visitEnd();
   1.168 +        }
   1.169 +        {
   1.170 +            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
   1.171 +            mv.visitCode();
   1.172 +            mv.visitTypeInsn(NEW, "p1/T3");
   1.173 +            mv.visitInsn(DUP);
   1.174 +            mv.visitMethodInsn(INVOKESPECIAL, "p1/T3", "<init>", "()V", false);
   1.175 +            mv.visitMethodInsn(INVOKEVIRTUAL, "p1/T2", "m", "()I", false);
   1.176 +            mv.visitInsn(IRETURN);
   1.177 +            mv.visitMaxs(3, 2);
   1.178 +            mv.visitEnd();
   1.179 +        }
   1.180 +        cw.visitEnd();
   1.181 +
   1.182 +        return cw.toByteArray();
   1.183 +    }
   1.184 +}

mercurial