1 /*
   2  * Copyright (c) 2015, 2018, 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 8136421
  27  * @requires vm.jvmci
  28  * @summary Testing compiler.jvmci.CompilerToVM.lookupKlassInPool method
  29  * @library /test/lib /
  30  * @library ../common/patches
  31  * @modules java.base/jdk.internal.access
  32  *          java.base/jdk.internal.reflect
  33  *          java.base/jdk.internal.org.objectweb.asm
  34  *          java.base/jdk.internal.org.objectweb.asm.tree
  35  *          jdk.internal.vm.ci/jdk.vm.ci.hotspot
  36  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
  37  *          jdk.internal.vm.ci/jdk.vm.ci.meta
  38  *
  39  * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
  40  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  41  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  42  * @run main/othervm -Xbootclasspath/a:.
  43  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  44  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  45  *                   -Djvmci.Compiler=null
  46  *                   compiler.jvmci.compilerToVM.LookupKlassInPoolTest
  47  */
  48 
  49 package compiler.jvmci.compilerToVM;
  50 
  51 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes;
  52 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;
  53 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.Validator;
  54 import compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;
  55 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  56 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  57 import jdk.vm.ci.meta.ConstantPool;
  58 
  59 import java.util.HashMap;
  60 import java.util.Map;
  61 
  62 import static compiler.jvmci.compilerToVM.ConstantPoolTestCase.ConstantTypes.CONSTANT_CLASS;
  63 
  64 /**
  65  * Test for {@code jdk.vm.ci.hotspot.CompilerToVM.lookupKlassInPool} method
  66  */
  67 public class LookupKlassInPoolTest {
  68 
  69     public static void main(String[] args) throws Exception  {
  70         Map<ConstantTypes, Validator> typeTests = new HashMap<>();
  71         typeTests.put(CONSTANT_CLASS, LookupKlassInPoolTest::validate);
  72         ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
  73         testCase.test();
  74         // The next "Class.forName" and repeating "testCase.test()"
  75         // are here for the following reason.
  76         // The first test run is without dummy class initialization,
  77         // which means no constant pool cache exists.
  78         // The second run is with initialized class (with constant pool cache available).
  79         // Some CompilerToVM methods require different input
  80         // depending on whether CP cache exists or not.
  81         for (DummyClasses dummy : DummyClasses.values()) {
  82             Class.forName(dummy.klass.getName());
  83         }
  84         testCase.test();
  85     }
  86 
  87     public static void validate(ConstantPool constantPoolCTVM,
  88                                 ConstantTypes cpType,
  89                                 DummyClasses dummyClass,
  90                                 int i) {
  91         TestedCPEntry entry = cpType.getTestedCPEntry(dummyClass, i);
  92         if (entry == null) {
  93             return;
  94         }
  95         Object classToVerify = CompilerToVMHelper.lookupKlassInPool(constantPoolCTVM, i);
  96         if (!(classToVerify instanceof HotSpotResolvedObjectType) && !(classToVerify instanceof String)) {
  97             String msg = String.format("Output of method CTVM.lookupKlassInPool is neither"
  98                                                + " a HotSpotResolvedObjectType, nor a String");
  99             throw new AssertionError(msg);
 100         }
 101         String classNameToRefer = entry.klass;
 102         String outputToVerify = classToVerify.toString();
 103         if (!outputToVerify.contains(classNameToRefer)) {
 104             String msg = String.format("Wrong class accessed by constant pool index %d: %s, but should be %s",
 105                                        i,
 106                                        outputToVerify,
 107                                        classNameToRefer);
 108             throw new AssertionError(msg);
 109         }
 110     }
 111 }