Search in sources :

Example 76 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_Static_CrossPackage_SuperclassLookup.

/**
 * Negative test : findStaticGetter, findStaticSetter tests using private static fields of a sub-class belonging
 * to a different package than the lookup class ( which is the super-class of the class containing the fields being looked up.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_Static_CrossPackage_SuperclassLookup() throws Throwable {
    boolean illegalAccessExceptionThrown = false;
    Lookup superclassLookup = PackageExamples.getLookup();
    try {
        superclassLookup.findStaticSetter(CrossPackageExampleSubclass.class, "staticPrivateField_Child", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        superclassLookup.findStaticGetter(CrossPackageExampleSubclass.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)

Example 77 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Private_CrossPackage_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 belonging to a different package.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_CrossPackage_InnerClass_Level2() throws Throwable {
    Lookup publicLookup = PackageExamples.getLookup();
    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 78 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_Static_SamePackage_SubclassLookup.

/**
 * Negative test : findSetter, findGetter test using a Lookup whose lookup class is a sub-class
 * and lookup fields are public static fields belonging to the super-class of the lookup class under the same package as
 * the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_Static_SamePackage_SubclassLookup() throws Throwable {
    Lookup subclassLookup = SamePackageExampleSubclass.getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        subclassLookup.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)

Example 79 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_SamePackage_InnerClasse_Nested_Level12.

/**
 * findSetter, findGetter test using public fields of a second level inner class
 * where the lookup class is the first level inner class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Public_SamePackage_InnerClasse_Nested_Level12() throws Throwable {
    SamePackageExample spe = new SamePackageExample();
    SamePackageInnerClass spe_inner1 = spe.new SamePackageInnerClass();
    Lookup publicLookup = spe_inner1.getLookup();
    MethodHandle mhSetter = publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPublicField_Inner2", int.class);
    MethodHandle mhGetter = publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticPublicField_Inner2", int.class);
    SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2 g = ((new SamePackageExample()).new SamePackageInnerClass()).new SamePackageInnerClass_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_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 80 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Protected_CrossPackage.

/*protected fields ( cross package )*/
/**
 * findSetter, findGetter tests involving protected fields of a class belonging to a different package than the Lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Protected_CrossPackage() throws Throwable {
    Lookup publicLookup = MethodHandles.lookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        publicLookup.findSetter(PackageExamples.class, "nonStaticProtectedField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        publicLookup.findGetter(PackageExamples.class, "nonStaticProtectedField", 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