Search in sources :

Example 41 with MethodHandle

use of java.lang.invoke.MethodHandle in project invokebinder by headius.

the class SmartBinderTest method testPrepend.

@Test
public void testPrepend() throws Throwable {
    MethodHandle target = Subjects.concatHandle();
    SmartHandle handle = SmartBinder.from(String.class, new String[] { "arg1", "arg2" }, Object.class, String.class).prepend("arg0", "Hello, ").drop("arg1").invoke(target);
    assertEquals(MethodType.methodType(String.class, Object.class, String.class), handle.signature().type());
    assertEquals("Hello, world", (String) handle.handle().invokeExact(new Object(), "world"));
    MethodHandle target2 = Subjects.concatHandle();
    SmartHandle handle2 = SmartBinder.from(String.class, new String[] { "arg1", "arg2" }, Object.class, String.class).prepend("arg0", String.class, "Hello, ").drop("arg1").invoke(target2);
    assertEquals(MethodType.methodType(String.class, Object.class, String.class), handle2.signature().type());
    assertEquals("Hello, world", (String) handle2.handle().invokeExact(new Object(), "world"));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 42 with MethodHandle

use of java.lang.invoke.MethodHandle in project invokebinder by headius.

the class SmartBinderTest method testCast.

@Test
public void testCast() throws Throwable {
    MethodHandle target = LOOKUP.unreflect(String.class.getMethod("split", String.class));
    // cast for virtual call using full static signature
    SmartHandle handle = SmartBinder.from(Object.class, new String[] { "this", "regex" }, Object.class, Object.class).cast(String[].class, String.class, String.class).invoke(target);
    assertArrayEquals(new String[] { "foo", "bar" }, (String[]) (Object) handle.handle().invokeExact((Object) "foo,bar", (Object) ","));
    // cast for virtual call using ret, this, args
    handle = SmartBinder.from(new Signature(Object.class, Object.class, new Class[] { Object.class }, "this", "regex")).castVirtual(String[].class, String.class, new Class[] { String.class }).invoke(target);
    assertArrayEquals(new String[] { "foo", "bar" }, (String[]) (Object) handle.handle().invokeExact((Object) "foo,bar", (Object) ","));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 43 with MethodHandle

use of java.lang.invoke.MethodHandle in project invokebinder by headius.

the class BinderTest method testFold.

@Test
public void testFold() throws Throwable {
    MethodHandle target = Subjects.concatHandle();
    MethodHandle fold = Binder.from(String.class, String.class).drop(0).constant("yahoo");
    MethodHandle handle = Binder.from(String.class, String.class).fold(fold).invoke(target);
    assertEquals(MethodType.methodType(String.class, String.class), handle.type());
    assertEquals("yahoofoo", (String) handle.invokeExact("foo"));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 44 with MethodHandle

use of java.lang.invoke.MethodHandle in project invokebinder by headius.

the class BinderTest method testDropAll.

@Test
public void testDropAll() throws Throwable {
    MethodHandle target = Subjects.concatHandle();
    MethodHandle handle = Binder.from(String.class, String.class, Object.class).dropAll().insert(0, "Hello, ", "world").invoke(target);
    assertEquals(MethodType.methodType(String.class, String.class, Object.class), handle.type());
    assertEquals("Hello, world", (String) handle.invokeExact("Hello, ", new Object()));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 45 with MethodHandle

use of java.lang.invoke.MethodHandle in project invokebinder by headius.

the class BinderTest method testInvokeConstructor2.

@Test
public void testInvokeConstructor2() throws Throwable {
    MethodHandle handle = Binder.from(Constructable.class, String.class, String.class).invokeConstructorQuiet(LOOKUP, Constructable.class);
    assertEquals(MethodType.methodType(Constructable.class, String.class, String.class), handle.type());
    assertEquals(new Constructable("foo", "bar"), (Constructable) handle.invokeExact("foo", "bar"));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)302 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)44 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)13 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6