use of java.lang.invoke.MethodHandles.Lookup 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);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Private_Static_CrossPackage.
/*private static fields ( cross package )*/
/**
* Negative test : findStaticGetter, findStaticSetter tests using private static fields of a class belonging
* to a different package than the lookup class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_Static_CrossPackage() throws Throwable {
boolean illegalAccessExceptionThrown = false;
Lookup publicLookup = MethodHandles.lookup();
try {
publicLookup.findStaticSetter(PackageExamples.class, "staticPrivateField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
illegalAccessExceptionThrown = false;
try {
publicLookup.findStaticGetter(PackageExamples.class, "staticPrivateField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Protected_CrossPackage_InnerClass_Level2.
/**
* Negative test : findSetter, findGetter test using protected fields of an inner classes (level 2 deep)
* where the lookup class is the top level outer class belonging to a different package.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Protected_CrossPackage_InnerClass_Level2() throws Throwable {
Lookup publicLookup = PackageExamples.getLookup();
boolean illegalAccessExceptionThrown = false;
try {
publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticProtectedField_Inner2", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
illegalAccessExceptionThrown = false;
try {
publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticProtectedField_Inner2", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Unreflect_InvokeTracker method test_Unreflect_Protected_InnerClass_Level1_CrossPackage.
/**
* Negative test : unreflect a protected method from a level 1 protected inner class using a top level outer class lookup,
* where the lookup class is in a different package.
* Basically we unreflect a protected method from protected inner class package1.C.D where the lookup class used is package2.F.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Protected_InnerClass_Level1_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 outerclassLookup = PackageExamples.getLookup();
boolean illegalAccessExceptionThrown = false;
try {
outerclassLookup.unreflect(innerclassMethod_Level1);
System.out.println("IllegalAccessException is NOT thrown while trying to unreflect a protected method " + "of a protected inner class belonging to a different package than the lookup class");
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Unreflect_InvokeTracker method test_UnreflectConstructor_SamePackage_SubClass.
/**
*******************************************************************
*unreflectConstructor tests involving inheritance.
*Combinations : parent / child lookups within the same package
********************************************************************
*/
/**
* unreflect subclass constructor using super class lookup, parent child in same package.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_UnreflectConstructor_SamePackage_SubClass() throws Throwable {
Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExampleSubclass");
Constructor c = clazz.getDeclaredConstructor(int.class, int.class);
Lookup parentLookup = SamePackageExample.getLookup();
MethodHandle mh = parentLookup.unreflectConstructor(c);
SamePackageExampleSubclass g = (SamePackageExampleSubclass) mh.invoke(1, 2);
AssertJUnit.assertEquals(g.nonStaticPublicField_Child, 3);
}
Aggregations