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.test.lib.Asserts;
  46 import sun.reflect.ConstantPool;
  47 
  48 /**
  49  * Test for {@code compiler.jvmci.CompilerToVM.resolveConstantInPool} method
  50  */
  51 public class ResolveConstantInPoolTest {
  52 
  53     public static void main(String[] args) throws Exception {
  54         Map<ConstantPoolTestsHelper.ConstantTypes,
  55                 ConstantPoolTestCase.Validator> typeTests = new HashMap<>(2);
  56         typeTests.put(ConstantPoolTestsHelper.ConstantTypes.CONSTANT_METHODHANDLE,
  57                 ResolveConstantInPoolTest::validateMethodHandle);
  58         typeTests.put(ConstantPoolTestsHelper.ConstantTypes.CONSTANT_METHODTYPE,
  59                 ResolveConstantInPoolTest::validateMethodType);
  60         ConstantPoolTestCase testCase = new ConstantPoolTestCase(typeTests);
  61         testCase.test();
  62     }
  63 
  64     private static void validateMethodHandle(
  65             jdk.vm.ci.meta.ConstantPool 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(
  81             jdk.vm.ci.meta.ConstantPool constantPoolCTVM,
  82             ConstantPool constantPoolSS,
  83             ConstantPoolTestsHelper.DummyClasses dummyClass, int index) {
  84         Object constantInPool = CompilerToVMHelper
  85                 .resolveConstantInPool(constantPoolCTVM, index);
  86         Class mtToVerify = constantInPool.getClass();
  87         Class mtToRefer = MethodType.class;
  88         String msg = String.format("Wrong %s accessed by constant pool index"
  89                             + " %d: %s, but should be %s", "method type class",
  90                             index, mtToVerify, mtToRefer);
  91         Asserts.assertEQ(mtToRefer, mtToVerify, msg);
  92     }
  93 }