Mon, 21 Jan 2013 11:16:28 -0800
Merge
mcimadamore@716 | 1 | /* |
ohair@962 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@716 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@716 | 4 | * |
mcimadamore@716 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@716 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@716 | 7 | * published by the Free Software Foundation. |
mcimadamore@716 | 8 | * |
mcimadamore@716 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@716 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@716 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@716 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@716 | 13 | * accompanied this code). |
mcimadamore@716 | 14 | * |
mcimadamore@716 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@716 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@716 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@716 | 18 | * |
mcimadamore@716 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@716 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@716 | 21 | * questions. |
mcimadamore@716 | 22 | */ |
mcimadamore@716 | 23 | |
mcimadamore@716 | 24 | /* |
mcimadamore@716 | 25 | * @test |
mcimadamore@716 | 26 | * @bug 6991980 |
mcimadamore@716 | 27 | * @summary polymorphic signature calls don't share the same CP entries |
mcimadamore@716 | 28 | * @run main TestCP |
mcimadamore@716 | 29 | */ |
mcimadamore@716 | 30 | |
mcimadamore@716 | 31 | import com.sun.tools.classfile.Instruction; |
mcimadamore@716 | 32 | import com.sun.tools.classfile.Attribute; |
mcimadamore@716 | 33 | import com.sun.tools.classfile.ClassFile; |
mcimadamore@716 | 34 | import com.sun.tools.classfile.Code_attribute; |
mcimadamore@716 | 35 | import com.sun.tools.classfile.ConstantPool.*; |
mcimadamore@716 | 36 | import com.sun.tools.classfile.Method; |
mcimadamore@716 | 37 | |
ksrini@957 | 38 | import java.lang.invoke.*; |
mcimadamore@716 | 39 | import java.io.*; |
mcimadamore@716 | 40 | |
mcimadamore@716 | 41 | public class TestCP { |
mcimadamore@716 | 42 | |
mcimadamore@716 | 43 | static class TestClass { |
mcimadamore@716 | 44 | void test(MethodHandle mh) throws Throwable { |
mcimadamore@820 | 45 | Number n = (Number)mh.invokeExact("daddy",1,'n'); |
mcimadamore@716 | 46 | n = (Number)mh.invokeExact("bunny",1,'d'); |
mcimadamore@820 | 47 | n = (Number)(mh.invokeExact("foo",1,'d')); |
mcimadamore@820 | 48 | n = (Number)((mh.invokeExact("bar",1,'d'))); |
mcimadamore@716 | 49 | } |
mcimadamore@716 | 50 | } |
mcimadamore@716 | 51 | |
mcimadamore@716 | 52 | static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;"; |
mcimadamore@820 | 53 | static final int PS_CALLS_COUNT = 4; |
mcimadamore@716 | 54 | static final String SUBTEST_NAME = TestClass.class.getName() + ".class"; |
mcimadamore@716 | 55 | static final String TEST_METHOD_NAME = "test"; |
mcimadamore@716 | 56 | |
mcimadamore@716 | 57 | public static void main(String... args) throws Exception { |
mcimadamore@716 | 58 | new TestCP().run(); |
mcimadamore@716 | 59 | } |
mcimadamore@716 | 60 | |
mcimadamore@716 | 61 | public void run() throws Exception { |
mcimadamore@716 | 62 | String workDir = System.getProperty("test.classes"); |
mcimadamore@716 | 63 | File compiledTest = new File(workDir, SUBTEST_NAME); |
mcimadamore@716 | 64 | verifyMethodHandleInvocationDescriptors(compiledTest); |
mcimadamore@716 | 65 | } |
mcimadamore@716 | 66 | |
mcimadamore@716 | 67 | void verifyMethodHandleInvocationDescriptors(File f) { |
mcimadamore@716 | 68 | System.err.println("verify: " + f); |
mcimadamore@716 | 69 | try { |
mcimadamore@716 | 70 | int count = 0; |
mcimadamore@716 | 71 | ClassFile cf = ClassFile.read(f); |
mcimadamore@716 | 72 | Method testMethod = null; |
mcimadamore@716 | 73 | for (Method m : cf.methods) { |
mcimadamore@716 | 74 | if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) { |
mcimadamore@716 | 75 | testMethod = m; |
mcimadamore@716 | 76 | break; |
mcimadamore@716 | 77 | } |
mcimadamore@716 | 78 | } |
mcimadamore@716 | 79 | if (testMethod == null) { |
mcimadamore@716 | 80 | throw new Error("Test method not found"); |
mcimadamore@716 | 81 | } |
mcimadamore@716 | 82 | Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); |
mcimadamore@716 | 83 | if (testMethod == null) { |
mcimadamore@716 | 84 | throw new Error("Code attribute for test() method not found"); |
mcimadamore@716 | 85 | } |
mcimadamore@716 | 86 | int instr_count = 0; |
mcimadamore@716 | 87 | int cp_entry = -1; |
mcimadamore@716 | 88 | |
mcimadamore@716 | 89 | for (Instruction i : ea.getInstructions()) { |
mcimadamore@716 | 90 | if (i.getMnemonic().equals("invokevirtual")) { |
mcimadamore@716 | 91 | instr_count++; |
mcimadamore@716 | 92 | if (cp_entry == -1) { |
mcimadamore@716 | 93 | cp_entry = i.getUnsignedShort(1); |
mcimadamore@716 | 94 | } else if (cp_entry != i.getUnsignedShort(1)) { |
mcimadamore@716 | 95 | throw new Error("Unexpected CP entry in polymorphic signature call"); |
mcimadamore@716 | 96 | } |
mcimadamore@716 | 97 | CONSTANT_Methodref_info methRef = |
mcimadamore@716 | 98 | (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry); |
mcimadamore@716 | 99 | String type = methRef.getNameAndTypeInfo().getType(); |
mcimadamore@716 | 100 | if (!type.equals(PS_TYPE)) { |
mcimadamore@716 | 101 | throw new Error("Unexpected type in polymorphic signature call: " + type); |
mcimadamore@716 | 102 | } |
mcimadamore@716 | 103 | } |
mcimadamore@716 | 104 | } |
mcimadamore@716 | 105 | if (instr_count != PS_CALLS_COUNT) { |
mcimadamore@716 | 106 | throw new Error("Wrong number of polymorphic signature call found: " + instr_count); |
mcimadamore@716 | 107 | } |
mcimadamore@716 | 108 | } catch (Exception e) { |
mcimadamore@716 | 109 | e.printStackTrace(); |
mcimadamore@716 | 110 | throw new Error("error reading " + f +": " + e); |
mcimadamore@716 | 111 | } |
mcimadamore@716 | 112 | } |
mcimadamore@716 | 113 | } |