private static @NotNull RemoteCallResult getRemoteCallResult(@NotNull Session session, Object result) {
    if (isPassByValue(result)) {
      // does not need a session, pass by value
      return new RemoteCallResult(result);
    }

    Ref ref = putAdhocReference(result, session);

    var stream =
      result instanceof Collection<?> collection ? collection.stream() :
      result.getClass().isArray() ? Arrays.stream((Object[])result) :
      null;

    if (stream == null) {
      return new RemoteCallResult(ref);
    }

    List<Ref> items = stream.map(item -> putAdhocReference(item, session)).toList();
    return new RemoteCallResult(new RefList(ref.id(), result.getClass().getName(), items));
  }
private static @NotNull RemoteCallResult getRemoteCallResult(@NotNull Session session, Object result)