use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_StaticGetterSetter_Public_Static_SamePackage_SuperclassLookup.
/**
* findStaticGetter, findStaticSetter test using a Lookup whose lookup class is a super-class
* and lookup fields are public static fields belonging to a subclass of the super-class under the same package as
* the lookup class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_StaticGetterSetter_Public_Static_SamePackage_SuperclassLookup() throws Throwable {
Lookup superclassLookup = SamePackageExample.getLookup();
/*
* CMVC: 190581
*
* Works in J9 but fails in Oracle.
* Error summary: The following line of code throws java.lang.InternalError: uncaught exception in Oracle, "Caused by: java.lang.NoSuchFieldException: staticPublicField"
* Note: The 'staticPublicField' belongs to the parent of SamePackageExampleSubclass.
**/
MethodHandle mhSetter = superclassLookup.findStaticSetter(SamePackageExampleSubclass.class, "staticPublicField", int.class);
MethodHandle mhGetter = superclassLookup.findStaticGetter(SamePackageExampleSubclass.class, "staticPublicField", 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_Protected_CrossPackage_SuperclassLookup.
/**
* Negative test : findSetter, findGetter tests involving protected fields of a sub-class belonging to a different package than the Lookup ( super ) class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Protected_CrossPackage_SuperclassLookup() throws Throwable {
Lookup superclassLookup = PackageExamples.getLookup();
try {
superclassLookup.findSetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField_Child", int.class);
Assert.fail("Superclass lookup Shouldn't be able to findSetter a protected field of subclass");
} catch (IllegalAccessException e) {
// Pass
}
try {
superclassLookup.findGetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField_Child", int.class);
Assert.fail("Superclass lookup Shouldn't be able to findGetter a protected field of subclass");
} catch (IllegalAccessException e) {
// Pass
}
/* Using superclass protected fields */
superclassLookup.findSetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField", int.class);
superclassLookup.findGetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField", int.class);
}
use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Private_SamePackage_InnerClass_Level2.
/**
* Negative test : findSetter, findGetter test using private fields of an inner classes (level 2 deep) where the
* lookup class is the top outer class.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_SamePackage_InnerClass_Level2() throws Throwable {
Lookup publicLookup = MethodHandles.lookup();
boolean illegalAccessExceptionThrown = false;
try {
publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPrivateField_Inner2", int.class);
} catch (IllegalAccessException e) {
illegalAccessExceptionThrown = true;
}
AssertJUnit.assertTrue(illegalAccessExceptionThrown);
illegalAccessExceptionThrown = false;
try {
publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPrivateField_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_Public_InnerClass_Level1.
/**
***************************************************************************************************
* unreflect tests involving inner classes.
* Combination : Different levels of inner/outer class public/protected methods unreflected using
* different levels of outer/inner public/protected classes, same package/cross package.
*****************************************************************************************************
*
* /**
* unreflect a public method from a level 1 inner class using the top level outer class lookup.
* Basically we unreflect a method from class C.D where the lookup class used is C.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Public_InnerClass_Level1() throws Throwable {
Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample$SamePackageInnerClass");
Method innerclassMethod_Level1 = clazz.getDeclaredMethod("addPublicInner", int.class, int.class);
Lookup outerclassLookup = SamePackageExample.getLookup();
MethodHandle mh = outerclassLookup.unreflect(innerclassMethod_Level1);
SamePackageExample.SamePackageInnerClass g = (new SamePackageExample()).new SamePackageInnerClass();
int out = (int) mh.invokeExact(g, 1, 2);
AssertJUnit.assertEquals(3, out);
}
use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Unreflect_InvokeTracker method test_UnreflectConstructor_SamePackage_SuperClass.
/**
* unreflect a super class constructor using a subclass lookup, parent child in same package
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_UnreflectConstructor_SamePackage_SuperClass() throws Throwable {
Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample");
Constructor c = clazz.getDeclaredConstructor(int.class, int.class);
Lookup childLookup = SamePackageExampleSubclass.getLookup();
MethodHandle mh = childLookup.unreflectConstructor(c);
SamePackageExample g = (SamePackageExample) mh.invoke(1, 2);
AssertJUnit.assertEquals(g.nonStaticPublicField, 3);
}
Aggregations