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.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Nov 04 18:51:56 2013 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Wed Nov 06 17:48:25 2013 +0100 1.3 @@ -1378,7 +1378,11 @@ 1.4 ref = make.Ident(sym); 1.5 args = make.Idents(md.params); 1.6 } else { 1.7 - ref = make.Select(make.Ident(md.params.head), sym); 1.8 + Symbol msym = sym; 1.9 + if (sym.owner.isInterface()) { 1.10 + msym = msym.clone(types.supertype(accessor.owner.type).tsym); 1.11 + } 1.12 + ref = make.Select(make.Ident(md.params.head), msym); 1.13 args = make.Idents(md.params.tail); 1.14 } 1.15 JCStatement stat; // The statement accessing the private symbol.
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/defaultMethods/super/TestDirectSuperInterfaceInvoke.java Wed Nov 06 17:48:25 2013 +0100 2.3 @@ -0,0 +1,107 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8027281 2.30 + * @summary As per JVMS 4.9.2, invokespecial can only refer to direct superinterfaces 2.31 + * @compile TestDirectSuperInterfaceInvoke.java 2.32 + * @run main TestDirectSuperInterfaceInvoke 2.33 + */ 2.34 + 2.35 +import java.io.File; 2.36 +import com.sun.tools.classfile.Attribute; 2.37 +import com.sun.tools.classfile.ClassFile; 2.38 +import com.sun.tools.classfile.Code_attribute; 2.39 +import com.sun.tools.classfile.ConstantPool.CPRefInfo; 2.40 +import com.sun.tools.classfile.Instruction; 2.41 +import com.sun.tools.classfile.Method; 2.42 +import com.sun.tools.classfile.Opcode; 2.43 + 2.44 +interface BaseInterface { 2.45 + public default int testedMethod(){ return 1; } 2.46 +} 2.47 + 2.48 +interface IntermediateInterface extends BaseInterface { 2.49 +} 2.50 + 2.51 +interface TestInterface extends IntermediateInterface { 2.52 + public default void test() { 2.53 + IntermediateInterface.super.testedMethod(); 2.54 + } 2.55 +} 2.56 + 2.57 +abstract class BaseClass implements BaseInterface { } 2.58 + 2.59 +class TestClass extends BaseClass implements BaseInterface { 2.60 + public int testedMethod() {return 9;} 2.61 + public void test() { 2.62 + if (super.testedMethod() != 1) 2.63 + throw new IllegalStateException(); 2.64 + if (TestClass.super.testedMethod() != 1) 2.65 + throw new IllegalStateException(); 2.66 + new Runnable() { 2.67 + public void run() { 2.68 + if (TestClass.super.testedMethod() != 1) 2.69 + throw new IllegalStateException(); 2.70 + } 2.71 + }.run(); 2.72 + } 2.73 +} 2.74 + 2.75 +public class TestDirectSuperInterfaceInvoke { 2.76 + public static void main(String... args) throws Exception { 2.77 + new TestDirectSuperInterfaceInvoke().run(); 2.78 + } 2.79 + 2.80 + public void run() throws Exception { 2.81 + new TestClass().test(); 2.82 + verifyDefaultBody("TestClass.class"); 2.83 + new TestInterface() {}.test(); 2.84 + verifyDefaultBody("TestInterface.class"); 2.85 + } 2.86 + 2.87 + void verifyDefaultBody(String classFile) { 2.88 + String workDir = System.getProperty("test.classes"); 2.89 + File file = new File(workDir, classFile); 2.90 + try { 2.91 + final ClassFile cf = ClassFile.read(file); 2.92 + for (Method m : cf.methods) { 2.93 + Code_attribute codeAttr = (Code_attribute)m.attributes.get(Attribute.Code); 2.94 + for (Instruction instr : codeAttr.getInstructions()) { 2.95 + if (instr.getOpcode() == Opcode.INVOKESPECIAL) { 2.96 + int pc_index = instr.getShort(1); 2.97 + CPRefInfo ref = (CPRefInfo)cf.constant_pool.get(pc_index); 2.98 + String className = ref.getClassName(); 2.99 + if (className.equals("BaseInterface")) 2.100 + throw new IllegalStateException("Must not directly refer to TestedInterface"); 2.101 + } 2.102 + } 2.103 + } 2.104 + } catch (Exception e) { 2.105 + e.printStackTrace(); 2.106 + throw new Error("error reading " + file +": " + e); 2.107 + } 2.108 + } 2.109 + 2.110 +}