public static <T> T substituteClassLoader(final @NotNull T remote, final @Nullable ClassLoader classLoader) throws Exception {
    final class MyHandler implements InvocationHandler {
      @Override
      public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
        if ("equals".equals(method.getName())) {
          Object arg = args.length == 1 ? args[0] : null;
          if (arg != null && Proxy.isProxyClass(arg.getClass()) && Proxy.getInvocationHandler(arg) instanceof MyHandler) {
            return arg.equals(remote);
          }
        }
        return executeWithClassLoader(() -> invokeRemote(method, method, remote, args, classLoader, true), classLoader);
      }
    }
    return executeWithClassLoader(() -> {
      Object proxy = Proxy.newProxyInstance(classLoader, remote.getClass().getInterfaces(), new MyHandler());
      return (T)proxy;
    }, classLoader);
  }
public static <T> T substituteClassLoader(final @NotNull T remote, final @Nullable ClassLoader classLoader)