Search in sources :

Example 76 with MethodType

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

the class InvokeGenericTest method testReferenceConversions.

@Test
public void testReferenceConversions() throws Throwable {
    startTest("testReferenceConversions");
    toString_MH = LOOKUP.findVirtual(Object.class, "toString", MethodType.methodType(String.class));
    Object[] args = { "one", "two" };
    for (MethodType type : allMethodTypes(2, Object.class, String.class, CharSequence.class)) {
        testReferenceConversions(type, args);
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 77 with MethodType

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

the class InvokeGenericTest method changeArgTypes.

static MethodHandle changeArgTypes(MethodHandle target, int beg, int end, Class<?> argType) {
    MethodType targetType = target.type();
    end = Math.min(end, targetType.parameterCount());
    ArrayList<Class<?>> argTypes = new ArrayList<>(targetType.parameterList());
    Collections.fill(argTypes.subList(beg, end), argType);
    MethodType ttype2 = MethodType.methodType(targetType.returnType(), argTypes);
    return target.asType(ttype2);
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 78 with MethodType

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

the class JavaDocExamplesTest method testPermuteArguments.

@Test
public void testPermuteArguments() throws Throwable {
    {
        {
            /// JAVADOC
            {
            }
            MethodType intfn1 = methodType(int.class, int.class);
            MethodType intfn2 = methodType(int.class, int.class, int.class);
            // ... {int x, int y => x-y} ...;
            MethodHandle sub = SUB_2;
            assert (sub.type().equals(intfn2));
            MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1);
            MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0);
            assert ((int) rsub.invokeExact(1, 100) == 99);
            // ... {int x, int y => x+y} ...;
            MethodHandle add = ADD_2;
            assert (add.type().equals(intfn2));
            MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
            assert (twice.type().equals(intfn1));
            assert ((int) twice.invokeExact(21) == 42);
        }
    }
    {
        {
            /// JAVADOC
            {
            }
            MethodHandle cat = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
            assertEquals("xy", (String) cat.invokeExact("x", "y"));
            MethodHandle d0 = dropArguments(cat, 0, String.class);
            assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
            MethodHandle d1 = dropArguments(cat, 1, String.class);
            assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
            MethodHandle d2 = dropArguments(cat, 2, String.class);
            assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
            MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
            assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
        }
    }
}
Also used : MethodType(java.lang.invoke.MethodType)

Example 79 with MethodType

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

the class JavaDocExamplesTest method testMethodHandlesSummary.

@Test
public void testMethodHandlesSummary() throws Throwable {
    {
        {
            /// JAVADOC
            {
            }
            Object x, y;
            String s;
            int i;
            MethodType mt;
            MethodHandle mh;
            MethodHandles.Lookup lookup = MethodHandles.lookup();
            // mt is (char,char)String
            mt = MethodType.methodType(String.class, char.class, char.class);
            mh = lookup.findVirtual(String.class, "replace", mt);
            s = (String) mh.invokeExact("daddy", 'd', 'n');
            // invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
            assertEquals(s, "nanny");
            // weakly typed invocation (using MHs.invoke)
            s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
            assertEquals(s, "savvy");
            // mt is (Object[])List
            mt = MethodType.methodType(java.util.List.class, Object[].class);
            mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
            assert (mh.isVarargsCollector());
            x = mh.invoke("one", "two");
            // invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
            assertEquals(x, java.util.Arrays.asList("one", "two"));
            // mt is (Object,Object,Object)Object
            mt = MethodType.genericMethodType(3);
            mh = mh.asType(mt);
            x = mh.invokeExact((Object) 1, (Object) 2, (Object) 3);
            // invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
            assertEquals(x, java.util.Arrays.asList(1, 2, 3));
            // mt is ()int
            mt = MethodType.methodType(int.class);
            mh = lookup.findVirtual(java.util.List.class, "size", mt);
            i = (int) mh.invokeExact(java.util.Arrays.asList(1, 2, 3));
            // invokeExact(Ljava/util/List;)I
            assert (i == 3);
            mt = MethodType.methodType(void.class, String.class);
            mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
            mh.invokeExact(System.out, "Hello, world.");
            // invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
            {
            }
        }
    }
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandles(java.lang.invoke.MethodHandles)

Example 80 with MethodType

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

the class BigArityTest method exactInvoker02.

@Test
public void exactInvoker02() {
    for (int i = 0; i < 255; i++) {
        MethodType mt = MethodType.genericMethodType(i);
        MethodType expMT = mt.insertParameterTypes(0, MethodHandle.class);
        if (i < 254) {
            assertEquals(expMT, MethodHandles.exactInvoker(mt).type());
        } else {
            try {
                MethodHandles.exactInvoker(mt);
                assert (false) : i;
            } catch (IllegalArgumentException ex) {
                System.out.println("OK: " + ex);
            }
        }
    }
}
Also used : MethodType(java.lang.invoke.MethodType) Test(org.junit.Test)

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