use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Test_ReflectionAndMethodHandles method test_unreflectSpecial_child_public_static_method.
/**
* This method is a negative test to verify that the methodHandle for the public static method
* from the child interface cannot be successfully produced using unreflectSpecial.
* An IllegalAccessException is expected to be thrown
* @throws IllegalAccessException
*/
@Test
public static void test_unreflectSpecial_child_public_static_method() throws Throwable {
Class<?> c = Class.forName("ITestChild", true, loadClass);
Method method = c.getDeclaredMethod("child_public_static_method");
method.setAccessible(true);
Lookup lookup = getLookupObject(c);
try {
MethodHandle mh = lookup.unreflectSpecial(method, c);
Assert.fail("Should have thrown an IllegalAccessException");
} catch (IllegalAccessException e) {
/* Pass */
}
}
use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Test_ReflectionAndMethodHandles method test_find_static_ancestor_public_static.
/**
* This method is a negative test to verify that the methodHandle for the public static
* method from the parent interface ITest cannot be successfully looked up using findStatic.
* A NoSuchMethodException is expected to be thrown
* @throws NoSuchMethodException
*/
@Test
public static void test_find_static_ancestor_public_static() throws Exception {
Class<?> c = Class.forName("ITestChild", true, loadClass);
Lookup lookup = getLookupObject(c);
try {
MethodHandle mh = lookup.findStatic(c, "RunTest_private_static_method_with_InvokeStatic", type);
Assert.fail("Should have thrown a NoSuchMethodException");
} catch (NoSuchMethodException e) {
/* Pass */
}
}
use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Test_ReflectionAndMethodHandles method test_unreflectSpecial_child_private_non_static_method.
/**
* This method is used to verify if the method handle for a private non static method
* in our child interface can be successfully produced using unreflectSpecial, and then
* successfully invoke the method to verify that the correct MethodHandle was found
*/
@Test
public static void test_unreflectSpecial_child_private_non_static_method() throws Throwable {
Class<?> c = Class.forName("ITestChild", true, loadClass);
Method method = c.getDeclaredMethod("child_private_non_static_method");
method.setAccessible(true);
Lookup lookup = getLookupObject(c);
MethodHandle mh = lookup.unreflectSpecial(method, c);
String returnStatement = (String) mh.invoke(instance);
AssertJUnit.assertEquals("Failing as private non static method of ITestChild interface didnt return expected output", expected_child_private_non_static, returnStatement);
}
use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Test_ReflectionAndMethodHandles method test_find_virtual_child_private_static.
/**
* This method is a negative test to verify that the methodHandle for the private static
* method from the child interface cannot be successfully looked up using findVirtual.
* An IllegalAccessException is expected to be thrown
* @throws IllegalAccessException
*/
@Test
public static void test_find_virtual_child_private_static() throws Exception {
Class<?> c = Class.forName("ITestChild", true, loadClass);
Lookup lookup = getLookupObject(c);
try {
MethodHandle mh = lookup.findVirtual(c, "child_private_static_method", type);
Assert.fail("Should have thrown an IllegalAccessException");
} catch (IllegalAccessException e) {
/* Pass */
}
}
use of java.lang.invoke.MethodHandles.lookup in project openj9 by eclipse.
the class Find_InvokeTracker method test_GetterSetter_Public_SamePackage_InnerClass_Level2.
/**
* findSetter, findGetter test using 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_Public_SamePackage_InnerClass_Level2() throws Throwable {
Lookup publicLookup = MethodHandles.lookup();
MethodHandle mhSetter = publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPublicField_Inner2", int.class);
MethodHandle mhGetter = publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPublicField_Inner2", int.class);
SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2 g = ((new SamePackageExample()).new SamePackageInnerClass()).new SamePackageInnerClass_Nested_Level2();
mhSetter.invokeExact(g, 5);
int o = (int) mhGetter.invokeExact(g);
AssertJUnit.assertEquals(o, 5);
}
Aggregations