use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testInvoke.
@Test
public void testInvoke() throws Throwable {
MethodHandle target = Subjects.concatHandle();
MethodHandle handle = Binder.from(String.class, String.class, String.class).invoke(target);
assertEquals(MethodType.methodType(String.class, String.class, String.class), handle.type());
assertEquals("Hello, world", (String) handle.invokeExact("Hello, ", "world"));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testGetStatic.
@Test
public void testGetStatic() throws Throwable {
MethodHandle handle = Binder.from(String.class).getStatic(LOOKUP, Fields.class, "staticField");
assertEquals(MethodType.methodType(String.class), handle.type());
assertEquals("initial", (String) handle.invokeExact());
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testTryFinally2.
@Test
public void testTryFinally2() 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, "setZeroToFooAndRaise");
assertEquals(MethodType.methodType(void.class, String[].class), handle.type());
String[] stringAry = new String[1];
try {
handle.invokeExact(stringAry);
assertTrue("should not have reached here", false);
} catch (BlahException re) {
}
assertEquals("foofinally", stringAry[0]);
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testDropLast.
@Test
public void testDropLast() throws Throwable {
MethodHandle target = Subjects.concatHandle();
MethodHandle handle = Binder.from(String.class, String.class, Object.class).dropLast().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()));
handle = Binder.from(String.class, String.class, Object.class, double.class).dropLast(2).insert(1, "world").invoke(target);
assertEquals(MethodType.methodType(String.class, String.class, Object.class, double.class), handle.type());
assertEquals("Hello, world", (String) handle.invokeExact("Hello, ", new Object(), 1.0));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testInvokeConstructor.
@Test
public void testInvokeConstructor() throws Throwable {
MethodHandle handle = Binder.from(Constructable.class, String.class, String.class).invokeConstructor(LOOKUP, Constructable.class);
assertEquals(MethodType.methodType(Constructable.class, String.class, String.class), handle.type());
assertEquals(new Constructable("foo", "bar"), (Constructable) handle.invokeExact("foo", "bar"));
}
Aggregations