test/tools/javac/meth/TestCP.java

changeset 716
493ecc8111ba
child 820
2d5aff89aaa3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/meth/TestCP.java	Mon Oct 18 19:14:36 2010 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6991980
    1.30 + * @summary  polymorphic signature calls don't share the same CP entries
    1.31 + * @run main TestCP
    1.32 + */
    1.33 +
    1.34 +import com.sun.tools.classfile.Instruction;
    1.35 +import com.sun.tools.classfile.Attribute;
    1.36 +import com.sun.tools.classfile.ClassFile;
    1.37 +import com.sun.tools.classfile.Code_attribute;
    1.38 +import com.sun.tools.classfile.ConstantPool.*;
    1.39 +import com.sun.tools.classfile.Method;
    1.40 +
    1.41 +import java.dyn.*;
    1.42 +import java.io.*;
    1.43 +
    1.44 +public class TestCP {
    1.45 +
    1.46 +    static class TestClass {
    1.47 +        void test(MethodHandle mh) throws Throwable {
    1.48 +            Number n = mh.<Number>invokeExact("daddy",1,'n');
    1.49 +            n = (Number)mh.invokeExact("bunny",1,'d');
    1.50 +        }
    1.51 +    }
    1.52 +
    1.53 +    static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
    1.54 +    static final int PS_CALLS_COUNT = 2;
    1.55 +    static final String SUBTEST_NAME = TestClass.class.getName() + ".class";
    1.56 +    static final String TEST_METHOD_NAME = "test";
    1.57 +
    1.58 +    public static void main(String... args) throws Exception {
    1.59 +        new TestCP().run();
    1.60 +    }
    1.61 +
    1.62 +    public void run() throws Exception {
    1.63 +        String workDir = System.getProperty("test.classes");
    1.64 +        File compiledTest = new File(workDir, SUBTEST_NAME);
    1.65 +        verifyMethodHandleInvocationDescriptors(compiledTest);
    1.66 +    }
    1.67 +
    1.68 +    void verifyMethodHandleInvocationDescriptors(File f) {
    1.69 +        System.err.println("verify: " + f);
    1.70 +        try {
    1.71 +            int count = 0;
    1.72 +            ClassFile cf = ClassFile.read(f);
    1.73 +            Method testMethod = null;
    1.74 +            for (Method m : cf.methods) {
    1.75 +                if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
    1.76 +                    testMethod = m;
    1.77 +                    break;
    1.78 +                }
    1.79 +            }
    1.80 +            if (testMethod == null) {
    1.81 +                throw new Error("Test method not found");
    1.82 +            }
    1.83 +            Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
    1.84 +            if (testMethod == null) {
    1.85 +                throw new Error("Code attribute for test() method not found");
    1.86 +            }
    1.87 +            int instr_count = 0;
    1.88 +            int cp_entry = -1;
    1.89 +
    1.90 +            for (Instruction i : ea.getInstructions()) {
    1.91 +                if (i.getMnemonic().equals("invokevirtual")) {
    1.92 +                    instr_count++;
    1.93 +                    if (cp_entry == -1) {
    1.94 +                        cp_entry = i.getUnsignedShort(1);
    1.95 +                    } else if (cp_entry != i.getUnsignedShort(1)) {
    1.96 +                        throw new Error("Unexpected CP entry in polymorphic signature call");
    1.97 +                    }
    1.98 +                    CONSTANT_Methodref_info methRef =
    1.99 +                            (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
   1.100 +                    String type = methRef.getNameAndTypeInfo().getType();
   1.101 +                    if (!type.equals(PS_TYPE)) {
   1.102 +                        throw new Error("Unexpected type in polymorphic signature call: " + type);
   1.103 +                    }
   1.104 +                }
   1.105 +            }
   1.106 +            if (instr_count != PS_CALLS_COUNT) {
   1.107 +                throw new Error("Wrong number of polymorphic signature call found: " + instr_count);
   1.108 +            }
   1.109 +        } catch (Exception e) {
   1.110 +            e.printStackTrace();
   1.111 +            throw new Error("error reading " + f +": " + e);
   1.112 +        }
   1.113 +    }
   1.114 +}

mercurial