LookupMethodInPoolTest.java revision 12158:0fe2815ffa74
1170754Sdelphij/*
2170754Sdelphij * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3170754Sdelphij * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4170754Sdelphij *
5170754Sdelphij * This code is free software; you can redistribute it and/or modify it
6170754Sdelphij * under the terms of the GNU General Public License version 2 only, as
7170754Sdelphij * published by the Free Software Foundation.
8170754Sdelphij *
9170754Sdelphij * This code is distributed in the hope that it will be useful, but WITHOUT
10170754Sdelphij * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11170754Sdelphij * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12170754Sdelphij * version 2 for more details (a copy is included in the LICENSE file that
13170754Sdelphij * accompanied this code).
14170754Sdelphij *
15170754Sdelphij * You should have received a copy of the GNU General Public License version
16170754Sdelphij * 2 along with this work; if not, write to the Free Software Foundation,
17170754Sdelphij * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18170754Sdelphij *
19170754Sdelphij * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20170754Sdelphij * or visit www.oracle.com if you need additional information or have any
21170754Sdelphij * questions.
22170754Sdelphij */
23170754Sdelphij
24170754Sdelphij/*
25170754Sdelphij * @test
26170754Sdelphij * @bug 8138708
27170754Sdelphij * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
28170754Sdelphij * @library /test/lib /
29170754Sdelphij * @library ../common/patches
30170754Sdelphij * @modules java.base/jdk.internal.misc
31170754Sdelphij *          java.base/jdk.internal.reflect
32170754Sdelphij *          java.base/jdk.internal.org.objectweb.asm
33170754Sdelphij *          java.base/jdk.internal.org.objectweb.asm.tree
34170754Sdelphij *          jdk.vm.ci/jdk.vm.ci.hotspot
35170754Sdelphij *          jdk.vm.ci/jdk.vm.ci.meta
36170754Sdelphij *          jdk.vm.ci/jdk.vm.ci.runtime
37170754Sdelphij *
38170754Sdelphij * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
39170754Sdelphij * @run driver ClassFileInstaller sun.hotspot.WhiteBox
40170754Sdelphij *                                sun.hotspot.WhiteBox$WhiteBoxPermission
41170754Sdelphij * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
42170754Sdelphij *                   -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
43170754Sdelphij *                   compiler.jvmci.compilerToVM.LookupMethodInPoolTest
44170754Sdelphij */
45170754Sdelphij
46170754Sdelphijpackage compiler.jvmci.compilerToVM;
47170754Sdelphij
48170754Sdelphijimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes;
49170754Sdelphijimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;
50170754Sdelphijimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator;
51170754Sdelphijimport compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;
52170754Sdelphijimport jdk.test.lib.Asserts;
53170754Sdelphijimport jdk.vm.ci.hotspot.CompilerToVMHelper;
54170754Sdelphijimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
55170754Sdelphijimport jdk.vm.ci.meta.ConstantPool;
56170754Sdelphij
57170754Sdelphijimport java.util.HashMap;
58170754Sdelphijimport java.util.Map;
59170754Sdelphij
60170754Sdelphijimport static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_INTERFACEMETHODREF;
61170754Sdelphijimport static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODREF;
62170754Sdelphij
63170754Sdelphij/**
64170754Sdelphij * Test for {@code jdk.vm.ci.hotspot.CompilerToVM.lookupMethodInPool} method
65170754Sdelphij */
66170754Sdelphijpublic class LookupMethodInPoolTest {
67170754Sdelphij
68170754Sdelphij    public static void main(String[] args) throws Exception {
69170754Sdelphij        Map<ConstantTypes, Validator> typeTests = new HashMap<>();
70170754Sdelphij        typeTests.put(CONSTANT_METHODREF, LookupMethodInPoolTest::validate);
71170754Sdelphij        typeTests.put(CONSTANT_INTERFACEMETHODREF, LookupMethodInPoolTest::validate);
72170754Sdelphij        ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
73170754Sdelphij        testCase.test();
74170754Sdelphij        // The next "Class.forName" and repeating "testCase.test()"
75170754Sdelphij        // are here for the following reason.
76170754Sdelphij        // The first test run is without dummy class initialization,
77170754Sdelphij        // which means no constant pool cache exists.
78170754Sdelphij        // The second run is with initialized class (with constant pool cache available).
79170754Sdelphij        // Some CompilerToVM methods require different input
80170754Sdelphij        // depending on whether CP cache exists or not.
81170754Sdelphij        for (DummyClasses dummy : DummyClasses.values()) {
82170754Sdelphij            Class.forName(dummy.klass.getName());
83170754Sdelphij        }
84170754Sdelphij        testCase.test();
85170754Sdelphij    }
86170754Sdelphij
87170754Sdelphij    private static void validate(ConstantPool constantPoolCTVM,
88170754Sdelphij                                 ConstantTypes cpType,
89170754Sdelphij                                 DummyClasses dummyClass,
90170754Sdelphij                                 int cpi) {
91170754Sdelphij        TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, cpi);
92170754Sdelphij        if (entry == null) {
93170754Sdelphij            return;
94170754Sdelphij        }
95170754Sdelphij        int index = cpi;
96170754Sdelphij        String cached = "";
97170754Sdelphij        int cpci = dummyClass.getCPCacheIndex(cpi);
98170754Sdelphij        if (cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {
99170754Sdelphij            index = cpci;
100170754Sdelphij            cached = "cached ";
101170754Sdelphij        }
102170754Sdelphij        for (int j = 0; j < entry.opcodes.length; j++) {
103170754Sdelphij            HotSpotResolvedJavaMethod methodToVerify = CompilerToVMHelper
104170754Sdelphij                    .lookupMethodInPool(constantPoolCTVM, index, entry.opcodes[j]);
105170754Sdelphij            String msg = String.format("Object returned by lookupMethodInPool method"
106170754Sdelphij                                               + " for %sindex %d should not be null",
107170754Sdelphij                                       cached,
108170754Sdelphij                                       index);
109170754Sdelphij            Asserts.assertNotNull(methodToVerify, msg);
110170754Sdelphij            String[] classNameSplit = entry.klass.split("/");
111170754Sdelphij            String classNameToRefer = classNameSplit[classNameSplit.length - 1];
112170754Sdelphij            String methodNameToRefer = entry.name;
113170754Sdelphij            String methodToVerifyToString = methodToVerify.toString();
114170754Sdelphij            if (!methodToVerifyToString.contains(classNameToRefer)
115170754Sdelphij                    || !methodToVerifyToString.contains(methodNameToRefer)) {
116170754Sdelphij                msg = String.format("String representation \"%s\" of the object"
117170754Sdelphij                                            + " returned by lookupMethodInPool method"
118170754Sdelphij                                            + " for index %d does not contain a method's class name"
119170754Sdelphij                                            + " or method's name, should contain %s.%s",
120170754Sdelphij                                    methodToVerifyToString,
121170754Sdelphij                                    index,
122170754Sdelphij                                    classNameToRefer,
123170754Sdelphij                                    methodNameToRefer);
124170754Sdelphij                throw new AssertionError(msg);
125170754Sdelphij            }
126170754Sdelphij        }
127170754Sdelphij    }
128170754Sdelphij}
129170754Sdelphij