Search in sources :

Example 21 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_Constructor_CrossPackage.

/**
 * Test to unreflect a constructor belonging to a different package than the Lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_Unreflect_Constructor_CrossPackage() throws Throwable {
    Class clazz = Class.forName("examples.PackageExamples");
    Constructor c = clazz.getDeclaredConstructor(int.class, int.class);
    MethodHandle mh = MethodHandles.lookup().unreflectConstructor(c);
    PackageExamples example = (PackageExamples) mh.invoke(1, 2);
    AssertJUnit.assertEquals(example.nonStaticPublicField, 3);
}
Also used : Constructor(java.lang.reflect.Constructor) PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 22 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Find_InvokeTracker method test_FindVirtual_Protected_CrossPackage_Overridden_SubclassLookup.

/**
 * findVirtual test with a protected overridden method belonging to a super-class of the lookup class that is on a different package than the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_FindVirtual_Protected_CrossPackage_Overridden_SubclassLookup() throws Throwable {
    MethodHandle example = CrossPackageExampleSubclass.getLookup().findVirtual(PackageExamples.class, "addProtected", MethodType.methodType(int.class, int.class, int.class));
    PackageExamples g = new PackageExamples();
    try {
        int s = (int) example.invoke(g, 1, 2);
        Assert.fail("Should have thrown ClassCastException");
    } catch (ClassCastException e) {
    }
    CrossPackageExampleSubclass cpes = new CrossPackageExampleSubclass();
    int s = (int) example.invoke(cpes, 1, 2);
    AssertJUnit.assertEquals(cpes.addProtected(1, 2), s);
}
Also used : PackageExamples(examples.PackageExamples) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 23 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_Protected_InnerClass_Nested_DifferentHierarchy_CrossPackage.

/**
 * Negative test : unreflect a protected method from a level 2 protected inner class using a lookup class which is a
 * level 1 public inner class under a top level outer class belonging to a different package than the lookup class.
 * Basically we unreflect a method in class package1.C.D.E where the lookup class is package2.F.G.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Protected_InnerClass_Nested_DifferentHierarchy_CrossPackage() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample$SamePackageInnerClass_Protected$SamePackageInnerClass_Nested_Level2");
    Method innerclassMethod_Level2 = clazz.getDeclaredMethod("addProtectedInner_Level2", int.class, int.class);
    Lookup level1InnerClassLookup_crossPackage = (new PackageExamples()).new CrossPackageInnerClass().getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        level1InnerClassLookup_crossPackage.unreflect(innerclassMethod_Level2);
        System.out.println("IllegalAccessException NOT thrown while attempting to access " + "a protected method inside a level 2 protected inner class in a different package");
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) PackageExamples(examples.PackageExamples) Test(org.testng.annotations.Test)

Example 24 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_Protected_OuterClass_Nested_DifferentHierarchy_CrossPackage.

/**
 * Negative test : unreflect a protected method from a level 1 protected inner class using a lookup class which is a level 2 public inner class
 * under a top level outer class belonging to a different package than the lookup class.
 * Basically we unreflect a protected method in class package1.C.D where the lookup class is package2.F.G.H.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Protected_OuterClass_Nested_DifferentHierarchy_CrossPackage() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample$SamePackageInnerClass_Protected");
    Method innerclassMethod_Level1 = clazz.getDeclaredMethod("addProtectedInner", int.class, int.class);
    Lookup level2InnerClassLookup = ((new PackageExamples()).new CrossPackageInnerClass()).new CrossPackageInnerClass2_Nested_Level2().getLookup();
    boolean illegalAccessException = false;
    try {
        level2InnerClassLookup.unreflect(innerclassMethod_Level1);
        System.out.println("IllegalAccessExcetpion NOT thrown while attempting to unreflect a protected inner class " + "method from a different class");
    } catch (IllegalAccessException e) {
        illegalAccessException = true;
    }
    AssertJUnit.assertTrue(illegalAccessException);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) PackageExamples(examples.PackageExamples) Test(org.testng.annotations.Test)

Example 25 with PackageExamples

use of examples.PackageExamples in project openj9 by eclipse.

the class Unreflect_InvokeTracker method test_Unreflect_SetterGetter_Private_CrossPackage.

/**
 * Negative test : unreflectSetter/getter test using a private method belonging to a different package than the Lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_Unreflect_SetterGetter_Private_CrossPackage() throws Throwable {
    PackageExamples g = new PackageExamples();
    Field nonStaticPrivateField = PackageExamples.class.getDeclaredField("nonStaticPrivateField");
    MethodHandle mhSetter = null;
    boolean illegalAccessExceptionThrown = false;
    try {
        mhSetter = MethodHandles.lookup().unreflectSetter(nonStaticPrivateField);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    nonStaticPrivateField.setAccessible(true);
    mhSetter = MethodHandles.lookup().unreflectSetter(nonStaticPrivateField);
    mhSetter.invokeExact(g, 100);
    MethodHandle mhGetter = MethodHandles.lookup().unreflectGetter(nonStaticPrivateField);
    int out = (int) mhGetter.invokeExact(g);
    AssertJUnit.assertEquals(100, out);
}
Also used : Field(java.lang.reflect.Field) 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