Search in sources :

Example 81 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class ExplicitCastArgumentsTest method testVarargsCollector.

/**
     * Tests that MHs.explicitCastArguments does incorrect type checks for
     * VarargsCollector. Bug 8066746.
     *
     * @throws java.lang.Throwable
     */
public static void testVarargsCollector() throws Throwable {
    MethodType mt = MethodType.methodType(String[].class, String[].class);
    MethodHandle mh = MethodHandles.publicLookup().findStatic(THIS_CLASS, "f", mt);
    mh = MethodHandles.explicitCastArguments(mh, MethodType.methodType(Object.class, Object.class));
    mh.invokeWithArguments((Object) (new String[] { "str1", "str2" }));
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 82 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class LambdaReceiver method main.

public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti, A, h, X);
    IA p = (IA) cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
Also used : MethodType(java.lang.invoke.MethodType) LambdaReceiver_A(LambdaReceiver_anotherpkg.LambdaReceiver_A) CallSite(java.lang.invoke.CallSite)

Example 83 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class LambdaReceiverBridge method main.

public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiverBridge.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti, X, h, X, LambdaMetafactory.FLAG_BRIDGES, 1, A);
    IA p = (IA) cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
Also used : MethodType(java.lang.invoke.MethodType) LambdaReceiver_A(LambdaReceiver_anotherpkg.LambdaReceiver_A) CallSite(java.lang.invoke.CallSite)

Example 84 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class PermuteArgsTest method testPermutations.

static void testPermutations(MethodHandle mh) throws Throwable {
    HashSet<String> done = new HashSet<>();
    MethodType mt = mh.type();
    int[] perm = nullPerm(mt.parameterCount());
    final int MARGIN = (perm.length <= 10 ? 2 : 0);
    int testCases0 = testCases;
    for (int j = 0; j <= 1; j++) {
        int maxStart = perm.length - 1;
        if (j != 0)
            maxStart /= 2;
        for (int start = 0; start <= maxStart; start++) {
            int maxOmit = (maxStart - start) / 2;
            if (start != 0)
                maxOmit = 2;
            if (j != 0)
                maxOmit = 1;
            for (int omit = 0; omit <= maxOmit; omit++) {
                int end = perm.length - omit;
                if (end - start >= 2) {
                    //System.out.println("testPermutations"+Arrays.asList(start, end)+(j == 0 ? "" : " (reverse)"));
                    testPermutations(mh, perm, start, end, done);
                }
                omit = jump(omit, (start == 0 && j == 0 ? MARGIN : 0), maxOmit);
            }
            start = jump(start, (j == 0 ? MARGIN : 0), maxStart);
        }
        // do everything in reverse:
        reverse(perm, 0, perm.length);
    }
    switch(perm.length) {
        case 2:
            assert (testCases - testCases0 == 2);
            break;
        case 3:
            assert (testCases - testCases0 == 6);
            break;
        case 4:
            assert (testCases - testCases0 == 24);
            break;
        case 5:
            assert (testCases - testCases0 == 120);
            break;
        case 6:
            assert (testCases - testCases0 > 720 / 3);
            break;
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 85 with MethodType

use of java.lang.invoke.MethodType in project jdk8u_jdk by JetBrains.

the class PermuteArgsTest method testOnePermutation.

static void testOnePermutation(MethodHandle mh, int[] perm, Object[] args) throws Throwable {
    MethodType mt = mh.type();
    MethodType pmt = methodType(mt.returnType(), unpermuteArgs(perm, mt.parameterArray(), Class[].class));
    if (VERBOSE)
        System.out.println(Arrays.toString(perm));
    testCases += 1;
    if (DRY_RUN)
        return;
    Object res = permuteArguments(mh, pmt, perm).invokeWithArguments(unpermuteArgs(perm, args));
    String str = String.valueOf(res);
    if (!Arrays.toString(args).equals(str)) {
        System.out.println(Arrays.toString(perm) + " " + str + " *** WRONG ***");
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Aggregations

MethodType (java.lang.invoke.MethodType)103 MethodHandle (java.lang.invoke.MethodHandle)37 MethodHandles (java.lang.invoke.MethodHandles)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)3 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)3 CallSite (java.lang.invoke.CallSite)3 LambdaReceiver_A (LambdaReceiver_anotherpkg.LambdaReceiver_A)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)2 ConnectorSession (com.facebook.presto.spi.ConnectorSession)2 PrestoException (com.facebook.presto.spi.PrestoException)2 Map (java.util.Map)2 InitializeInvokerException (cn.moyada.dubbo.faker.core.exception.InitializeInvokerException)1 Session (com.facebook.presto.Session)1 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Scope (com.facebook.presto.bytecode.Scope)1