langtools/test/tools/javac/meth/TestCP.java
author ohair
Wed, 13 Apr 2011 16:57:22 -0700
changeset 9091 7a2738b5e9c6
parent 9082 53409a3a93f8
parent 9087 e9e44877cd18
child 30730 d3ce7619db2c
permissions -rw-r--r--
Merge
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
/*
9087
e9e44877cd18 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8036
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6934
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
9082
53409a3a93f8 7028405: (javac) remove unused JSR-292 code
ksrini
parents: 8036
diff changeset
    38
import java.lang.invoke.*;
6934
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 {
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 6934
diff changeset
    45
            Number n = (Number)mh.invokeExact("daddy",1,'n');
6934
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    46
            n = (Number)mh.invokeExact("bunny",1,'d');
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 6934
diff changeset
    47
            n = (Number)(mh.invokeExact("foo",1,'d'));
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 6934
diff changeset
    48
            n = (Number)((mh.invokeExact("bar",1,'d')));
6934
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
    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    51
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    52
    static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 6934
diff changeset
    53
    static final int PS_CALLS_COUNT = 4;
6934
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    54
    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
    55
    static final String TEST_METHOD_NAME = "test";
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    56
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    57
    public static void main(String... args) throws Exception {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    58
        new TestCP().run();
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    59
    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    60
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    61
    public void run() throws Exception {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    62
        String workDir = System.getProperty("test.classes");
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    63
        File compiledTest = new File(workDir, SUBTEST_NAME);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    64
        verifyMethodHandleInvocationDescriptors(compiledTest);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    65
    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    66
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    67
    void verifyMethodHandleInvocationDescriptors(File f) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    68
        System.err.println("verify: " + f);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    69
        try {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    70
            int count = 0;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    71
            ClassFile cf = ClassFile.read(f);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    72
            Method testMethod = null;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    73
            for (Method m : cf.methods) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    74
                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
    75
                    testMethod = m;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    76
                    break;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    77
                }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    78
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    79
            if (testMethod == null) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    80
                throw new Error("Test method not found");
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    81
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    82
            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
    83
            if (testMethod == null) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    84
                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
    85
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    86
            int instr_count = 0;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    87
            int cp_entry = -1;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    88
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    89
            for (Instruction i : ea.getInstructions()) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    90
                if (i.getMnemonic().equals("invokevirtual")) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    91
                    instr_count++;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    92
                    if (cp_entry == -1) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    93
                        cp_entry = i.getUnsignedShort(1);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    94
                    } else if (cp_entry != i.getUnsignedShort(1)) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    95
                        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
    96
                    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    97
                    CONSTANT_Methodref_info methRef =
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    98
                            (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
    99
                    String type = methRef.getNameAndTypeInfo().getType();
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   100
                    if (!type.equals(PS_TYPE)) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   101
                        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
   102
                    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   103
                }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   104
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   105
            if (instr_count != PS_CALLS_COUNT) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   106
                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
   107
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   108
        } catch (Exception e) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   109
            e.printStackTrace();
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   110
            throw new Error("error reading " + f +": " + e);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   111
        }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   112
    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   113
}