Source:
public ClientResponseHandler generateErrorHandlers(@NonNull final MethodHandles.Lookup caller, @NonNull final Object o) { Arrays.stream(o.getClass().getDeclaredMethods()) .filter(method -> method.isAnnotationPresent(ServerErrorsHandler.class)) .forEach(method -> { ServerErrorsHandler handler = method.getAnnotation(ServerErrorsHandler.class); if (!method.getParameterTypes()[0] .isAssignableFrom(ResponseMsg.class)) { throw new RuntimeException("Incorrect message type, expected " + ResponseMsg.class + " but provided " + method.getParameterTypes()[0]); } if (errorHandlerMap.containsKey(handler.type())) { throw new RuntimeException("Handler for " + handler.type() + " already registered!"); } try { if (Modifier.isStatic(method.getModifiers())) { MethodHandle mh = caller.unreflect(method); MethodType mt = mh.type().changeParameterType(0, ResponseMsg.class); errorHandlerMap.put(handler.type(), (Handler) LambdaMetafactory .metafactory(caller, "handle", MethodType.methodType(Handler.class), mt, mh, mh.type()) .getTarget().invokeExact()); } else { MethodType mt = MethodType .methodType(method.getReturnType(), method.getParameterTypes()); MethodHandle mh = caller.findVirtual(o.getClass(), method.getName(), mt); MethodType mtGeneric = mh.type() .changeParameterType(1, ResponseMsg.class) .changeReturnType(Object.class); errorHandlerMap.put(handler.type(), (Handler) LambdaMetafactory .metafactory(caller, "handle", MethodType.methodType(Handler.class, o.getClass()), mtGeneric.dropParameterTypes(0, 1), mh, mh.type() .dropParameterTypes(0, 1)) .getTarget().bindTo(o).invoke()); } } catch (Throwable e) { log.error("Exception during incoming message handling", e); throw new RuntimeException(e); } }); return this; }

Target:
public ClientResponseHandler generateErrorHandlers(@NonNull final MethodHandles.Lookup caller, @NonNull final Object o) { Arrays.stream(o.getClass().getDeclaredMethods()) .filter(method -> method.isAnnotationPresent(ServerErrorsHandler.class)) .forEach(method -> { ServerErrorsHandler handler = method.getAnnotation(ServerErrorsHandler.class); if (!method.getParameterTypes()[0] .isAssignableFrom(ResponseMsg.class)) { throw new RuntimeException("Incorrect message type, expected " + ResponseMsg.class + " but provided " + method.getParameterTypes()[0]); } if (errorHandlerMap.containsKey(handler.type())) { throw new RuntimeException("Handler for " + handler.type() + " already registered!"); } try { if (Modifier.isStatic(method.getModifiers())) { MethodHandle mh = caller.unreflect(method); errorHandlerMap.put(handler.type(), (Handler) LambdaMetafactory .metafactory(caller, "handle", MethodType.methodType(Handler.class), mh.type(), mh, mh.type()) .getTarget().invokeExact()); } else { MethodType mt = MethodType .methodType(method.getReturnType(), method.getParameterTypes()); MethodHandle mh = caller.findVirtual(o.getClass(), method.getName(), mt); MethodType mtGeneric = mh.type() .changeReturnType(Object.class); errorHandlerMap.put(handler.type(), (Handler) LambdaMetafactory .metafactory(caller, "handle", MethodType.methodType(Handler.class, o.getClass()), mtGeneric.dropParameterTypes(0, 1), mh, mh.type() .dropParameterTypes(0, 1)) .getTarget().bindTo(o).invoke()); } } catch (Throwable e) { log.error("Exception during incoming message handling", e); throw new RuntimeException(e); } }); return this; }