CompileContext (CompileContext context, Collection<ModuleBuildTarget> moduleTargets) { final Class<MessageHandler> messageHandlerInterface = MessageHandler.class; return ReflectionUtil.proxy(context.getClass().getClassLoader(), CompileContext.class, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (args != null && args.length > 0 && messageHandlerInterface.equals(method.getDeclaringClass())) { for (Object arg : args) { if (arg instanceof CompilerMessage) { final CompilerMessage compilerMessage = (CompilerMessage)arg; for (ModuleBuildTarget target : moduleTargets) { compilerMessage.addModuleName(target.getModule().getName()); } break; } } } final MethodHandle mh = MethodHandles.lookup().unreflect(method); return args == null? mh.invoke(context) : mh.bindTo(context).asSpreader(Object[].class, args.length).invoke(args); } }); }