1 /*
   2  * Copyright (c) 2015, 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.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @build compiler.jvmci.compilerToVM.ResolveConstantInPoolTest
  32  * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
  33  * @run main/othervm -Xbootclasspath/a:.
  34  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  35  *                   compiler.jvmci.compilerToVM.ResolveConstantInPoolTest
  36  */
  37 
  38 package compiler.jvmci.compilerToVM;
  39 
  40 import java.lang.invoke.MethodHandle;
  41 import java.lang.invoke.MethodType;
  42 import java.util.HashMap;
  43 import java.util.Map;
  44 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  45 import jdk.vm.ci.hotspot.HotSpotConstantPool;
  46 import jdk.test.lib.Asserts;
  47 import sun.reflect.ConstantPool;
  48 
  49 /**
  50  * Test for {@code compiler.jvmci.CompilerToVM.resolveConstantInPool} method
  51  */
  52 public class ResolveConstantInPoolTest {
  53 
  54     public static void main(String[] args) throws Exception {
  55         Map<ConstantPoolTestsHelper.ConstantTypes,
  56                 ConstantPoolTestCase.Validator> typeTests = new HashMap<>(2);
  57         typeTests.put(ConstantPoolTestsHelper.ConstantTypes.CONSTANT_METHODHANDLE,
  58                 ResolveConstantInPoolTest::validateMethodHandle);
  59         typeTests.put(ConstantPoolTestsHelper.ConstantTypes.CONSTANT_METHODTYPE,
  60                 ResolveConstantInPoolTest::validateMethodType);
  61         ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
  62         testCase.test();
  63     }
  64 
  65     private static void validateMethodHandle(HotSpotConstantPool constantPoolCTVM,
  66             ConstantPool constantPoolSS,
  67             ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
  68         Object constantInPool = CompilerToVMHelper
  69                 .resolveConstantInPool(constantPoolCTVM, index);
  70         if (!(constantInPool instanceof MethodHandle)) {
  71             String msg = String.format(
  72                     "Wrong constant pool entry accessed by index"
  73                             + " %d: %s, but should be subclass of %s",
  74                     index + 1, constantInPool.getClass(),
  75                     MethodHandle.class.getName());
  76             throw new AssertionError(msg);
  77         }
  78     }
  79 
  80     private static void validateMethodType(HotSpotConstantPool constantPoolCTVM,
  81             ConstantPool constantPoolSS,
  82             ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
  83         Object constantInPool = CompilerToVMHelper
  84                 .resolveConstantInPool(constantPoolCTVM, index);
  85         Class mtToVerify = constantInPool.getClass();
  86         Class mtToRefer = MethodType.class;
  87         String msg = String.format("Wrong %s accessed by constant pool index"
  88                             + " %d: %s, but should be %s", "method type class",
  89                             index, mtToVerify, mtToRefer);
  90         Asserts.assertEQ(mtToRefer, mtToVerify, msg);
  91     }
  92 }