Search in sources :

Example 46 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_SamePackage_InnerClasses_Parallel_Level2.

/**
 * findSetter, findGetter test using fields of a second level inner class
 * where the lookup class is another second level inner class of the same outer class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_SamePackage_InnerClasses_Parallel_Level2() throws Throwable {
    SamePackageExample spe = new SamePackageExample();
    SamePackageInnerClass spe_inner1 = spe.new SamePackageInnerClass();
    SamePackageInnerClass_Nested_Level2 spe_inner2 = spe_inner1.new SamePackageInnerClass_Nested_Level2();
    Lookup publicLookup = spe_inner2.getLookup();
    MethodHandle mhSetter = publicLookup.findSetter(SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2.class, "nonStaticPublicField_Inner22", int.class);
    MethodHandle mhGetter = publicLookup.findGetter(SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2.class, "nonStaticPublicField_Inner22", int.class);
    SamePackageExample.SamePackageInnerClass2.SamePackageInnerClass2_Nested_Level2 g = ((new SamePackageExample()).new SamePackageInnerClass2()).new SamePackageInnerClass2_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(com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 47 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_SamePackage_SubclassLookup.

/**
 * Negative test : findSetter, findGetter tests using a sub-class Lookup and super-class private fields where both super and sub
 * classes belong to the same package.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_SamePackage_SubclassLookup() throws Throwable {
    Lookup subclassLookup = SamePackageExampleSubclass.getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        subclassLookup.findSetter(SamePackageExample.class, "nonStaticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    /*findSetter access to private field of parent should be given to child*/
    AssertJUnit.assertFalse(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        subclassLookup.findGetter(SamePackageExample.class, "nonStaticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    /*findSetter access to private field of parent should be given to child*/
    AssertJUnit.assertFalse(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 48 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_Static_SamePackage.

/*private static fields ( same package )*/
/**
 * Negative test : findGetter, findSetter test using a Lookup whose lookup class is LookupAPITests_find(this class)
 * and lookup fields are private static fields belonging to a different class under the same package as the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_Static_SamePackage() throws Throwable {
    Lookup publicLookup = MethodHandles.lookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        publicLookup.findSetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        publicLookup.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 {
        publicLookup.findStaticSetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        publicLookup.findStaticGetter(SamePackageExample.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 49 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_CrossPackage_SuperclassLookup.

/**
 * findSetter, findGetter tests using a super-class as lookup class and public fields
 * belonging to a sub-class under a different package than the lookup ( super ) class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_CrossPackage_SuperclassLookup() throws Throwable {
    Lookup superclassLookup = PackageExamples.getLookup();
    MethodHandle mhSetter = superclassLookup.findSetter(CrossPackageExampleSubclass.class, "nonStaticPublicField_Child", int.class);
    MethodHandle mhGetter = superclassLookup.findGetter(CrossPackageExampleSubclass.class, "nonStaticPublicField_Child", int.class);
    CrossPackageExampleSubclass g = new CrossPackageExampleSubclass();
    mhSetter.invokeExact(g, 5);
    int o = (int) mhGetter.invoke(g);
    AssertJUnit.assertEquals(o, 5);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 50 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_Static_SamePackage_SuperClassLookup.

/**
 * Negative test : findGetter, findSetter test using a Lookup whose lookup class is a super-class and
 * and lookup fields are private static fields belonging to a subclass 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_SuperClassLookup() throws Throwable {
    Lookup superclassLookup = SamePackageExample.getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        superclassLookup.findSetter(SamePackageExampleSubclass.class, "staticPrivateField_Child", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        superclassLookup.findGetter(SamePackageExampleSubclass.class, "staticPrivateField_Child", 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 {
        superclassLookup.findStaticSetter(SamePackageExampleSubclass.class, "staticPrivateField_Child", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        superclassLookup.findStaticGetter(SamePackageExampleSubclass.class, "staticPrivateField_Child", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) 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