LookupKlassRefIndexInPoolTest.java revision 10815:88dd08d7be0f
1219820Sjeff/*
2219820Sjeff * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3219820Sjeff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219820Sjeff *
5219820Sjeff * This code is free software; you can redistribute it and/or modify it
6219820Sjeff * under the terms of the GNU General Public License version 2 only, as
7219820Sjeff * published by the Free Software Foundation.
8219820Sjeff *
9219820Sjeff * This code is distributed in the hope that it will be useful, but WITHOUT
10219820Sjeff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219820Sjeff * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219820Sjeff * version 2 for more details (a copy is included in the LICENSE file that
13219820Sjeff * accompanied this code).
14219820Sjeff *
15219820Sjeff * You should have received a copy of the GNU General Public License version
16219820Sjeff * 2 along with this work; if not, write to the Free Software Foundation,
17219820Sjeff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219820Sjeff *
19219820Sjeff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20219820Sjeff * or visit www.oracle.com if you need additional information or have any
21219820Sjeff * questions.
22219820Sjeff *
23219820Sjeff */
24219820Sjeff
25219820Sjeff/*
26219820Sjeff * @test
27219820Sjeff * @bug 8138708
28219820Sjeff * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
29219820Sjeff * @library /testlibrary /test/lib /
30219820Sjeff * @library ../common/patches
31219820Sjeff * @modules java.base/jdk.internal.misc
32219820Sjeff *          java.base/jdk.internal.reflect
33219820Sjeff *          java.base/jdk.internal.org.objectweb.asm
34219820Sjeff *          java.base/jdk.internal.org.objectweb.asm.tree
35219820Sjeff *          jdk.vm.ci/jdk.vm.ci.hotspot
36219820Sjeff *          jdk.vm.ci/jdk.vm.ci.meta
37219820Sjeff * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
38219820Sjeff * @build sun.hotspot.WhiteBox
39219820Sjeff *        compiler.jvmci.compilerToVM.LookupKlassRefIndexInPoolTest
40219820Sjeff * @run main ClassFileInstaller sun.hotspot.WhiteBox
41219820Sjeff *                              sun.hotspot.WhiteBox$WhiteBoxPermission
42219820Sjeff * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
43219820Sjeff *                   -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
44219820Sjeff *                   compiler.jvmci.compilerToVM.LookupKlassRefIndexInPoolTest
45219820Sjeff */
46219820Sjeff
47219820Sjeffpackage compiler.jvmci.compilerToVM;
48219820Sjeff
49219820Sjeffimport compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;
50219820Sjeffimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes;
51219820Sjeffimport static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.*;
52219820Sjeffimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;
53219820Sjeffimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator;
54219820Sjeffimport java.util.HashMap;
55219820Sjeffimport java.util.Map;
56219820Sjeffimport jdk.test.lib.Asserts;
57219820Sjeffimport jdk.vm.ci.hotspot.CompilerToVMHelper;
58219820Sjeffimport jdk.vm.ci.meta.ConstantPool;
59219820Sjeff
60219820Sjeff/**
61219820Sjeff * Test for {@code jdk.vm.ci.hotspot.CompilerToVM.lookupKlassRefIndexInPool} method
62219820Sjeff */
63219820Sjeffpublic class LookupKlassRefIndexInPoolTest {
64219820Sjeff
65219820Sjeff    public static void main(String[] args) throws Exception {
66219820Sjeff        Map<ConstantTypes, Validator> typeTests = new HashMap<>();
67219820Sjeff        typeTests.put(CONSTANT_METHODREF, LookupKlassRefIndexInPoolTest::validate);
68219820Sjeff        typeTests.put(CONSTANT_INTERFACEMETHODREF, LookupKlassRefIndexInPoolTest::validate);
69219820Sjeff        typeTests.put(CONSTANT_FIELDREF, LookupKlassRefIndexInPoolTest::validate);
70219820Sjeff        ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
71219820Sjeff        testCase.test();
72219820Sjeff        // The next "Class.forName" and repeating "testCase.test()"
73219820Sjeff        // are here for the following reason.
74219820Sjeff        // The first test run is without dummy class initialization,
75219820Sjeff        // which means no constant pool cache exists.
76219820Sjeff        // The second run is with initialized class (with constant pool cache available).
77219820Sjeff        // Some CompilerToVM methods require different input
78219820Sjeff        // depending on whether CP cache exists or not.
79219820Sjeff        for (DummyClasses dummy : DummyClasses.values()) {
80219820Sjeff            Class.forName(dummy.klass.getName());
81219820Sjeff        }
82219820Sjeff        testCase.test();
83219820Sjeff    }
84219820Sjeff
85219820Sjeff    private static void validate(ConstantPool constantPoolCTVM,
86219820Sjeff                                 ConstantTypes cpType,
87219820Sjeff                                 DummyClasses dummyClass,
88219820Sjeff                                 int cpi) {
89219820Sjeff        TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, cpi);
90219820Sjeff        if (entry == null) {
91219820Sjeff            return;
92219820Sjeff        }
93219820Sjeff        int index = cpi;
94219820Sjeff        String cached = "";
95219820Sjeff        int cpci = dummyClass.getCPCacheIndex(cpi);
96219820Sjeff        if (cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {
97219820Sjeff            index = cpci;
98219820Sjeff            cached = "cached ";
99219820Sjeff        }
100219820Sjeff        int indexToVerify = CompilerToVMHelper.lookupKlassRefIndexInPool(constantPoolCTVM, index);
101219820Sjeff        int indexToRefer = dummyClass.constantPoolSS.getClassRefIndexAt(cpi);
102219820Sjeff        String msg = String.format("Wrong class index returned by lookupKlassRefIndexInPool method "
103219820Sjeff                                           + "applied to %sconstant pool index %d",
104219820Sjeff                                   cached,
105219820Sjeff                                   index);
106219820Sjeff        Asserts.assertEQ(indexToRefer, indexToVerify, msg);
107219820Sjeff    }
108219820Sjeff}
109219820Sjeff