1 /*
   2  * Copyright (c) 2015, 2016, 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 /*
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  29  * @library /testlibrary /test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @build sun.hotspot.WhiteBox
  32  *        compiler.jvmci.compilerToVM.ResolveConstantInPoolTest
  33  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  34  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  35  *                              jdk.vm.ci.hotspot.CompilerToVMHelper
  36  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  37  *                   -XX:+WhiteBoxAPI
  38  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  39  *                   compiler.jvmci.compilerToVM.ResolveConstantInPoolTest
  40  */
  41 
  42 package compiler.jvmci.compilerToVM;
  43 
  44 import compiler.jvmci.compilerToVM.ConstantPoolTestsHelper.DummyClasses;
  45 import java.lang.invoke.MethodHandle;
  46 import java.lang.invoke.MethodType;
  47 import java.util.HashMap;
  48 import java.util.Map;
  49 import jdk.test.lib.Asserts;
  50 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  51 import jdk.vm.ci.meta.ConstantPool;
  52 
  53 /**
  54  * Test for {@code jdk.vm.ci.hotspot.CompilerToVM.resolveConstantInPool} method
  55  */
  56 public class ResolveConstantInPoolTest {
  57 
  58     private static final String NOT_NULL_MSG
  59             = "Object returned by resolveConstantInPool method should not be null";
  60 
  61     public static void main(String[] args) throws Exception {
  62         Map<ConstantPoolTestCase.ConstantTypes,
  63                 ConstantPoolTestCase.Validator> typeTests = new HashMap<>();
  64         typeTests.put(ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODHANDLE,
  65                 ResolveConstantInPoolTest::validateMethodHandle);
  66         typeTests.put(ConstantPoolTestCase.ConstantTypes.CONSTANT_METHODTYPE,
  67                 ResolveConstantInPoolTest::validateMethodType);
  68         ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
  69         testCase.test();
  70         // The next "Class.forName" and repeating "testCase.test()"
  71         // are here for the following reason.
  72         // The first test run is without dummy class initialization,
  73         // which means no constant pool cache exists.
  74         // The second run is with initialized class (with constant pool cache available).
  75         // Some CompilerToVM methods require different input
  76         // depending on whether CP cache exists or not.
  77         for (DummyClasses dummy : DummyClasses.values()) {
  78             Class.forName(dummy.klass.getName());
  79         }
  80         testCase.test();
  81     }
  82 
  83     private static void validateMethodHandle(ConstantPool constantPoolCTVM,
  84             ConstantPoolTestCase.ConstantTypes cpType,
  85             ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
  86         Object constantInPool = CompilerToVMHelper
  87                 .resolveConstantInPool(constantPoolCTVM, index);
  88         String msg = String.format("%s for index %d", NOT_NULL_MSG, index);
  89         Asserts.assertNotNull(constantInPool, msg);
  90         if (!(constantInPool instanceof MethodHandle)) {
  91             msg = String.format("Wrong constant pool entry accessed by index"
  92                     + " %d: %s, but should be subclass of %s",
  93                     index, constantInPool.getClass(),
  94                     MethodHandle.class.getName());
  95             throw new AssertionError(msg);
  96         }
  97     }
  98 
  99     private static void validateMethodType(ConstantPool constantPoolCTVM,
 100             ConstantPoolTestCase.ConstantTypes cpType,
 101             ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
 102         Object constantInPool = CompilerToVMHelper
 103                 .resolveConstantInPool(constantPoolCTVM, index);
 104         String msg = String.format("%s for index %d", NOT_NULL_MSG, index);
 105         Asserts.assertNotNull(constantInPool, msg);
 106         Class mtToVerify = constantInPool.getClass();
 107         Class mtToRefer = MethodType.class;
 108         msg = String.format("Wrong method type class accessed by"
 109                 + " constant pool index %d", index);
 110         Asserts.assertEQ(mtToRefer, mtToVerify, msg);
 111     }
 112 }