ResolvePossiblyCachedConstantInPoolTest.java revision 11833:1cbffa2beba6
1248302Sbrooks/*
2244401Sbrooks * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3241236Sbrooks * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4241236Sbrooks *
5241236Sbrooks * This code is free software; you can redistribute it and/or modify it
6241236Sbrooks * under the terms of the GNU General Public License version 2 only, as
7241236Sbrooks * published by the Free Software Foundation.
8241236Sbrooks *
9241236Sbrooks * This code is distributed in the hope that it will be useful, but WITHOUT
10241236Sbrooks * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11241236Sbrooks * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12241236Sbrooks * version 2 for more details (a copy is included in the LICENSE file that
13241236Sbrooks * accompanied this code).
14241236Sbrooks *
15241236Sbrooks * You should have received a copy of the GNU General Public License version
16241236Sbrooks * 2 along with this work; if not, write to the Free Software Foundation,
17241236Sbrooks * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18241236Sbrooks *
19241236Sbrooks * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20241236Sbrooks * or visit www.oracle.com if you need additional information or have any
21241236Sbrooks * questions.
22241236Sbrooks *
23241236Sbrooks */
24241236Sbrooks
25241236Sbrooks/*
26241236Sbrooks * @test
27241236Sbrooks * @bug 8138708
28241236Sbrooks * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
29241236Sbrooks * @library /test/lib /
30241236Sbrooks * @library ../common/patches
31241236Sbrooks * @modules java.base/jdk.internal.misc
32241236Sbrooks *          java.base/jdk.internal.reflect
33241236Sbrooks *          java.base/jdk.internal.org.objectweb.asm
34241236Sbrooks *          java.base/jdk.internal.org.objectweb.asm.tree
35241236Sbrooks *          jdk.vm.ci/jdk.vm.ci.hotspot
36241236Sbrooks *          jdk.vm.ci/jdk.vm.ci.runtime
37241236Sbrooks *          jdk.vm.ci/jdk.vm.ci.meta
38241236Sbrooks *
39241236Sbrooks * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
40241236Sbrooks * @run driver ClassFileInstaller sun.hotspot.WhiteBox
41241236Sbrooks *                                sun.hotspot.WhiteBox$WhiteBoxPermission
42241236Sbrooks * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
43244230Sbrooks *                   -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
44244230Sbrooks *                   compiler.jvmci.compilerToVM.ResolvePossiblyCachedConstantInPoolTest
45241236Sbrooks */
46241236Sbrooks
47241236Sbrookspackage compiler.jvmci.compilerToVM;
48241236Sbrooks
49241236Sbrooksimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes;
50244230Sbrooksimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;
51244230Sbrooksimport compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator;
52244230Sbrooksimport compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;
53241236Sbrooksimport jdk.test.lib.Asserts;
54244230Sbrooksimport jdk.vm.ci.hotspot.CompilerToVMHelper;
55241236Sbrooksimport jdk.vm.ci.meta.ConstantPool;
56241236Sbrooks
57241236Sbrooksimport java.util.HashMap;
58241236Sbrooksimport java.util.Map;
59244230Sbrooks
60244230Sbrooksimport static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_STRING;
61244230Sbrooks
62244401Sbrooks/**
63244401Sbrooks * Test for {@code jdk.vm.ci.hotspot.CompilerToVM.resolvePossiblyCachedConstantInPool} method
64244401Sbrooks */
65244401Sbrookspublic class ResolvePossiblyCachedConstantInPoolTest {
66244401Sbrooks
67241236Sbrooks    public static void main(String[] args) throws Exception {
68241236Sbrooks        Map<ConstantTypes, Validator> typeTests = new HashMap<>();
69241236Sbrooks        typeTests.put(CONSTANT_STRING, ResolvePossiblyCachedConstantInPoolTest::validateString);
70241236Sbrooks        ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
71241236Sbrooks        // The next "Class.forName" is here for the following reason.
72241236Sbrooks        // When class is initialized, constant pool cache is available.
73241236Sbrooks        // This method works only with cached constant pool.
74241236Sbrooks        for (DummyClasses dummy : DummyClasses.values()) {
75241236Sbrooks            Class.forName(dummy.klass.getName());
76241236Sbrooks        }
77241236Sbrooks        testCase.test();
78241236Sbrooks    }
79241236Sbrooks
80241236Sbrooks    private static void validateString(ConstantPool constantPoolCTVM,
81241236Sbrooks                                       ConstantTypes cpType,
82241236Sbrooks                                       DummyClasses dummyClass,
83241236Sbrooks                                       int cpi) {
84241236Sbrooks        TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, cpi);
85241236Sbrooks        if (entry == null) {
86241236Sbrooks            return;
87241236Sbrooks        }
88241236Sbrooks        int index = cpi;
89241236Sbrooks        String cached = "";
90241236Sbrooks        int cpci = dummyClass.getCPCacheIndex(cpi);
91241236Sbrooks        if (cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {
92241236Sbrooks            index = cpci;
93241236Sbrooks            cached = "cached ";
94241236Sbrooks        }
95241236Sbrooks        Object constantInPool = CompilerToVMHelper.resolvePossiblyCachedConstantInPool(constantPoolCTVM, index);
96241236Sbrooks        String stringToVerify = (String) constantInPool;
97241236Sbrooks        String stringToRefer = entry.name;
98241236Sbrooks        if (stringToRefer.equals("") && cpci != ConstantPoolTestsHelper.NO_CP_CACHE_PRESENT) {
99248302Sbrooks            stringToRefer = null; // tested method returns null for cached empty strings
100241236Sbrooks        }
101241236Sbrooks        String msg = String.format("Wrong string accessed by %sconstant pool index %d", cached, index);
102241236Sbrooks        Asserts.assertEQ(stringToRefer, stringToVerify, msg);
103248302Sbrooks    }
104248302Sbrooks}
105241236Sbrooks