Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/MutableCallSiteTest.java
6007 views
/*******************************************************************************1* Copyright (c) 2001, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9.jsr292;2223import org.testng.annotations.Test;24import org.testng.AssertJUnit;25import java.lang.invoke.MethodHandle;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodType;28import java.lang.invoke.MutableCallSite;29import java.lang.invoke.WrongMethodTypeException;3031/**32* @author mesbah33* This class contains tests for MutableCallSite API34*/35public class MutableCallSiteTest {36/**37* Basic sanity test for MutableCallSite38* @throws Throwable39*/40@Test(groups = { "level.extended" })41public void testBasic_MutableCallSite() throws Throwable {42MethodType mt = MethodType.methodType(double.class,String.class);43MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", mt);44MutableCallSite mcs = new MutableCallSite(mt);45mcs.setTarget(mh);46double d = (double)mcs.dynamicInvoker().invokeExact("1.1");4748AssertJUnit.assertEquals(1.1,d);49}5051/**52* Test for validating default MethodHandle behavior of MutableCallSite53* using MutableCallSite.dynamicInvoker().invoke()54* @throws Throwable55*/56@Test(groups = { "level.extended" })57public void testIllegalState_dynamicInvoker_MutableCallSite() throws Throwable {58MethodType mt = MethodType.methodType(void.class);59MutableCallSite mcs = new MutableCallSite(mt);60boolean iseHit = false;6162try {63mcs.dynamicInvoker().invoke();64}65catch ( IllegalStateException e ) {66iseHit = true;67}6869AssertJUnit.assertTrue(iseHit);70}7172/**73* Test for validating default MethodHandle behavior of MutableCallSite74* using MutableCallSite.getTarget().invoke()75* @throws Throwable76*/77@Test(groups = { "level.extended" })78public void testIllegalState_getTarget_MutableCallSite() throws Throwable {79MethodType mt = MethodType.methodType(void.class);80MutableCallSite mcs = new MutableCallSite(mt);81boolean iseHit = false;8283try {84mcs.getTarget().invoke();85}86catch ( IllegalStateException e ) {87iseHit = true;88}8990AssertJUnit.assertTrue(iseHit);91}9293/**94* Basic negative test for MutableCallSite95* @throws Throwable96*/97@Test(groups = { "level.extended" })98public void testBasicNegative_MutableCallSite() throws Throwable {99MethodHandle mh = MethodHandles.lookup().findStatic(Math.class, "pow", MethodType.methodType(double.class,double.class,double.class));100MutableCallSite mcs = new MutableCallSite(mh);101102boolean wmtThrown = false;103104try {105String s = (String)mcs.dynamicInvoker().invokeExact(2,3);106}107catch ( WrongMethodTypeException e ) {108wmtThrown = true;109}110111AssertJUnit.assertTrue(wmtThrown);112}113114/**115* Test for MutableCallSite.type()116* @throws Throwable117*/118@Test(groups = { "level.extended" })119public void testType() throws Throwable {120MethodType mt = MethodType.methodType(String.class);121MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", mt);122MutableCallSite mcs = new MutableCallSite(mh);123124AssertJUnit.assertEquals(mt,mcs.type());125}126127/**128* MutableCallSite subclass that overrides setTarget, one where setTarget calls the inherited one.129* @throws Throwable130*/131@Test(groups = { "level.extended" })132public void testMCSChild_CallsParent() throws Throwable {133MethodType mt = MethodType.methodType(double.class,String.class);134MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", mt);135MCSChild1 mcs = new MCSChild1(mt);136mcs.setTarget(mh);137double d = (double)mcs.dynamicInvoker().invokeExact("1.1");138139AssertJUnit.assertEquals(1.1,d);140}141142/**143* MutableCallSite subclass that overrides setTarget, one where setTarget does not calls the inherited one.144* @throws Throwable145*/146@Test(groups = { "level.extended" })147public void testMCSChild_DoesNotCallParent() throws Throwable {148MethodType mt = MethodType.methodType(double.class,String.class);149MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", mt);150MCSChild2 mcs = new MCSChild2(mt);151mcs.setTarget(mh);152153AssertJUnit.assertEquals(mcs.ignoredTarget.toString(),mh.toString());154}155}156157158159160