langtools/test/tools/javac/meth/TestCP.java
author hannesw
Tue, 22 Mar 2016 14:23:16 +0100
changeset 36690 06b714373aa4
parent 30846 2b3f379840f0
child 38537 ba5503adca64
permissions -rw-r--r--
8151810: for-in iteration does not provide per-iteration scope Reviewed-by: attila, lagergren
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 9091
diff changeset
     2
 * Copyright (c) 2010, 2015, 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
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    28
 * @modules jdk.jdeps/com.sun.tools.classfile
6934
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    29
 * @run main TestCP
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
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.classfile.Instruction;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.classfile.Attribute;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.classfile.ClassFile;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.classfile.Code_attribute;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.classfile.ConstantPool.*;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.classfile.Method;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    38
9082
53409a3a93f8 7028405: (javac) remove unused JSR-292 code
ksrini
parents: 8036
diff changeset
    39
import java.lang.invoke.*;
6934
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    40
import java.io.*;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    41
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    42
public class TestCP {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    43
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    44
    static class TestClass {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    45
        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
    46
            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
    47
            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
    48
            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
    49
            n = (Number)((mh.invokeExact("bar",1,'d')));
6934
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
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    53
    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
    54
    static final int PS_CALLS_COUNT = 4;
6934
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    55
    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
    56
    static final String TEST_METHOD_NAME = "test";
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
    public static void main(String... args) throws Exception {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    59
        new TestCP().run();
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
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    62
    public void run() throws Exception {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    63
        String workDir = System.getProperty("test.classes");
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    64
        File compiledTest = new File(workDir, SUBTEST_NAME);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    65
        verifyMethodHandleInvocationDescriptors(compiledTest);
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
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    68
    void verifyMethodHandleInvocationDescriptors(File f) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    69
        System.err.println("verify: " + f);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    70
        try {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    71
            int count = 0;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    72
            ClassFile cf = ClassFile.read(f);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    73
            Method testMethod = null;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    74
            for (Method m : cf.methods) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    75
                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
    76
                    testMethod = m;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    77
                    break;
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
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    80
            if (testMethod == null) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    81
                throw new Error("Test method not found");
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    82
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    83
            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
    84
            if (testMethod == null) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    85
                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
    86
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    87
            int instr_count = 0;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    88
            int cp_entry = -1;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    89
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    90
            for (Instruction i : ea.getInstructions()) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    91
                if (i.getMnemonic().equals("invokevirtual")) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    92
                    instr_count++;
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    93
                    if (cp_entry == -1) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    94
                        cp_entry = i.getUnsignedShort(1);
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    95
                    } else if (cp_entry != i.getUnsignedShort(1)) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    96
                        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
    97
                    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    98
                    CONSTANT_Methodref_info methRef =
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
    99
                            (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
   100
                    String type = methRef.getNameAndTypeInfo().getType();
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   101
                    if (!type.equals(PS_TYPE)) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   102
                        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
   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
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   106
            if (instr_count != PS_CALLS_COUNT) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   107
                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
   108
            }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   109
        } catch (Exception e) {
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   110
            e.printStackTrace();
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   111
            throw new Error("error reading " + f +": " + e);
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
    }
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
diff changeset
   114
}