Search in sources :

Example 81 with Lookup

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

the class Find_InvokeTracker method test_StaticGetterSetter_Public_Static_SamePackage_SuperclassLookup.

/**
 * findStaticGetter, findStaticSetter test using a Lookup whose lookup class is a super-class
 * and lookup fields are public static fields belonging to a subclass of the super-class under the same package as
 * the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_StaticGetterSetter_Public_Static_SamePackage_SuperclassLookup() throws Throwable {
    Lookup superclassLookup = SamePackageExample.getLookup();
    /*
		 * CMVC: 190581 
		 * 
		 * Works in J9 but fails in Oracle.
		 * Error summary: The following line of code throws java.lang.InternalError: uncaught exception in Oracle, "Caused by: java.lang.NoSuchFieldException: staticPublicField"
		 * Note: The 'staticPublicField' belongs to the parent of SamePackageExampleSubclass.   
		 **/
    MethodHandle mhSetter = superclassLookup.findStaticSetter(SamePackageExampleSubclass.class, "staticPublicField", int.class);
    MethodHandle mhGetter = superclassLookup.findStaticGetter(SamePackageExampleSubclass.class, "staticPublicField", int.class);
    mhSetter.invokeExact(5);
    int o = (int) mhGetter.invokeExact();
    AssertJUnit.assertEquals(o, 5);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 82 with Lookup

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

the class Find_InvokeTracker method test_GetterSetter_Protected_CrossPackage_SuperclassLookup.

/**
 * Negative test : findSetter, findGetter tests involving protected fields of a sub-class belonging to a different package than the Lookup ( super ) class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Protected_CrossPackage_SuperclassLookup() throws Throwable {
    Lookup superclassLookup = PackageExamples.getLookup();
    try {
        superclassLookup.findSetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField_Child", int.class);
        Assert.fail("Superclass lookup Shouldn't be able to findSetter a protected field of subclass");
    } catch (IllegalAccessException e) {
    // Pass
    }
    try {
        superclassLookup.findGetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField_Child", int.class);
        Assert.fail("Superclass lookup Shouldn't be able to findGetter a protected field of subclass");
    } catch (IllegalAccessException e) {
    // Pass
    }
    /* Using superclass protected fields */
    superclassLookup.findSetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField", int.class);
    superclassLookup.findGetter(CrossPackageExampleSubclass.class, "nonStaticProtectedField", int.class);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 83 with Lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_SamePackage_InnerClass_Level2.

/**
 * Negative test : findSetter, findGetter test using private 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_Private_SamePackage_InnerClass_Level2() throws Throwable {
    Lookup publicLookup = MethodHandles.lookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPrivateField_Inner2", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPrivateField_Inner2", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) SamePackageInnerClass(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass) Test(org.testng.annotations.Test)

Example 84 with Lookup

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

the class Unreflect_InvokeTracker method test_Unreflect_Public_InnerClass_Level1.

/**
 ***************************************************************************************************
 * unreflect tests involving inner classes.
 * Combination : Different levels of inner/outer class public/protected methods unreflected using
 *               different levels of outer/inner public/protected classes, same package/cross package.
 *****************************************************************************************************
 *
 *	/**
 * unreflect a public method from a level 1 inner class using the top level outer class lookup.
 * Basically we unreflect a method from class C.D where the lookup class used is C.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Public_InnerClass_Level1() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample$SamePackageInnerClass");
    Method innerclassMethod_Level1 = clazz.getDeclaredMethod("addPublicInner", int.class, int.class);
    Lookup outerclassLookup = SamePackageExample.getLookup();
    MethodHandle mh = outerclassLookup.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 85 with Lookup

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

the class Unreflect_InvokeTracker method test_UnreflectConstructor_SamePackage_SuperClass.

/**
 * unreflect a super class constructor using a subclass lookup, parent child in same package
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_UnreflectConstructor_SamePackage_SuperClass() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample");
    Constructor c = clazz.getDeclaredConstructor(int.class, int.class);
    Lookup childLookup = SamePackageExampleSubclass.getLookup();
    MethodHandle mh = childLookup.unreflectConstructor(c);
    SamePackageExample g = (SamePackageExample) mh.invoke(1, 2);
    AssertJUnit.assertEquals(g.nonStaticPublicField, 3);
}
Also used : Constructor(java.lang.reflect.Constructor) Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

Lookup (java.lang.invoke.MethodHandles.Lookup)216 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)36 SamePackageInnerClass (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass)17 MutableCallSite (java.lang.invoke.MutableCallSite)11 MethodHandles (java.lang.invoke.MethodHandles)9 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