1/*
2 * Copyright (c) 2016, 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 8087223
27 * @summary Adding constantTag to keep method call consistent with it.
28 * @modules java.base/jdk.internal.org.objectweb.asm
29 *          java.base/jdk.internal.misc
30 *          java.management
31 * @compile -XDignore.symbol.file IntfMethod.java
32 * @run main/othervm IntfMethod
33 * @run main/othervm -Xint IntfMethod
34 * @run main/othervm -Xcomp IntfMethod
35 */
36
37
38import jdk.internal.org.objectweb.asm.*;
39import java.io.FileOutputStream;
40import java.lang.reflect.InvocationTargetException;
41import static jdk.internal.org.objectweb.asm.Opcodes.*;
42
43public class IntfMethod {
44    static byte[] dumpC() {
45        ClassWriter cw = new ClassWriter(0);
46        cw.visit(52, ACC_PUBLIC | ACC_SUPER, "C", null, "java/lang/Object", new String[]{"I"});
47        {
48            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
49            mv.visitCode();
50            mv.visitVarInsn(ALOAD, 0);
51            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
52            mv.visitInsn(RETURN);
53            mv.visitMaxs(1, 1);
54            mv.visitEnd();
55        }
56        {
57            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "testSpecialIntf", "()V", null, null);
58            mv.visitCode();
59            mv.visitVarInsn(ALOAD, 0);
60            mv.visitMethodInsn(INVOKESPECIAL, "I", "f1", "()V", /*itf=*/false);
61            mv.visitInsn(RETURN);
62            mv.visitMaxs(1, 1);
63            mv.visitEnd();
64        }
65        {
66            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "testStaticIntf", "()V", null, null);
67            mv.visitCode();
68            mv.visitMethodInsn(INVOKESTATIC, "I", "f2", "()V", /*itf=*/false);
69            mv.visitInsn(RETURN);
70            mv.visitMaxs(1, 1);
71            mv.visitEnd();
72        }
73        {
74            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "testSpecialClass", "()V", null, null);
75            mv.visitCode();
76            mv.visitVarInsn(ALOAD, 0);
77            mv.visitMethodInsn(INVOKESPECIAL, "C", "f1", "()V", /*itf=*/true);
78            mv.visitInsn(RETURN);
79            mv.visitMaxs(1, 1);
80            mv.visitEnd();
81        }
82
83        {
84            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f2", "()V", null, null);
85            mv.visitCode();
86            mv.visitInsn(RETURN);
87            mv.visitMaxs(0, 1);
88            mv.visitEnd();
89        }
90        {
91            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "testStaticClass", "()V", null, null);
92            mv.visitCode();
93            mv.visitMethodInsn(INVOKESTATIC, "C", "f2", "()V", /*itf=*/true);
94            mv.visitInsn(RETURN);
95            mv.visitMaxs(1, 1);
96            mv.visitEnd();
97        }
98        cw.visitEnd();
99        return cw.toByteArray();
100    }
101
102    static byte[] dumpI() {
103        ClassWriter cw = new ClassWriter(0);
104        cw.visit(52, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "I", null, "java/lang/Object", null);
105        {
106            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "f1", "()V", null, null);
107            mv.visitCode();
108            mv.visitInsn(RETURN);
109            mv.visitMaxs(0, 1);
110            mv.visitEnd();
111        }
112        {
113            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f2", "()V", null, null);
114            mv.visitCode();
115            mv.visitInsn(RETURN);
116            mv.visitMaxs(0, 1);
117            mv.visitEnd();
118        }
119        cw.visitEnd();
120        return cw.toByteArray();
121    }
122
123    static class CL extends ClassLoader {
124        @Override
125        protected Class<?> findClass(String name) throws ClassNotFoundException {
126            byte[] classFile;
127            switch (name) {
128                case "I": classFile = dumpI(); break;
129                case "C": classFile = dumpC(); break;
130                default:
131                    throw new ClassNotFoundException(name);
132            }
133            return defineClass(name, classFile, 0, classFile.length);
134        }
135    }
136
137    public static void main(String[] args) throws Throwable {
138        Class<?> cls = (new CL()).loadClass("C");
139        try (FileOutputStream fos = new FileOutputStream("I.class")) { fos.write(dumpI()); }
140        try (FileOutputStream fos = new FileOutputStream("C.class")) { fos.write(dumpC()); }
141
142        int success = 0;
143        for (String name : new String[] { "testSpecialIntf", "testStaticIntf", "testSpecialClass", "testStaticClass"}) {
144            System.out.printf("%s: ", name);
145            try {
146                cls.getMethod(name).invoke(cls.newInstance());
147                System.out.println("FAILED - ICCE not thrown");
148            } catch (Throwable e) {
149                if (e instanceof InvocationTargetException &&
150                    e.getCause() != null && e.getCause() instanceof IncompatibleClassChangeError) {
151                    System.out.println("PASSED - expected ICCE thrown");
152                    success++;
153                    continue;
154                }
155            }
156        }
157        if (success != 4) throw new Exception("Failed to catch ICCE");
158    }
159}
160