Search in sources :

Example 11 with MutableCallSite

use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.

the class JSR292_MultiThreadedTests method testSyncAll.

/**
 * The test creates a MutableCallSite object, uses 1 separate Worker thread to change the target of the MutableCallSite,
 * makes a call to MutableCallSite.syncAll() and validates that the change to the target by the worker thread is visible to the "main" thread.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testSyncAll() throws Throwable {
    MethodHandle mh = null;
    MutableCallSite mutableCS = null;
    mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));
    mutableCS = new MutableCallSite(mh);
    String s = (String) mutableCS.dynamicInvoker().invokeExact();
    AssertJUnit.assertEquals("1", s);
    Thread workerThread = new WorkerThread_CS(mutableCS);
    workerThread.start();
    /* Sleep so that the worker thread has an opportunity to run */
    for (int i = 0; i < 4; i++) {
        Thread.sleep(1000);
        if (workerThread.isAlive() == false) {
            break;
        }
    }
    /*Call to MutableCallSite.syncAll(). We expect update made by worker thread to be visible to the "main" thread now*/
    MutableCallSite.syncAll(new MutableCallSite[] { mutableCS });
    s = (String) mutableCS.dynamicInvoker().invokeExact();
    boolean updatePerformed = false;
    if (!s.equals("2")) {
        System.out.println("WARNING: testSyncAll: MutableCallSite update not visible after 4 seconds");
        System.out.println("WARNING: testSyncAll: joining worker thread");
        workerThread.join();
        /*Call to MutableCallSite.syncAll(). We expect update made by worker thread to be visible to the "main" thread now*/
        MutableCallSite.syncAll(new MutableCallSite[] { mutableCS });
        s = (String) mutableCS.dynamicInvoker().invokeExact();
        if (s.equals("2")) {
            updatePerformed = true;
        } else {
            System.out.println("ERROR: testSyncAll: MutableCallSite update made by worker thread not visible in main thread after syncAll");
        }
    } else {
        updatePerformed = true;
    }
    AssertJUnit.assertTrue(updatePerformed);
}
Also used : MutableCallSite(java.lang.invoke.MutableCallSite) SwitchPoint(java.lang.invoke.SwitchPoint) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 12 with MutableCallSite

use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.

the class MutableCallSiteTest method testIllegalState_dynamicInvoker_MutableCallSite.

/**
 * Test for validating default MethodHandle behavior of MutableCallSite
 * using MutableCallSite.dynamicInvoker().invoke()
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testIllegalState_dynamicInvoker_MutableCallSite() throws Throwable {
    MethodType mt = MethodType.methodType(void.class);
    MutableCallSite mcs = new MutableCallSite(mt);
    boolean iseHit = false;
    try {
        mcs.dynamicInvoker().invoke();
    } catch (IllegalStateException e) {
        iseHit = true;
    }
    AssertJUnit.assertTrue(iseHit);
}
Also used : MethodType(java.lang.invoke.MethodType) MutableCallSite(java.lang.invoke.MutableCallSite) Test(org.testng.annotations.Test)

Example 13 with MutableCallSite

use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.

the class MutableCallSiteTest method testBasicNegative_MutableCallSite.

/**
 * Basic negative test for MutableCallSite
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testBasicNegative_MutableCallSite() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findStatic(Math.class, "pow", MethodType.methodType(double.class, double.class, double.class));
    MutableCallSite mcs = new MutableCallSite(mh);
    boolean wmtThrown = false;
    try {
        String s = (String) mcs.dynamicInvoker().invokeExact(2, 3);
    } catch (WrongMethodTypeException e) {
        wmtThrown = true;
    }
    AssertJUnit.assertTrue(wmtThrown);
}
Also used : MutableCallSite(java.lang.invoke.MutableCallSite) WrongMethodTypeException(java.lang.invoke.WrongMethodTypeException) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 14 with MutableCallSite

use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.

the class BootstrapMethods method mcsBootstrap.

public static CallSite mcsBootstrap(Lookup ignored, String name, MethodType type) throws Throwable {
    Lookup lookup = lookup();
    MethodHandle add = lookup.findStatic(BootstrapMethods.class, "add", methodType(int.class, int.class, int.class));
    MutableCallSite mcs = new MutableCallSite(add);
    return new MutableCallSite(mcs.dynamicInvoker());
}
Also used : MutableCallSite(java.lang.invoke.MutableCallSite) Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle)

Example 15 with MutableCallSite

use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.

the class BootstrapMethods method catchBootstrap.

public static CallSite catchBootstrap(Lookup ignored, String name, MethodType type) throws Throwable {
    Lookup lookup = lookup();
    MethodHandle add = lookup.findStatic(BootstrapMethods.class, "add", methodType(int.class, int.class, int.class));
    MethodHandle catchHandle = catchException(add, RuntimeException.class, throwException(add.type().returnType(), RuntimeException.class));
    return new MutableCallSite(catchHandle);
}
Also used : MutableCallSite(java.lang.invoke.MutableCallSite) Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MutableCallSite (java.lang.invoke.MutableCallSite)17 MethodHandle (java.lang.invoke.MethodHandle)15 Lookup (java.lang.invoke.MethodHandles.Lookup)7 Test (org.testng.annotations.Test)7 MethodType (java.lang.invoke.MethodType)4 SwitchPoint (java.lang.invoke.SwitchPoint)3 ConstantCallSite (java.lang.invoke.ConstantCallSite)1 WrongMethodTypeException (java.lang.invoke.WrongMethodTypeException)1