mcimadamore@716: /* ohair@962: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. mcimadamore@716: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@716: * mcimadamore@716: * This code is free software; you can redistribute it and/or modify it mcimadamore@716: * under the terms of the GNU General Public License version 2 only, as mcimadamore@716: * published by the Free Software Foundation. mcimadamore@716: * mcimadamore@716: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@716: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@716: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@716: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@716: * accompanied this code). mcimadamore@716: * mcimadamore@716: * You should have received a copy of the GNU General Public License version mcimadamore@716: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@716: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@716: * mcimadamore@716: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@716: * or visit www.oracle.com if you need additional information or have any mcimadamore@716: * questions. mcimadamore@716: */ mcimadamore@716: mcimadamore@716: /* mcimadamore@716: * @test mcimadamore@716: * @bug 6991980 mcimadamore@716: * @summary polymorphic signature calls don't share the same CP entries mcimadamore@716: * @run main TestCP mcimadamore@716: */ mcimadamore@716: mcimadamore@716: import com.sun.tools.classfile.Instruction; mcimadamore@716: import com.sun.tools.classfile.Attribute; mcimadamore@716: import com.sun.tools.classfile.ClassFile; mcimadamore@716: import com.sun.tools.classfile.Code_attribute; mcimadamore@716: import com.sun.tools.classfile.ConstantPool.*; mcimadamore@716: import com.sun.tools.classfile.Method; mcimadamore@716: ksrini@957: import java.lang.invoke.*; mcimadamore@716: import java.io.*; mcimadamore@716: mcimadamore@716: public class TestCP { mcimadamore@716: mcimadamore@716: static class TestClass { mcimadamore@716: void test(MethodHandle mh) throws Throwable { mcimadamore@820: Number n = (Number)mh.invokeExact("daddy",1,'n'); mcimadamore@716: n = (Number)mh.invokeExact("bunny",1,'d'); mcimadamore@820: n = (Number)(mh.invokeExact("foo",1,'d')); mcimadamore@820: n = (Number)((mh.invokeExact("bar",1,'d'))); mcimadamore@716: } mcimadamore@716: } mcimadamore@716: mcimadamore@716: static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;"; mcimadamore@820: static final int PS_CALLS_COUNT = 4; mcimadamore@716: static final String SUBTEST_NAME = TestClass.class.getName() + ".class"; mcimadamore@716: static final String TEST_METHOD_NAME = "test"; mcimadamore@716: mcimadamore@716: public static void main(String... args) throws Exception { mcimadamore@716: new TestCP().run(); mcimadamore@716: } mcimadamore@716: mcimadamore@716: public void run() throws Exception { mcimadamore@716: String workDir = System.getProperty("test.classes"); mcimadamore@716: File compiledTest = new File(workDir, SUBTEST_NAME); mcimadamore@716: verifyMethodHandleInvocationDescriptors(compiledTest); mcimadamore@716: } mcimadamore@716: mcimadamore@716: void verifyMethodHandleInvocationDescriptors(File f) { mcimadamore@716: System.err.println("verify: " + f); mcimadamore@716: try { mcimadamore@716: int count = 0; mcimadamore@716: ClassFile cf = ClassFile.read(f); mcimadamore@716: Method testMethod = null; mcimadamore@716: for (Method m : cf.methods) { mcimadamore@716: if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) { mcimadamore@716: testMethod = m; mcimadamore@716: break; mcimadamore@716: } mcimadamore@716: } mcimadamore@716: if (testMethod == null) { mcimadamore@716: throw new Error("Test method not found"); mcimadamore@716: } mcimadamore@716: Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); mcimadamore@716: if (testMethod == null) { mcimadamore@716: throw new Error("Code attribute for test() method not found"); mcimadamore@716: } mcimadamore@716: int instr_count = 0; mcimadamore@716: int cp_entry = -1; mcimadamore@716: mcimadamore@716: for (Instruction i : ea.getInstructions()) { mcimadamore@716: if (i.getMnemonic().equals("invokevirtual")) { mcimadamore@716: instr_count++; mcimadamore@716: if (cp_entry == -1) { mcimadamore@716: cp_entry = i.getUnsignedShort(1); mcimadamore@716: } else if (cp_entry != i.getUnsignedShort(1)) { mcimadamore@716: throw new Error("Unexpected CP entry in polymorphic signature call"); mcimadamore@716: } mcimadamore@716: CONSTANT_Methodref_info methRef = mcimadamore@716: (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry); mcimadamore@716: String type = methRef.getNameAndTypeInfo().getType(); mcimadamore@716: if (!type.equals(PS_TYPE)) { mcimadamore@716: throw new Error("Unexpected type in polymorphic signature call: " + type); mcimadamore@716: } mcimadamore@716: } mcimadamore@716: } mcimadamore@716: if (instr_count != PS_CALLS_COUNT) { mcimadamore@716: throw new Error("Wrong number of polymorphic signature call found: " + instr_count); mcimadamore@716: } mcimadamore@716: } catch (Exception e) { mcimadamore@716: e.printStackTrace(); mcimadamore@716: throw new Error("error reading " + f +": " + e); mcimadamore@716: } mcimadamore@716: } mcimadamore@716: }