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