use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testFrom.
@Test
public void testFrom() throws Throwable {
MethodHandle target = Subjects.concatHandle();
Binder binder1 = Binder.from(String.class, String.class, Object.class).drop(1);
MethodHandle handle = Binder.from(binder1).insert(1, "world").invoke(target);
assertEquals(MethodType.methodType(String.class, String.class, Object.class), handle.type());
assertEquals("Hello, world", (String) handle.invokeExact("Hello, ", new Object()));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testAppend.
@Test
public void testAppend() throws Throwable {
MethodHandle target = Subjects.concatHandle();
MethodHandle handle = Binder.from(String.class, String.class, Object.class).append("world").drop(1).invoke(target);
assertEquals(MethodType.methodType(String.class, String.class, Object.class), handle.type());
assertEquals("Hello, world", (String) handle.invokeExact("Hello, ", new Object()));
MethodHandle target2 = Subjects.concatCharSequenceHandle();
MethodHandle handle2 = Binder.from(String.class, String.class, Object.class).append(CharSequence.class, "world").drop(1).invoke(target2);
assertEquals(MethodType.methodType(String.class, String.class, Object.class), handle2.type());
assertEquals("Hello, world", (String) handle2.invokeExact("Hello, ", new Object()));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testSetField2.
@Test
public void testSetField2() throws Throwable {
Fields fields = new Fields();
MethodHandle handle = Binder.from(void.class, Fields.class, String.class).setFieldQuiet(LOOKUP, "instanceField");
assertEquals(MethodType.methodType(void.class, Fields.class, String.class), handle.type());
handle.invokeExact(fields, "modified");
assertEquals("modified", fields.instanceField);
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testTryFinally.
@Test
public void testTryFinally() throws Throwable {
MethodHandle post = Binder.from(void.class, String[].class).invokeStatic(LOOKUP, BinderTest.class, "finallyLogic");
MethodHandle handle = Binder.from(void.class, String[].class).tryFinally(post).invokeStatic(LOOKUP, BinderTest.class, "setZeroToFoo");
assertEquals(MethodType.methodType(void.class, String[].class), handle.type());
String[] stringAry = new String[1];
handle.invokeExact(stringAry);
assertEquals("foofinally", stringAry[0]);
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testTryFinallyReturn.
@Test
public void testTryFinallyReturn() throws Throwable {
MethodHandle post = Binder.from(void.class, String[].class).invokeStatic(LOOKUP, BinderTest.class, "finallyLogic");
MethodHandle handle = Binder.from(int.class, String[].class).tryFinally(post).invokeStatic(LOOKUP, BinderTest.class, "setZeroToFooReturnInt");
assertEquals(MethodType.methodType(int.class, String[].class), handle.type());
String[] stringAry = new String[1];
assertEquals(1, (int) handle.invokeExact(stringAry));
assertEquals("foofinally", stringAry[0]);
}
Aggregations