Mon, 18 Oct 2010 19:14:36 +0100
6991980: polymorphic signature calls don't share the same CP entries
Summary: wrong use of attr env in Infer.java prevents sharing of CP entries for PS calls
Reviewed-by: darcy, jrose
src/share/classes/com/sun/tools/javac/comp/Infer.java | file | annotate | diff | comparison | revisions | |
test/tools/javac/meth/TestCP.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Infer.java Wed Oct 13 17:52:29 2010 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Infer.java Mon Oct 18 19:14:36 2010 +0100 1.3 @@ -553,12 +553,24 @@ 1.4 //the enclosing tree E, as follows: if E is a cast, then use the 1.5 //target type of the cast expression as a return type; if E is an 1.6 //expression statement, the return type is 'void' - otherwise the 1.7 - //return type is simply 'Object'. 1.8 - switch (env.outer.tree.getTag()) { 1.9 + //return type is simply 'Object'. A correctness check ensures that 1.10 + //env.next refers to the lexically enclosing environment in which 1.11 + //the polymorphic signature call environment is nested. 1.12 + 1.13 + switch (env.next.tree.getTag()) { 1.14 case JCTree.TYPECAST: 1.15 - restype = ((JCTypeCast)env.outer.tree).clazz.type; break; 1.16 + JCTypeCast castTree = (JCTypeCast)env.next.tree; 1.17 + restype = (castTree.expr == env.tree) ? 1.18 + castTree.clazz.type : 1.19 + syms.objectType; 1.20 + break; 1.21 case JCTree.EXEC: 1.22 - restype = syms.voidType; break; 1.23 + JCTree.JCExpressionStatement execTree = 1.24 + (JCTree.JCExpressionStatement)env.next.tree; 1.25 + restype = (execTree.expr == env.tree) ? 1.26 + syms.voidType : 1.27 + syms.objectType; 1.28 + break; 1.29 default: 1.30 restype = syms.objectType; 1.31 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/meth/TestCP.java Mon Oct 18 19:14:36 2010 +0100 2.3 @@ -0,0 +1,111 @@ 2.4 +/* 2.5 + * Copyright (c) 2010, 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 6991980 2.30 + * @summary polymorphic signature calls don't share the same CP entries 2.31 + * @run main TestCP 2.32 + */ 2.33 + 2.34 +import com.sun.tools.classfile.Instruction; 2.35 +import com.sun.tools.classfile.Attribute; 2.36 +import com.sun.tools.classfile.ClassFile; 2.37 +import com.sun.tools.classfile.Code_attribute; 2.38 +import com.sun.tools.classfile.ConstantPool.*; 2.39 +import com.sun.tools.classfile.Method; 2.40 + 2.41 +import java.dyn.*; 2.42 +import java.io.*; 2.43 + 2.44 +public class TestCP { 2.45 + 2.46 + static class TestClass { 2.47 + void test(MethodHandle mh) throws Throwable { 2.48 + Number n = mh.<Number>invokeExact("daddy",1,'n'); 2.49 + n = (Number)mh.invokeExact("bunny",1,'d'); 2.50 + } 2.51 + } 2.52 + 2.53 + static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;"; 2.54 + static final int PS_CALLS_COUNT = 2; 2.55 + static final String SUBTEST_NAME = TestClass.class.getName() + ".class"; 2.56 + static final String TEST_METHOD_NAME = "test"; 2.57 + 2.58 + public static void main(String... args) throws Exception { 2.59 + new TestCP().run(); 2.60 + } 2.61 + 2.62 + public void run() throws Exception { 2.63 + String workDir = System.getProperty("test.classes"); 2.64 + File compiledTest = new File(workDir, SUBTEST_NAME); 2.65 + verifyMethodHandleInvocationDescriptors(compiledTest); 2.66 + } 2.67 + 2.68 + void verifyMethodHandleInvocationDescriptors(File f) { 2.69 + System.err.println("verify: " + f); 2.70 + try { 2.71 + int count = 0; 2.72 + ClassFile cf = ClassFile.read(f); 2.73 + Method testMethod = null; 2.74 + for (Method m : cf.methods) { 2.75 + if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) { 2.76 + testMethod = m; 2.77 + break; 2.78 + } 2.79 + } 2.80 + if (testMethod == null) { 2.81 + throw new Error("Test method not found"); 2.82 + } 2.83 + Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); 2.84 + if (testMethod == null) { 2.85 + throw new Error("Code attribute for test() method not found"); 2.86 + } 2.87 + int instr_count = 0; 2.88 + int cp_entry = -1; 2.89 + 2.90 + for (Instruction i : ea.getInstructions()) { 2.91 + if (i.getMnemonic().equals("invokevirtual")) { 2.92 + instr_count++; 2.93 + if (cp_entry == -1) { 2.94 + cp_entry = i.getUnsignedShort(1); 2.95 + } else if (cp_entry != i.getUnsignedShort(1)) { 2.96 + throw new Error("Unexpected CP entry in polymorphic signature call"); 2.97 + } 2.98 + CONSTANT_Methodref_info methRef = 2.99 + (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry); 2.100 + String type = methRef.getNameAndTypeInfo().getType(); 2.101 + if (!type.equals(PS_TYPE)) { 2.102 + throw new Error("Unexpected type in polymorphic signature call: " + type); 2.103 + } 2.104 + } 2.105 + } 2.106 + if (instr_count != PS_CALLS_COUNT) { 2.107 + throw new Error("Wrong number of polymorphic signature call found: " + instr_count); 2.108 + } 2.109 + } catch (Exception e) { 2.110 + e.printStackTrace(); 2.111 + throw new Error("error reading " + f +": " + e); 2.112 + } 2.113 + } 2.114 +}