Search in sources :

Example 1 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_Public_CrossPackage_SuperClass.

/**
 * unreflect a super class method using a subclass lookup, parent child in different packages.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Public_CrossPackage_SuperClass() throws Throwable {
    Class clazz = Class.forName("examples.PackageExamples");
    Method parentMethod = clazz.getDeclaredMethod("addPublic", int.class, int.class);
    Lookup childLookup = CrossPackageExampleSubclass.getLookup();
    MethodHandle mh = childLookup.unreflect(parentMethod);
    PackageExamples g = new PackageExamples();
    int out = (int) mh.invokeExact(g, 1, 2);
    AssertJUnit.assertEquals(3, out);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 2 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_Protected_CrossPackage_Overridden.

/**
 * Test to unreflect a protected overridden method belonging to a different package than the Lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_Unreflect_Protected_CrossPackage_Overridden() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.CrossPackageExampleSubclass");
    Method m = clazz.getDeclaredMethod("addProtected", int.class, int.class);
    MethodHandle mh = MethodHandles.lookup().unreflect(m);
    PackageExamples g = new CrossPackageExampleSubclass();
    int out = (int) mh.invoke(g, 1, 2);
    AssertJUnit.assertEquals(-1, out);
}
Also used : Method(java.lang.reflect.Method) PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 3 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_Public_CrossPackage_Overridden.

/**
 * Test to unreflect a public overridden method belonging to a different package than the Lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_Unreflect_Public_CrossPackage_Overridden() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.CrossPackageExampleSubclass");
    Method m = clazz.getDeclaredMethod("addPublic", int.class, int.class);
    MethodHandle mh = MethodHandles.lookup().unreflect(m);
    PackageExamples g = new CrossPackageExampleSubclass();
    int out = (int) mh.invoke(g, 1, 2);
    AssertJUnit.assertEquals(5, out);
}
Also used : Method(java.lang.reflect.Method) PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 4 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Find_InvokeTracker method test_GetterSetter_Public_CrossPackage_SubclassLookup.

/**
 * findSetter, findGetter tests using a sub-class as lookup class and public fields
 * belonging to the super-class under a different package than the lookup ( sub ) class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_CrossPackage_SubclassLookup() throws Throwable {
    Lookup subclassLookup = CrossPackageExampleSubclass.getLookup();
    MethodHandle mhSetter = subclassLookup.findSetter(PackageExamples.class, "nonStaticPublicField", int.class);
    MethodHandle mhGetter = subclassLookup.findGetter(PackageExamples.class, "nonStaticPublicField", int.class);
    PackageExamples g = new PackageExamples();
    mhSetter.invokeExact(g, 5);
    int o = (int) mhGetter.invoke(g);
    AssertJUnit.assertEquals(o, 5);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 5 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class HelperConstructorClass method test_asVarargsCollector_CrossPackage_Virtual_WrongType.

/**
 * Negative test : asVarargsCollector test using wrong type and a virtual method belonging to a class in a different package
 * @throws Throwable
 */
@Test(expectedExceptions = IllegalArgumentException.class, groups = { "level.extended" })
public void test_asVarargsCollector_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.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)

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