Search in sources :

Example 21 with MethodHandles.lookup

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 */
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 22 with MethodHandles.lookup

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 */
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 23 with MethodHandles.lookup

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);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 24 with MethodHandles.lookup

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 */
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 25 with MethodHandles.lookup

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);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) SamePackageInnerClass_Nested_Level2(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2) SamePackageInnerClass_Nested_Level2(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2) SamePackageInnerClass(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

Lookup (java.lang.invoke.MethodHandles.Lookup)218 Test (org.testng.annotations.Test)176 MethodHandle (java.lang.invoke.MethodHandle)96 MethodHandles.publicLookup (java.lang.invoke.MethodHandles.publicLookup)39 Method (java.lang.reflect.Method)37 SamePackageInnerClass (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass)17 MutableCallSite (java.lang.invoke.MutableCallSite)11 MethodHandles (java.lang.invoke.MethodHandles)10 SamePackageInnerClass_Nested_Level2 (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2)7 PackageExamples (examples.PackageExamples)7 MethodHandles.constant (java.lang.invoke.MethodHandles.constant)4 MethodHandles.dropArguments (java.lang.invoke.MethodHandles.dropArguments)4 MethodHandles.exactInvoker (java.lang.invoke.MethodHandles.exactInvoker)4 MethodHandles.foldArguments (java.lang.invoke.MethodHandles.foldArguments)4 MethodType.methodType (java.lang.invoke.MethodType.methodType)4 Constructor (java.lang.reflect.Constructor)4 ArrayList (java.util.ArrayList)4 Objects (java.util.Objects)4 Function (java.util.function.Function)4 ToDoubleFunction (java.util.function.ToDoubleFunction)4