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