Search in sources :

Example 16 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class Test_ReflectionAndMethodHandles method test_find_virtual_ancestor_public_non_static.

/**
 * This method is a test to verify that the methodHandle for the public non static
 * method from the parent interface ITest can be successfully looked up using findVirtual, and then a
 * negative test to verify that the method cannot be invoked. Here an IncompatibleClassChangeError is
 * expected to be thrown
 * @throws IncompatibleClassChangeError
 */
@Test
public static void test_find_virtual_ancestor_public_non_static() throws Throwable {
    Class<?> c = Class.forName("ITestChild", true, loadClass);
    Lookup lookup = getLookupObject(c);
    MethodHandle mh = lookup.findVirtual(c, "RunTest_private_non_static_method_with_InvokeStatic", type);
    try {
        String returnStatement = (String) mh.invoke(instance);
        Assert.fail("Failing as expected IncompatibleClassChangeError to be thrown when invoking method " + "RunTest_private_non_static_method_with_InvokeStatic");
    } catch (IncompatibleClassChangeError e) {
    /* Pass */
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 17 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class Test_ReflectionAndMethodHandles method test_unreflectSpecial_interface_ITest_private_static_method.

/**
 * This method is a negative test to verify that the methodHandle for the private static method
 * from the parent interface cannot be successfully produced using unreflectSpecial.
 * An IllegalAccessException is expected to be thrown
 * @throws IllegalAccessException
 */
@Test
public static void test_unreflectSpecial_interface_ITest_private_static_method() throws Throwable {
    Class<?> d = Class.forName("ITest", true, loadClass);
    Method method = d.getDeclaredMethod("private_static_method");
    method.setAccessible(true);
    Lookup lookup = getLookupObject(d);
    try {
        MethodHandle mh = lookup.unreflectSpecial(method, d);
        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 18 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class Test_ReflectionAndMethodHandles method test_find_static_child_public_static.

/**
 * This method is used to verify if the method handle of a public static method
 * in our child interface can be successfully looked up using findStatic, and then
 * successfully invoke the method to verify that the correct MethodHandle was found
 */
@Test
public static void test_find_static_child_public_static() throws Throwable {
    Class<?> c = Class.forName("ITestChild", true, loadClass);
    Lookup lookup = getLookupObject(c);
    MethodHandle mh = lookup.findStatic(c, "child_public_static_method", type);
    String returnStatement = (String) mh.invoke();
    AssertJUnit.assertEquals("Failing as public static method of child interface didnt return expected output when invoked", expected_child_public_static, returnStatement);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 19 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class Test_ReflectionAndMethodHandles method test_find_special_child_private_static.

/* --------------- Tests to test findSpecial ---------------------- */
/**
 * 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 findSpecial.
 * An IllegalAccessException is expected to be thrown
 * @throws IllegalAccessException
 */
@Test
public static void test_find_special_child_private_static() throws Exception {
    Class<?> c = Class.forName("ITestChild", true, loadClass);
    Lookup lookup = getLookupObject(c);
    try {
        MethodHandle mh = lookup.findSpecial(c, "child_private_static_method", type, Test_ReflectionAndMethodHandles.class);
        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 20 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class Test_ReflectionAndMethodHandles method test_find_static_child_private_non_static.

/**
 * This method is a negative test to verify that the methodHandle for the private non static
 * method cannot be successfully looked up using findStatic. An IllegalAccessException is expected to be thrown
 * @throws IllegalAccessException
 */
@Test
public static void test_find_static_child_private_non_static() throws Exception {
    Class<?> c = Class.forName("ITestChild", true, loadClass);
    Lookup lookup = getLookupObject(c);
    try {
        MethodHandle mh = lookup.findStatic(c, "child_private_non_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)

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