Search in sources :

Example 41 with Lookup

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

the class Find_InvokeTracker method test_FindVirtual_Public_CrossPackage_Overridden_InnerClass_Nested_Level2.

/**
 * findVirtual test using a public overridden method belonging to a second level inner class
 * which is a child of a first level inner class under the same outer class. The lookup class
 * is a first level inner class in a different package.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_FindVirtual_Public_CrossPackage_Overridden_InnerClass_Nested_Level2() throws Throwable {
    PackageExamples pe = new PackageExamples();
    CrossPackageInnerClass cp_inner1 = pe.new CrossPackageInnerClass();
    Lookup publicLookup = cp_inner1.getLookup();
    MethodHandle example = publicLookup.findVirtual(SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2_SubOf_Inner1.class, "addPublicInner", MethodType.methodType(int.class, int.class, int.class));
    SamePackageExample.SamePackageInnerClass g = ((new SamePackageExample()).new SamePackageInnerClass2()).new SamePackageInnerClass2_Nested_Level2_SubOf_Inner1();
    int s = (int) example.invoke(g, 1, 2);
    AssertJUnit.assertEquals(23, s);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) SamePackageInnerClass(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass) PackageExamples(examples.PackageExamples) CrossPackageInnerClass(examples.PackageExamples.CrossPackageInnerClass) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 42 with Lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_Static_CrossPackage_SuperclassLookup.

/**
 * findStaticGetter, findStaticSetter tests using public static fields of a sub-class belonging to a different package
 * than the lookup class ( the super-class of the class containing the fields being looked up ).
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_Static_CrossPackage_SuperclassLookup() throws Throwable {
    Lookup superclassLookup = PackageExamples.getLookup();
    MethodHandle mhSetter = superclassLookup.findStaticSetter(CrossPackageExampleSubclass.class, "staticPublicField_Child", int.class);
    MethodHandle mhGetter = superclassLookup.findStaticGetter(CrossPackageExampleSubclass.class, "staticPublicField_Child", 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 43 with Lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_Static_SamePackage_SubClassLookup.

/**
 * Negative test : findGetter, findSetter test using a Lookup whose lookup class is a sub-class and
 * and lookup fields are private static fields belonging to the superclass of the lookup class under the same
 * package as the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_Static_SamePackage_SubClassLookup() throws Throwable {
    Lookup sublassLookup = SamePackageExampleSubclass.getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        sublassLookup.findSetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        sublassLookup.findGetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    // make sure we get access exception when trying to access static field with findGetter/findSetter
    if (illegalAccessExceptionThrown = false) {
        Assert.fail("Illegal access to static private field given to findSetter/findGetter");
    }
    illegalAccessExceptionThrown = false;
    try {
        sublassLookup.findStaticSetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    /*static setter access to private static fields of the parent 
		should be given to the child*/
    AssertJUnit.assertFalse(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        sublassLookup.findStaticGetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    /*static getter access to private static fields of the parent 
		should be given to the child*/
    AssertJUnit.assertFalse(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 44 with Lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_SamePackage_InnerClass_Level1.

/**
 * findSetter, findGetter test using fields of an inner classes (level 1 deep) where the
 * lookup class is the top outer class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_SamePackage_InnerClass_Level1() throws Throwable {
    Lookup publicLookup = MethodHandles.lookup();
    MethodHandle mhSetter = publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.class, "nonStaticPublicField_Inner1", int.class);
    MethodHandle mhGetter = publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.class, "nonStaticPublicField_Inner1", int.class);
    SamePackageExample.SamePackageInnerClass g = (new SamePackageExample()).new SamePackageInnerClass();
    mhSetter.invokeExact(g, 5);
    int o = (int) mhGetter.invokeExact(g);
    AssertJUnit.assertEquals(o, 5);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) SamePackageInnerClass(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass) SamePackageInnerClass(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 45 with Lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_Static_SamePackage.

/**
 * Negative test : findSetter, findGetter test using a Lookup whose lookup class is LookupAPITests_find (this class)
 * and lookup fields are public static fields belonging to a different class that is under the same package as
 * the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_Static_SamePackage() throws Throwable {
    Lookup publicLookup = MethodHandles.lookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        publicLookup.findSetter(SamePackageExample.class, "staticPublicField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    // make sure we get access exception when trying to access a static field with findGetter/findSetter
    if (illegalAccessExceptionThrown == false) {
        Assert.fail("Illegal access to static public field given to findSetter/findGetter");
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) 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