1 /*
   2  * Copyright (c) 2010, 2011, 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 = (Number)mh.invokeExact("daddy",1,'n');
  46             n = (Number)mh.invokeExact("bunny",1,'d');
  47             n = (Number)(mh.invokeExact("foo",1,'d'));
  48             n = (Number)((mh.invokeExact("bar",1,'d')));
  49         }
  50     }
  51 
  52     static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
  53     static final int PS_CALLS_COUNT = 4;
  54     static final String SUBTEST_NAME = TestClass.class.getName() + ".class";
  55     static final String TEST_METHOD_NAME = "test";
  56 
  57     public static void main(String... args) throws Exception {
  58         new TestCP().run();
  59     }
  60 
  61     public void run() throws Exception {
  62         String workDir = System.getProperty("test.classes");
  63         File compiledTest = new File(workDir, SUBTEST_NAME);
  64         verifyMethodHandleInvocationDescriptors(compiledTest);
  65     }
  66 
  67     void verifyMethodHandleInvocationDescriptors(File f) {
  68         System.err.println("verify: " + f);
  69         try {
  70             int count = 0;
  71             ClassFile cf = ClassFile.read(f);
  72             Method testMethod = null;
  73             for (Method m : cf.methods) {
  74                 if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
  75                     testMethod = m;
  76                     break;
  77                 }
  78             }
  79             if (testMethod == null) {
  80                 throw new Error("Test method not found");
  81             }
  82             Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
  83             if (testMethod == null) {
  84                 throw new Error("Code attribute for test() method not found");
  85             }
  86             int instr_count = 0;
  87             int cp_entry = -1;
  88 
  89             for (Instruction i : ea.getInstructions()) {
  90                 if (i.getMnemonic().equals("invokevirtual")) {
  91                     instr_count++;
  92                     if (cp_entry == -1) {
  93                         cp_entry = i.getUnsignedShort(1);
  94                     } else if (cp_entry != i.getUnsignedShort(1)) {
  95                         throw new Error("Unexpected CP entry in polymorphic signature call");
  96                     }
  97                     CONSTANT_Methodref_info methRef =
  98                             (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
  99                     String type = methRef.getNameAndTypeInfo().getType();
 100                     if (!type.equals(PS_TYPE)) {
 101                         throw new Error("Unexpected type in polymorphic signature call: " + type);
 102                     }
 103                 }
 104             }
 105             if (instr_count != PS_CALLS_COUNT) {
 106                 throw new Error("Wrong number of polymorphic signature call found: " + instr_count);
 107             }
 108         } catch (Exception e) {
 109             e.printStackTrace();
 110             throw new Error("error reading " + f +": " + e);
 111         }
 112     }
 113 }