1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/meth/TestCP.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,113 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6991980 1.30 + * @summary polymorphic signature calls don't share the same CP entries 1.31 + * @run main TestCP 1.32 + */ 1.33 + 1.34 +import com.sun.tools.classfile.Instruction; 1.35 +import com.sun.tools.classfile.Attribute; 1.36 +import com.sun.tools.classfile.ClassFile; 1.37 +import com.sun.tools.classfile.Code_attribute; 1.38 +import com.sun.tools.classfile.ConstantPool.*; 1.39 +import com.sun.tools.classfile.Method; 1.40 + 1.41 +import java.lang.invoke.*; 1.42 +import java.io.*; 1.43 + 1.44 +public class TestCP { 1.45 + 1.46 + static class TestClass { 1.47 + void test(MethodHandle mh) throws Throwable { 1.48 + Number n = (Number)mh.invokeExact("daddy",1,'n'); 1.49 + n = (Number)mh.invokeExact("bunny",1,'d'); 1.50 + n = (Number)(mh.invokeExact("foo",1,'d')); 1.51 + n = (Number)((mh.invokeExact("bar",1,'d'))); 1.52 + } 1.53 + } 1.54 + 1.55 + static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;"; 1.56 + static final int PS_CALLS_COUNT = 4; 1.57 + static final String SUBTEST_NAME = TestClass.class.getName() + ".class"; 1.58 + static final String TEST_METHOD_NAME = "test"; 1.59 + 1.60 + public static void main(String... args) throws Exception { 1.61 + new TestCP().run(); 1.62 + } 1.63 + 1.64 + public void run() throws Exception { 1.65 + String workDir = System.getProperty("test.classes"); 1.66 + File compiledTest = new File(workDir, SUBTEST_NAME); 1.67 + verifyMethodHandleInvocationDescriptors(compiledTest); 1.68 + } 1.69 + 1.70 + void verifyMethodHandleInvocationDescriptors(File f) { 1.71 + System.err.println("verify: " + f); 1.72 + try { 1.73 + int count = 0; 1.74 + ClassFile cf = ClassFile.read(f); 1.75 + Method testMethod = null; 1.76 + for (Method m : cf.methods) { 1.77 + if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) { 1.78 + testMethod = m; 1.79 + break; 1.80 + } 1.81 + } 1.82 + if (testMethod == null) { 1.83 + throw new Error("Test method not found"); 1.84 + } 1.85 + Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); 1.86 + if (testMethod == null) { 1.87 + throw new Error("Code attribute for test() method not found"); 1.88 + } 1.89 + int instr_count = 0; 1.90 + int cp_entry = -1; 1.91 + 1.92 + for (Instruction i : ea.getInstructions()) { 1.93 + if (i.getMnemonic().equals("invokevirtual")) { 1.94 + instr_count++; 1.95 + if (cp_entry == -1) { 1.96 + cp_entry = i.getUnsignedShort(1); 1.97 + } else if (cp_entry != i.getUnsignedShort(1)) { 1.98 + throw new Error("Unexpected CP entry in polymorphic signature call"); 1.99 + } 1.100 + CONSTANT_Methodref_info methRef = 1.101 + (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry); 1.102 + String type = methRef.getNameAndTypeInfo().getType(); 1.103 + if (!type.equals(PS_TYPE)) { 1.104 + throw new Error("Unexpected type in polymorphic signature call: " + type); 1.105 + } 1.106 + } 1.107 + } 1.108 + if (instr_count != PS_CALLS_COUNT) { 1.109 + throw new Error("Wrong number of polymorphic signature call found: " + instr_count); 1.110 + } 1.111 + } catch (Exception e) { 1.112 + e.printStackTrace(); 1.113 + throw new Error("error reading " + f +": " + e); 1.114 + } 1.115 + } 1.116 +}