TestCP.java revision 2942:08092deced3f
1/*
2 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6991980
27 * @summary  polymorphic signature calls don't share the same CP entries
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 * @run main TestCP
30 */
31
32import com.sun.tools.classfile.Instruction;
33import com.sun.tools.classfile.Attribute;
34import com.sun.tools.classfile.ClassFile;
35import com.sun.tools.classfile.Code_attribute;
36import com.sun.tools.classfile.ConstantPool.*;
37import com.sun.tools.classfile.Method;
38
39import java.lang.invoke.*;
40import java.io.*;
41
42public class TestCP {
43
44    static class TestClass {
45        void test(MethodHandle mh) throws Throwable {
46            Number n = (Number)mh.invokeExact("daddy",1,'n');
47            n = (Number)mh.invokeExact("bunny",1,'d');
48            n = (Number)(mh.invokeExact("foo",1,'d'));
49            n = (Number)((mh.invokeExact("bar",1,'d')));
50        }
51    }
52
53    static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
54    static final int PS_CALLS_COUNT = 4;
55    static final String SUBTEST_NAME = TestClass.class.getName() + ".class";
56    static final String TEST_METHOD_NAME = "test";
57
58    public static void main(String... args) throws Exception {
59        new TestCP().run();
60    }
61
62    public void run() throws Exception {
63        String workDir = System.getProperty("test.classes");
64        File compiledTest = new File(workDir, SUBTEST_NAME);
65        verifyMethodHandleInvocationDescriptors(compiledTest);
66    }
67
68    void verifyMethodHandleInvocationDescriptors(File f) {
69        System.err.println("verify: " + f);
70        try {
71            int count = 0;
72            ClassFile cf = ClassFile.read(f);
73            Method testMethod = null;
74            for (Method m : cf.methods) {
75                if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
76                    testMethod = m;
77                    break;
78                }
79            }
80            if (testMethod == null) {
81                throw new Error("Test method not found");
82            }
83            Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
84            if (testMethod == null) {
85                throw new Error("Code attribute for test() method not found");
86            }
87            int instr_count = 0;
88            int cp_entry = -1;
89
90            for (Instruction i : ea.getInstructions()) {
91                if (i.getMnemonic().equals("invokevirtual")) {
92                    instr_count++;
93                    if (cp_entry == -1) {
94                        cp_entry = i.getUnsignedShort(1);
95                    } else if (cp_entry != i.getUnsignedShort(1)) {
96                        throw new Error("Unexpected CP entry in polymorphic signature call");
97                    }
98                    CONSTANT_Methodref_info methRef =
99                            (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
100                    String type = methRef.getNameAndTypeInfo().getType();
101                    if (!type.equals(PS_TYPE)) {
102                        throw new Error("Unexpected type in polymorphic signature call: " + type);
103                    }
104                }
105            }
106            if (instr_count != PS_CALLS_COUNT) {
107                throw new Error("Wrong number of polymorphic signature call found: " + instr_count);
108            }
109        } catch (Exception e) {
110            e.printStackTrace();
111            throw new Error("error reading " + f +": " + e);
112        }
113    }
114}
115