use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testGetField2.
@Test
public void testGetField2() throws Throwable {
Fields fields = new Fields();
MethodHandle handle = Binder.from(String.class, Fields.class).getFieldQuiet(LOOKUP, "instanceField");
assertEquals(MethodType.methodType(String.class, Fields.class), handle.type());
assertEquals("initial", (String) handle.invokeExact(fields));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class BinderTest method testFoldStatic.
@Test
public void testFoldStatic() throws Throwable {
MethodHandle target = Subjects.concatHandle();
MethodHandle handle = Binder.from(LOOKUP, String.class, String.class).foldStatic(BinderTest.class, "alwaysYahooStatic").invoke(target);
assertEquals(MethodType.methodType(String.class, String.class), handle.type());
assertEquals("yahoofoo", (String) handle.invokeExact("foo"));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class SmartBinderTest method testPrintSignature.
@Test
public void testPrintSignature() throws Throwable {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
MethodHandle handle = SmartBinder.from(Subjects.StringIntegerIntegerInteger).foldStatic("x", Subjects.class, "foldStringIntegerIntegerInteger").drop("a").printSignature(ps).drop("b1").drop("b2").drop("b3").printSignature(ps).invokeStatic(MethodHandles.lookup(), Subjects.class, "upperCase").handle();
String result = baos.toString();
assertEquals("(String x, Integer b1, Integer b2, Integer b3)String\n" + "(String x)String\n", result);
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class SmartBinderTest method testAppend.
@Test
public void testAppend() throws Throwable {
MethodHandle target = Subjects.concatHandle();
SmartHandle handle = SmartBinder.from(String.class, new String[] { "arg0", "arg1" }, String.class, Object.class).append("arg2", "world").drop("arg1").invoke(target);
assertEquals(MethodType.methodType(String.class, String.class, Object.class), handle.signature().type());
assertEquals("Hello, world", (String) handle.handle().invokeExact("Hello, ", new Object()));
MethodHandle target2 = Subjects.concatCharSequenceHandle();
SmartHandle handle2 = SmartBinder.from(String.class, new String[] { "arg0", "arg1" }, String.class, Object.class).append("arg2", CharSequence.class, "world").drop("arg1").invoke(target2);
assertEquals(MethodType.methodType(String.class, String.class, Object.class), handle2.signature().type());
assertEquals("Hello, world", (String) handle2.handle().invokeExact("Hello, ", new Object()));
}
use of java.lang.invoke.MethodHandle in project invokebinder by headius.
the class SmartBinderTest method testFoldStatic.
@Test
public void testFoldStatic() throws Throwable {
MethodHandle handle = SmartBinder.from(Subjects.StringIntegerIntegerInteger).foldStatic("x", Subjects.class, "foldStringIntegerIntegerInteger").drop("a").drop("b1").drop("b2").drop("b3").invokeStatic(MethodHandles.lookup(), Subjects.class, "upperCase").handle();
assertEquals("FORTY_TWO", (String) handle.invokeExact("foo", Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)));
}
Aggregations