1/*
2 * Copyright (c) 2013, 2015, 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 8027281
27 * @summary As per JVMS 4.9.2, invokespecial can only refer to direct superinterfaces
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 * @compile TestDirectSuperInterfaceInvoke.java
30 * @run main TestDirectSuperInterfaceInvoke
31 */
32
33import java.io.File;
34import com.sun.tools.classfile.Attribute;
35import com.sun.tools.classfile.ClassFile;
36import com.sun.tools.classfile.Code_attribute;
37import com.sun.tools.classfile.ConstantPool.CPRefInfo;
38import com.sun.tools.classfile.Instruction;
39import com.sun.tools.classfile.Method;
40import com.sun.tools.classfile.Opcode;
41
42interface BaseInterface {
43    public default int testedMethod(){ return 1; }
44}
45
46interface IntermediateInterface extends BaseInterface {
47}
48
49interface TestInterface extends IntermediateInterface {
50    public default void test() {
51        IntermediateInterface.super.testedMethod();
52    }
53}
54
55abstract class BaseClass implements BaseInterface { }
56
57class TestClass extends BaseClass implements BaseInterface {
58    public int testedMethod() {return 9;}
59    public void test() {
60        if (super.testedMethod() != 1)
61            throw new IllegalStateException();
62        if (TestClass.super.testedMethod() != 1)
63            throw new IllegalStateException();
64        new Runnable() {
65            public void run() {
66                if (TestClass.super.testedMethod() != 1)
67                    throw new IllegalStateException();
68            }
69        }.run();
70    }
71}
72
73public class TestDirectSuperInterfaceInvoke {
74    public static void main(String... args) throws Exception {
75        new TestDirectSuperInterfaceInvoke().run();
76    }
77
78    public void run() throws Exception {
79        new TestClass().test();
80        verifyDefaultBody("TestClass.class");
81        new TestInterface() {}.test();
82        verifyDefaultBody("TestInterface.class");
83    }
84
85    void verifyDefaultBody(String classFile) {
86        String workDir = System.getProperty("test.classes");
87        File file = new File(workDir, classFile);
88        try {
89            final ClassFile cf = ClassFile.read(file);
90            for (Method m : cf.methods) {
91                Code_attribute codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
92                for (Instruction instr : codeAttr.getInstructions()) {
93                    if (instr.getOpcode() == Opcode.INVOKESPECIAL) {
94                        int pc_index = instr.getShort(1);
95                        CPRefInfo ref = (CPRefInfo)cf.constant_pool.get(pc_index);
96                        String className = ref.getClassName();
97                        if (className.equals("BaseInterface"))
98                            throw new IllegalStateException("Must not directly refer to TestedInterface");
99                    }
100                }
101            }
102        } catch (Exception e) {
103            e.printStackTrace();
104            throw new Error("error reading " + file +": " + e);
105        }
106    }
107
108}
109