code: public static Object replaceIfNotAtTopLevel(Object o, Supplier<Object> replacement) {
          File currentlyWriting = writing.get();
          if (beingWritten.containsKey(o) || currentlyWriting == null) {
              return o;
          } else {
              LOGGER.log(Level.WARNING, "JENKINS-45892: reference to " + o + " being saved from unexpected " + currentlyWriting, new IllegalStateException());
              return replacement.get();
          }
      }
docs: Provides an XStream replacement for an object unless a call to {@link #write} is currently in progress.
  As per JENKINS-45892 this may be used by any class which expects to be written at top level to an XML file
  but which cannot safely be serialized as a nested object (for example, because it expects some {@code onLoad} hook):
  implement a {@code writeReplace} method delegating to this method.
  The replacement need not be {@link Serializable} since it is only necessary for use from XStream.
  @param o an object ({@code this} from {@code writeReplace})
  @param replacement a supplier of a safely serializable replacement object with a {@code readResolve} method
  @return {@code o}, if {@link #write} is being called on it, else the replacement
  @since 2.74