1 /*
   2  * Copyright (c) 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  * @test
  26  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  27  * @library /testlibrary /test/lib /compiler/jvmci/jdk.vm.ci.hotspot.test/src
  28  * @modules jdk.vm.ci/jdk.vm.ci.meta
  29  *          jdk.vm.ci/jdk.vm.ci.runtime
  30  *          jdk.vm.ci/jdk.vm.ci.hotspot
  31  * @run testng/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  32  *      jdk.vm.ci.hotspot.test.MethodHandleAccessProviderTest
  33  */
  34 
  35 package jdk.vm.ci.hotspot.test;
  36 
  37 import java.lang.invoke.MethodHandle;
  38 import java.lang.invoke.MethodHandles;
  39 import java.lang.reflect.Method;
  40 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  41 import jdk.vm.ci.meta.MethodHandleAccessProvider;
  42 import jdk.vm.ci.meta.ResolvedJavaMethod;
  43 import jdk.vm.ci.runtime.JVMCI;
  44 import jdk.vm.ci.meta.JavaConstant;
  45 import jdk.vm.ci.meta.MetaAccessProvider;
  46 import jdk.vm.ci.meta.MethodHandleAccessProvider.IntrinsicMethod;
  47 import org.testng.annotations.Test;
  48 import org.testng.Assert;
  49 
  50 public class MethodHandleAccessProviderTest {
  51     private static final HotSpotConstantReflectionProvider CONSTANT_REFLECTION = (HotSpotConstantReflectionProvider) JVMCI.getRuntime().getHostJVMCIBackend().getConstantReflection();
  52     private static final MethodHandleAccessProvider PROVIDER = CONSTANT_REFLECTION.getMethodHandleAccess();
  53     private static final MetaAccessProvider META_ACCESS = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
  54 
  55     @Test(dataProvider = "intrinsicsPositive", dataProviderClass = MethodHandleAccessProviderData.class)
  56     public void testLookupMethodHandleIntrinsic(ResolvedJavaMethod mtd, IntrinsicMethod expected) {
  57         Assert.assertEquals(expected, PROVIDER.lookupMethodHandleIntrinsic(mtd), "Unexpected intrinsic returned for " + mtd);
  58     }
  59 
  60     @Test(dataProvider = "intrinsicsNegative", dataProviderClass = MethodHandleAccessProviderData.class)
  61     public void testLookupMethodHandleIntrinsicNegative(ResolvedJavaMethod mtd) {
  62         Assert.assertNull(PROVIDER.lookupMethodHandleIntrinsic(mtd), "Expected null return for " + mtd);
  63     }
  64 
  65     @Test(expectedExceptions = {NullPointerException.class})
  66     public void testLookupMethodHandleIntrinsicNull() {
  67         PROVIDER.lookupMethodHandleIntrinsic(null);
  68     }
  69 
  70     @Test(dataProvider = "invokeBasicPositive", dataProviderClass = MethodHandleAccessProviderData.class)
  71     public void testResolveInvokeBasicTarget(JavaConstant javaConstantMethodHandle, boolean force, String expected) {
  72         ResolvedJavaMethod mtd = PROVIDER.resolveInvokeBasicTarget(javaConstantMethodHandle, force);
  73         Assert.assertTrue(mtd.getName().startsWith(expected), "Unexpected method resolved: " + mtd);
  74     }
  75 
  76     @Test(dataProvider = "invokeBasicNegative1", dataProviderClass = MethodHandleAccessProviderData.class)
  77     public void testResolveInvokeBasicTargetNegative1(JavaConstant javaConstantMethodHandle, boolean force) {
  78         Assert.assertNull(PROVIDER.resolveInvokeBasicTarget(javaConstantMethodHandle, force),
  79                         "Expected null return for " + javaConstantMethodHandle + " with force=" + force);
  80     }
  81 
  82     @Test(dataProvider = "invokeBasicNegative2", dataProviderClass = MethodHandleAccessProviderData.class, expectedExceptions = {NullPointerException.class})
  83     public void testResolveInvokeBasicTargetNegative2(JavaConstant javaConstantMethodHandle, boolean force) {
  84         PROVIDER.resolveInvokeBasicTarget(javaConstantMethodHandle, force);
  85     }
  86 
  87     @Test
  88     public void testResolveLinkToTarget() {
  89         Method self;
  90         try {
  91             self = getClass().getDeclaredMethod("testResolveLinkToTarget");
  92         } catch (NoSuchMethodException e) {
  93             throw new Error("TESTBUG: can't find method: " + e, e);
  94         }
  95         MethodHandle mh;
  96         try {
  97             mh = MethodHandles.lookup().unreflect(self);
  98         } catch (IllegalAccessException e) {
  99             throw new Error("TESTBUG: can't get MHandle: " + e, e);
 100         }
 101         Method internalMemberNameMethod;
 102         try {
 103             internalMemberNameMethod = mh.getClass().getDeclaredMethod("internalMemberName");
 104         } catch (NoSuchMethodException e) {
 105             throw new Error("TESTBUG: can't find method: " + e, e);
 106         }
 107         internalMemberNameMethod.setAccessible(true);
 108         Object memberName;
 109         try {
 110             memberName = internalMemberNameMethod.invoke(mh);
 111         } catch (ReflectiveOperationException e) {
 112             throw new Error("TESTBUG: can't invoke internalMemberName method", e);
 113         }
 114         JavaConstant jcMemberName = CONSTANT_REFLECTION.forObject(memberName);
 115         ResolvedJavaMethod mtd = PROVIDER.resolveLinkToTarget(jcMemberName);
 116         Assert.assertEquals(mtd, META_ACCESS.lookupJavaMethod(self), "Got unexpected method: " + mtd);
 117     }
 118 
 119     @Test(expectedExceptions = {NullPointerException.class})
 120     public void testResolveLinkToTargetNegativeNull() {
 121         PROVIDER.resolveLinkToTarget(null);
 122     }
 123 
 124     @Test
 125     public void testResolveLinkToTargetNegativeNullConstant() {
 126         Assert.assertNull(PROVIDER.resolveLinkToTarget(JavaConstant.NULL_POINTER), "Expected null return");
 127     }
 128 
 129     @Test(expectedExceptions = {IllegalArgumentException.class})
 130     public void testResolveLinkToTargetNegativeWrongConstant() {
 131         PROVIDER.resolveLinkToTarget(CONSTANT_REFLECTION.forObject("42"));
 132     }
 133 }