use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_FindVirtual_Public_CrossPackage_Overridden_InnerClass_Nested_Level2.
/**
* findVirtual test using a public overridden method belonging to a second level inner class
* which is a child of a first level inner class under the same outer class. The lookup class
* is a first level inner class in a different package.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_FindVirtual_Public_CrossPackage_Overridden_InnerClass_Nested_Level2() throws Throwable {
PackageExamples pe = new PackageExamples();
CrossPackageInnerClass cp_inner1 = pe.new CrossPackageInnerClass();
Lookup publicLookup = cp_inner1.getLookup();
MethodHandle example = publicLookup.findVirtual(SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2_SubOf_Inner1.class, "addPublicInner", MethodType.methodType(int.class, int.class, int.class));
SamePackageExample.SamePackageInnerClass g = ((new SamePackageExample()).new SamePackageInnerClass2()).new SamePackageInnerClass2_Nested_Level2_SubOf_Inner1();
int s = (int) example.invoke(g, 1, 2);
AssertJUnit.assertEquals(23, s);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Public_Static_CrossPackage_SuperclassLookup.
/**
* findStaticGetter, findStaticSetter tests using public static fields of a sub-class belonging to a different package
* than the lookup class ( the super-class of the class containing the fields being looked up ).
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_Static_CrossPackage_SuperclassLookup() throws Throwable {
Lookup superclassLookup = PackageExamples.getLookup();
MethodHandle mhSetter = superclassLookup.findStaticSetter(CrossPackageExampleSubclass.class, "staticPublicField_Child", int.class);
MethodHandle mhGetter = superclassLookup.findStaticGetter(CrossPackageExampleSubclass.class, "staticPublicField_Child", int.class);
mhSetter.invokeExact(5);
int o = (int) mhGetter.invokeExact();
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_SamePackage_SubClassLookup.
/**
* Negative test : findGetter, findSetter test using a Lookup whose lookup class is a sub-class and
* and lookup fields are private static fields belonging to the superclass of the lookup class under the same
* package as the lookup class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_Static_SamePackage_SubClassLookup() throws Throwable {
Lookup sublassLookup = SamePackageExampleSubclass.getLookup();
boolean illegalAccessExceptionThrown = false;
try {
sublassLookup.findSetter(SamePackageExample.class, "staticPrivateField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
illegalAccessExceptionThrown = false;
try {
sublassLookup.findGetter(SamePackageExample.class, "staticPrivateField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
// make sure we get access exception when trying to access static field with findGetter/findSetter
if (illegalAccessExceptionThrown = false) {
Assert.fail("Illegal access to static private field given to findSetter/findGetter");
}
illegalAccessExceptionThrown = false;
try {
sublassLookup.findStaticSetter(SamePackageExample.class, "staticPrivateField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
/*static setter access to private static fields of the parent
should be given to the child*/
AssertJUnit.assertFalse(illegalAccessExceptionThrown);
illegalAccessExceptionThrown = false;
try {
sublassLookup.findStaticGetter(SamePackageExample.class, "staticPrivateField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
/*static getter access to private static fields of the parent
should be given to the child*/
AssertJUnit.assertFalse(illegalAccessExceptionThrown);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Public_SamePackage_InnerClass_Level1.
/**
* findSetter, findGetter test using fields of an inner classes (level 1 deep) where the
* lookup class is the top outer class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_SamePackage_InnerClass_Level1() throws Throwable {
Lookup publicLookup = MethodHandles.lookup();
MethodHandle mhSetter = publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.class, "nonStaticPublicField_Inner1", int.class);
MethodHandle mhGetter = publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.class, "nonStaticPublicField_Inner1", int.class);
SamePackageExample.SamePackageInnerClass g = (new SamePackageExample()).new SamePackageInnerClass();
mhSetter.invokeExact(g, 5);
int o = (int) mhGetter.invokeExact(g);
AssertJUnit.assertEquals(o, 5);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Public_Static_SamePackage.
/**
* Negative test : findSetter, findGetter test using a Lookup whose lookup class is LookupAPITests_find (this class)
* and lookup fields are public static fields belonging to a different class that is under the same package as
* the lookup class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_Static_SamePackage() throws Throwable {
Lookup publicLookup = MethodHandles.lookup();
boolean illegalAccessExceptionThrown = false;
try {
publicLookup.findSetter(SamePackageExample.class, "staticPublicField", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
// make sure we get access exception when trying to access a static field with findGetter/findSetter
if (illegalAccessExceptionThrown == false) {
Assert.fail("Illegal access to static public field given to findSetter/findGetter");
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
Aggregations