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