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