use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class DefBootstrapTests method testNoNullGuardAddWhenCached.
public void testNoNullGuardAddWhenCached() throws Throwable {
DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(MethodHandles.publicLookup(), "add", MethodType.methodType(Object.class, int.class, Object.class), 0, DefBootstrap.BINARY_OPERATOR, 0);
MethodHandle handle = site.dynamicInvoker();
assertEquals(2, (Object) handle.invokeExact(1, (Object) 1));
expectThrows(NullPointerException.class, () -> {
assertNotNull((Object) handle.invokeExact(5, (Object) null));
});
}
use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class DefBootstrapTests method testNullGuardAddWhenCached.
public void testNullGuardAddWhenCached() throws Throwable {
DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(MethodHandles.publicLookup(), "add", MethodType.methodType(Object.class, Object.class, Object.class), 0, DefBootstrap.BINARY_OPERATOR, DefBootstrap.OPERATOR_ALLOWS_NULL);
MethodHandle handle = site.dynamicInvoker();
assertEquals(2, (Object) handle.invokeExact((Object) 1, (Object) 1));
assertEquals("nulltest", (Object) handle.invokeExact((Object) null, (Object) "test"));
}
use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class DefBootstrapTests method testMegamorphic.
/** test that we revert to the megamorphic classvalue cache and that it works as expected */
public void testMegamorphic() throws Throwable {
DefBootstrap.PIC site = (DefBootstrap.PIC) DefBootstrap.bootstrap(MethodHandles.publicLookup(), "size", MethodType.methodType(int.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
// mark megamorphic
site.depth = DefBootstrap.PIC.MAX_DEPTH;
MethodHandle handle = site.dynamicInvoker();
assertEquals(2, (int) handle.invokeExact((Object) Arrays.asList("1", "2")));
assertEquals(1, (int) handle.invokeExact((Object) Collections.singletonMap("a", "b")));
assertEquals(3, (int) handle.invokeExact((Object) Arrays.asList("x", "y", "z")));
assertEquals(2, (int) handle.invokeExact((Object) Arrays.asList("u", "v")));
final HashMap<String, String> map = new HashMap<String, String>();
map.put("x", "y");
map.put("a", "b");
assertEquals(2, (int) handle.invokeExact((Object) map));
final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
Integer.toString((int) handle.invokeExact(new Object()));
});
assertEquals("Unable to find dynamic method [size] with [0] arguments for class [java.lang.Object].", iae.getMessage());
assertTrue("Does not fail inside ClassValue.computeValue()", Arrays.stream(iae.getStackTrace()).anyMatch(e -> {
return e.getMethodName().equals("computeValue") && e.getClassName().startsWith("org.elasticsearch.painless.DefBootstrap$PIC$");
}));
}
use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class DefBootstrapTests method testOneType.
/** calls toString() on integers, twice */
public void testOneType() throws Throwable {
CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(), "toString", MethodType.methodType(String.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
MethodHandle handle = site.dynamicInvoker();
assertDepthEquals(site, 0);
// invoke with integer, needs lookup
assertEquals("5", (String) handle.invokeExact((Object) 5));
assertDepthEquals(site, 1);
// invoked with integer again: should be cached
assertEquals("6", (String) handle.invokeExact((Object) 6));
assertDepthEquals(site, 1);
}
use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class DefBootstrapTests method testTooManyTypes.
public void testTooManyTypes() throws Throwable {
// if this changes, test must be rewritten
assertEquals(5, DefBootstrap.PIC.MAX_DEPTH);
CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(), "toString", MethodType.methodType(String.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
MethodHandle handle = site.dynamicInvoker();
assertDepthEquals(site, 0);
assertEquals("5", (String) handle.invokeExact((Object) 5));
assertDepthEquals(site, 1);
assertEquals("1.5", (String) handle.invokeExact((Object) 1.5f));
assertDepthEquals(site, 2);
assertEquals("6", (String) handle.invokeExact((Object) 6L));
assertDepthEquals(site, 3);
assertEquals("3.2", (String) handle.invokeExact((Object) 3.2d));
assertDepthEquals(site, 4);
assertEquals("foo", (String) handle.invokeExact((Object) "foo"));
assertDepthEquals(site, 5);
assertEquals("c", (String) handle.invokeExact((Object) 'c'));
assertDepthEquals(site, 5);
}
Aggregations