Search in sources :

Example 36 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class MethodHandleProxiesTest method test_wrapperInstanceType_NegativeTest.

/**
 * Negative test for wrapperInstanceType using non-wrapper types
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_wrapperInstanceType_NegativeTest() throws Throwable {
    boolean notAWrapperInstance = false;
    try {
        Class<?> retrievedTargetType = MethodHandleProxies.wrapperInstanceType(new PackageExamples());
        AssertJUnit.assertEquals(SamePackageSingleMethodInterfaceExample.class, retrievedTargetType);
    } catch (IllegalArgumentException e) {
        notAWrapperInstance = true;
    }
    AssertJUnit.assertTrue(notAWrapperInstance);
}
Also used : PackageExamples(examples.PackageExamples) Test(org.testng.annotations.Test)

Example 37 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class HelperConstructorClass method test_asVarargsCollector_SamePackage_Virtual_WrongType.

/**
 * Negative test : asVarargsCollector test using wrong method type
 * @throws Throwable
 */
@Test(expectedExceptions = IllegalArgumentException.class, groups = { "level.extended" })
public void test_asVarargsCollector_SamePackage_Virtual_WrongType() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findVirtual(PackageExamples.class, "getLength", MethodType.methodType(int.class, String[].class));
    PackageExamples g = new PackageExamples();
    mh = mh.bindTo(g);
    mh = mh.asVarargsCollector(int[].class);
    Assert.fail("The test case failed to throw an IllegalArgumentException in the case of the wrong method type");
}
Also used : PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 38 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class HelperConstructorClass method test_asSpreader_CrossPackage_Virtual_WrongType.

/**
 * Negative test : asSpreader test using cross package virtual method with a wrong method type
 * @throws Throwable
 */
@Test(expectedExceptions = WrongMethodTypeException.class, groups = { "level.extended" })
public void test_asSpreader_CrossPackage_Virtual_WrongType() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findVirtual(PackageExamples.class, "addPublic", MethodType.methodType(int.class, int.class, int.class));
    PackageExamples g = new PackageExamples();
    mh = mh.bindTo(g);
    mh = mh.asSpreader(String[].class, 2);
    Assert.fail("The test case failed to throw an WrongMethodTypeException in the case of the wrong method type");
}
Also used : PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 39 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class HelperConstructorClass method test_asCollector_CrossPackage_Virtual_WrongType.

/**
 * Negative test : asCollector using wrong type on a MH obtained through a findVirtual call to a class in a different package
 * @throws Throwable
 */
@Test(expectedExceptions = IllegalArgumentException.class, groups = { "level.extended" })
public void test_asCollector_CrossPackage_Virtual_WrongType() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findVirtual(PackageExamples.class, "getLength", MethodType.methodType(int.class, String[].class));
    PackageExamples g = new PackageExamples();
    mh = mh.bindTo(g);
    mh = mh.asCollector(int[].class, 2);
    Assert.fail("The test case failed to throw an IllegalArgumentException in the case of the wrong method type");
}
Also used : PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 40 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class HelperConstructorClass method test_asVarargsCollector_CrossPackage_Virtual_Invoke.

/**
 * Basic asVarargsCollector test using a virtual method in a class living in a different package (uses MethodHandle.invoke)
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_asVarargsCollector_CrossPackage_Virtual_Invoke() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findVirtual(PackageExamples.class, "getLength", MethodType.methodType(int.class, String[].class));
    PackageExamples g = new PackageExamples();
    mh = mh.bindTo(g);
    mh = mh.asVarargsCollector(String[].class);
    Assert.assertEquals(mh.type(), MethodType.methodType(int.class, String[].class));
    Assert.assertEquals(mh.invoke("A", "star", "I am looking at", "may already have", "died", "long ago"), 6);
}
Also used : PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

PackageExamples (examples.PackageExamples)46 Test (org.testng.annotations.Test)46 MethodHandle (java.lang.invoke.MethodHandle)41 CrossPackageSingleMethodInterfaceExample (examples.CrossPackageSingleMethodInterfaceExample)8 Lookup (java.lang.invoke.MethodHandles.Lookup)7 Method (java.lang.reflect.Method)6 Constructor (java.lang.reflect.Constructor)2 Field (java.lang.reflect.Field)2 SamePackageInnerClass (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass)1 CrossPackageInnerClass (examples.PackageExamples.CrossPackageInnerClass)1 ArrayList (java.util.ArrayList)1