Search in sources :

Example 26 with MethodHandles.lookup

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

Example 27 with MethodHandles.lookup

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

Example 28 with MethodHandles.lookup

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

Example 29 with MethodHandles.lookup

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

Example 30 with MethodHandles.lookup

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);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) PackageExamples(examples.PackageExamples) 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