use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Unreflect_InvokeTracker method test_Unreflect_Public_OuterClass_Nested_SametHierarchy.
/**
* unreflect a method from a level 1 inner class using a lookup class which is a level 2 inner class
* under the same top level outer class.
* Basically we unreflect a method in class C.D where the lookup class is C.D.E
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Public_OuterClass_Nested_SametHierarchy() throws Throwable {
Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample$SamePackageInnerClass");
Method innerclassMethod_Level1 = clazz.getDeclaredMethod("addPublicInner", int.class, int.class);
Lookup level2InnerClassLookup = ((new SamePackageExample()).new SamePackageInnerClass()).new SamePackageInnerClass_Nested_Level2().getLookup();
MethodHandle mh = level2InnerClassLookup.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_Unreflect_Public_CrossPackage_SubClass_Overridden.
/**
* unreflect a subclass overridden method using a superclass lookup, parent child in different packages.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Public_CrossPackage_SubClass_Overridden() throws Throwable {
Class clazz = Class.forName("com.ibm.j9.jsr292.CrossPackageExampleSubclass");
Method subclassOverriddenMethod = clazz.getDeclaredMethod("addPublic", int.class, int.class);
Lookup parentLookup = PackageExamples.getLookup();
MethodHandle mh = parentLookup.unreflect(subclassOverriddenMethod);
// PackageExamples g = new SamePackageExampleSubClass()
CrossPackageExampleSubclass g = new CrossPackageExampleSubclass();
// TODO:- WMT thrown here if 'g' is of the parent type, like above
int out = (int) mh.invokeExact(g, 1, 2);
AssertJUnit.assertEquals(5, out);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Unreflect_InvokeTracker method test_Unreflect_Static_RelatedClassLoaders_ChildLookupInParent.
/**
* unreflect a static method belonging to a class that has been dynamically loaded using a custom class loader A
* using a lookup class which is also dynamically loaded but using a custom class loader B which is a child of A.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_Unreflect_Static_RelatedClassLoaders_ChildLookupInParent() throws Throwable {
ParentCustomClassLoader parentCustomCL = new ParentCustomClassLoader(LookupAPITests_Unreflect.class.getClassLoader());
Class customLoadedClass1 = parentCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass1");
ChildCustomClassLoader childCustomCL = new ChildCustomClassLoader(parentCustomCL);
ICustomLoadedClass customLoadedClass2 = (ICustomLoadedClass) childCustomCL.loadClass("com.ibm.j9.jsr292.CustomLoadedClass2").newInstance();
Lookup childLookup = customLoadedClass2.getLookup();
Method method = customLoadedClass1.getDeclaredMethod("addStatic", int.class, int.class);
MethodHandle mh = childLookup.unreflect(method);
int out = (int) mh.invokeExact(1, 2);
AssertJUnit.assertEquals(103, out);
}
use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.
the class Unreflect_InvokeTracker method test_Unreflect_Protected_InnerClass_Level2_CrossPackage.
/**
* Negative test : unreflect a protected method from a level 2 protected inner class using the top level outer class lookup
* belonging to a different package.
* Basically we unreflect a method from protected inner class packagte1.C.D.E where the lookup class used is package2.F.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Protected_InnerClass_Level2_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 outerclassLookup = PackageExamples.getLookup();
boolean illegalAccessExceptionThrown = false;
try {
outerclassLookup.unreflect(innerclassMethod_Level2);
System.out.println("IllegalAccessException is NOT thrown while trying to unreflect a protected method " + "of a second level 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_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);
}
Aggregations