langtools/test/tools/javac/meth/TestCP.java
changeset 6934 258e5f06880f
child 8036 17b976649c61
equal deleted inserted replaced
6933:6b9085d37f01 6934:258e5f06880f
       
     1 /*
       
     2  * Copyright (c) 2010, 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  * @run main TestCP
       
    29  */
       
    30 
       
    31 import com.sun.tools.classfile.Instruction;
       
    32 import com.sun.tools.classfile.Attribute;
       
    33 import com.sun.tools.classfile.ClassFile;
       
    34 import com.sun.tools.classfile.Code_attribute;
       
    35 import com.sun.tools.classfile.ConstantPool.*;
       
    36 import com.sun.tools.classfile.Method;
       
    37 
       
    38 import java.dyn.*;
       
    39 import java.io.*;
       
    40 
       
    41 public class TestCP {
       
    42 
       
    43     static class TestClass {
       
    44         void test(MethodHandle mh) throws Throwable {
       
    45             Number n = mh.<Number>invokeExact("daddy",1,'n');
       
    46             n = (Number)mh.invokeExact("bunny",1,'d');
       
    47         }
       
    48     }
       
    49 
       
    50     static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
       
    51     static final int PS_CALLS_COUNT = 2;
       
    52     static final String SUBTEST_NAME = TestClass.class.getName() + ".class";
       
    53     static final String TEST_METHOD_NAME = "test";
       
    54 
       
    55     public static void main(String... args) throws Exception {
       
    56         new TestCP().run();
       
    57     }
       
    58 
       
    59     public void run() throws Exception {
       
    60         String workDir = System.getProperty("test.classes");
       
    61         File compiledTest = new File(workDir, SUBTEST_NAME);
       
    62         verifyMethodHandleInvocationDescriptors(compiledTest);
       
    63     }
       
    64 
       
    65     void verifyMethodHandleInvocationDescriptors(File f) {
       
    66         System.err.println("verify: " + f);
       
    67         try {
       
    68             int count = 0;
       
    69             ClassFile cf = ClassFile.read(f);
       
    70             Method testMethod = null;
       
    71             for (Method m : cf.methods) {
       
    72                 if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
       
    73                     testMethod = m;
       
    74                     break;
       
    75                 }
       
    76             }
       
    77             if (testMethod == null) {
       
    78                 throw new Error("Test method not found");
       
    79             }
       
    80             Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
       
    81             if (testMethod == null) {
       
    82                 throw new Error("Code attribute for test() method not found");
       
    83             }
       
    84             int instr_count = 0;
       
    85             int cp_entry = -1;
       
    86 
       
    87             for (Instruction i : ea.getInstructions()) {
       
    88                 if (i.getMnemonic().equals("invokevirtual")) {
       
    89                     instr_count++;
       
    90                     if (cp_entry == -1) {
       
    91                         cp_entry = i.getUnsignedShort(1);
       
    92                     } else if (cp_entry != i.getUnsignedShort(1)) {
       
    93                         throw new Error("Unexpected CP entry in polymorphic signature call");
       
    94                     }
       
    95                     CONSTANT_Methodref_info methRef =
       
    96                             (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
       
    97                     String type = methRef.getNameAndTypeInfo().getType();
       
    98                     if (!type.equals(PS_TYPE)) {
       
    99                         throw new Error("Unexpected type in polymorphic signature call: " + type);
       
   100                     }
       
   101                 }
       
   102             }
       
   103             if (instr_count != PS_CALLS_COUNT) {
       
   104                 throw new Error("Wrong number of polymorphic signature call found: " + instr_count);
       
   105             }
       
   106         } catch (Exception e) {
       
   107             e.printStackTrace();
       
   108             throw new Error("error reading " + f +": " + e);
       
   109         }
       
   110     }
       
   111 }