Search in sources :

Example 31 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Public_CrossPackage_SubclassLookup.

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

Example 32 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.

/*private static fields ( cross package )*/
/**
 * Negative test : findStaticGetter, findStaticSetter tests using private static fields of a class belonging
 * to a different package than the lookup class.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Private_Static_CrossPackage() throws Throwable {
    boolean illegalAccessExceptionThrown = false;
    Lookup publicLookup = MethodHandles.lookup();
    try {
        publicLookup.findStaticSetter(PackageExamples.class, "staticPrivateField", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        publicLookup.findStaticGetter(PackageExamples.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 33 with MethodHandles.lookup

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

the class Find_InvokeTracker method test_GetterSetter_Protected_CrossPackage_InnerClass_Level2.

/**
 * Negative test : findSetter, findGetter test using protected fields of an inner classes (level 2 deep)
 * where the lookup class is the top level outer class belonging to a different package.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void test_GetterSetter_Protected_CrossPackage_InnerClass_Level2() throws Throwable {
    Lookup publicLookup = PackageExamples.getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        publicLookup.findSetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticProtectedField_Inner2", int.class);
    } catch (IllegalAccessException e) {
        illegalAccessExceptionThrown = true;
    }
    AssertJUnit.assertTrue(illegalAccessExceptionThrown);
    illegalAccessExceptionThrown = false;
    try {
        publicLookup.findGetter(SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2.class, "nonStaticProtectedField_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 34 with MethodHandles.lookup

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

the class Unreflect_InvokeTracker method test_Unreflect_Protected_InnerClass_Level1_CrossPackage.

/**
 * Negative test : unreflect a protected method from a level 1 protected inner class using a top level outer class lookup,
 * where the lookup class is in a different package.
 * Basically we unreflect a protected method from protected inner class package1.C.D where the lookup class used is package2.F.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_Unreflect_Protected_InnerClass_Level1_CrossPackage() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExample$SamePackageInnerClass_Protected");
    Method innerclassMethod_Level1 = clazz.getDeclaredMethod("addProtectedInner", int.class, int.class);
    Lookup outerclassLookup = PackageExamples.getLookup();
    boolean illegalAccessExceptionThrown = false;
    try {
        outerclassLookup.unreflect(innerclassMethod_Level1);
        System.out.println("IllegalAccessException is NOT thrown while trying to unreflect a protected method " + "of a 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 35 with MethodHandles.lookup

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

the class Unreflect_InvokeTracker method test_UnreflectConstructor_SamePackage_SubClass.

/**
 *******************************************************************
 *unreflectConstructor tests involving inheritance.
 *Combinations : parent / child lookups within the same package
 ********************************************************************
 */
/**
 * unreflect subclass constructor using super class lookup, parent child in same package.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
@SuppressWarnings({ "unchecked", "rawtypes" })
public void test_UnreflectConstructor_SamePackage_SubClass() throws Throwable {
    Class clazz = Class.forName("com.ibm.j9.jsr292.SamePackageExampleSubclass");
    Constructor c = clazz.getDeclaredConstructor(int.class, int.class);
    Lookup parentLookup = SamePackageExample.getLookup();
    MethodHandle mh = parentLookup.unreflectConstructor(c);
    SamePackageExampleSubclass g = (SamePackageExampleSubclass) mh.invoke(1, 2);
    AssertJUnit.assertEquals(g.nonStaticPublicField_Child, 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)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