Search in sources :

Example 1 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project jdk8u_jdk by JetBrains.

the class PackageSibling method testFindSpecial.

void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
    countTest(positive);
    // foo/bar => foo
    String methodName = name.substring(1 + name.indexOf('/'));
    MethodType type = MethodType.methodType(ret, params);
    Lookup specialLookup = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller && (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)
            System.out.println("lookup via " + lookup + " of " + defc + " " + name + type);
        if (verbosity >= 5)
            System.out.println("  lookup => " + specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(// this check should happen first
        (!specialAccessOK) ? IllegalAccessException.class : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>")) ? NoSuchMethodException.class : IllegalAccessException.class, noAccess);
        if (verbosity >= 5)
            ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from " + specialCaller.getName() + " to " + defc.getName() + "." + name + "/" + type + " => " + target + (target == null ? "" : target.type()) + (noAccess == null ? "" : " !! " + noAccess));
    if (positive && noAccess != null)
        throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    // negative test failed as expected
    if (!positive)
        return;
    assertEquals(specialCaller, target.type().parameterType(0));
    assertEquals(type, target.type().dropParameterTypes(0, 1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class) specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 2 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project jdk8u_jdk by JetBrains.

the class PackageSibling method testFindSpecial0.

public void testFindSpecial0() throws Throwable {
    if (CAN_SKIP_WORKING)
        return;
    startTest("findSpecial");
    testFindSpecial(SubExample.class, Example.class, void.class, "v0");
    testFindSpecial(SubExample.class, Example.class, void.class, "pkg_v0");
    testFindSpecial(RemoteExample.class, PubExample.class, void.class, "Pub/pro_v0");
    // Do some negative testing:
    for (Lookup lookup : new Lookup[] { PRIVATE, EXAMPLE, PACKAGE, PUBLIC }) {
        testFindSpecial(false, lookup, Object.class, Example.class, void.class, "v0");
        testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "bogus");
        testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", int.class);
        testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "<init>", Void.class);
        testFindSpecial(false, lookup, SubExample.class, Example.class, void.class, "s0");
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 3 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project jdk8u_jdk by JetBrains.

the class PackageSibling method makeLists.

static MethodHandle[] makeLists() {
    ArrayList<MethodHandle> lists = new ArrayList<>();
    MethodHandles.Lookup lookup = IMPL_LOOKUP;
    for (; ; ) {
        int nargs = lists.size();
        MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
        String name = "list";
        MethodHandle list = null;
        try {
            list = lookup.findStatic(ValueConversions.class, name, type);
        } catch (ReflectiveOperationException ex) {
        // break from loop!
        }
        if (list == null)
            break;
        lists.add(list);
    }
    // current number of methods
    assertTrue(lists.size() == 11);
    return lists.toArray(new MethodHandle[0]);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 4 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project jdk8u_jdk by JetBrains.

the class PackageSibling method makeArrays.

static MethodHandle[] makeArrays() {
    ArrayList<MethodHandle> arrays = new ArrayList<>();
    MethodHandles.Lookup lookup = IMPL_LOOKUP;
    for (; ; ) {
        int nargs = arrays.size();
        MethodType type = MethodType.genericMethodType(nargs).changeReturnType(Object[].class);
        String name = "array";
        MethodHandle array = null;
        try {
            array = lookup.findStatic(ValueConversions.class, name, type);
        } catch (ReflectiveOperationException ex) {
        // break from loop!
        }
        if (array == null)
            break;
        arrays.add(array);
    }
    // current number of methods
    assertTrue(arrays.size() == 11);
    return arrays.toArray(new MethodHandle[0]);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 5 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project jdk8u_jdk by JetBrains.

the class PackageSibling method testGetter.

public void testGetter(int testMode) throws Throwable {
    // FIXME: test more lookups than this one
    Lookup lookup = PRIVATE;
    for (Object[] c : HasFields.CASES) {
        boolean positive = (c[1] != Error.class);
        testGetter(positive, lookup, c[0], c[1], testMode);
        if (positive)
            testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
    }
    testGetter(true, lookup, new Object[] { true, System.class, "out", java.io.PrintStream.class }, System.out, testMode);
    for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
        testGetter(false, lookup, new Object[] { (isStaticN != 0), System.class, "bogus", char.class }, null, testMode);
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup)

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