code: private static void setField(Class<?> expectedType, Object value, boolean saveOriginal) throws IllegalStateException {
          try {
              for (Field field : NamingManager.class.getDeclaredFields()) {
                  if (expectedType.equals(field.getType())) {
                      field.setAccessible(true);
                      if (saveOriginal) {
                          if (expectedType.equals(InitialContextFactoryBuilder.class)) {
                              originalICFBuilder = (InitialContextFactoryBuilder) field.get(null);
                          } else {
                              originalOFBuilder = (ObjectFactoryBuilder) field.get(null);
                          }
                      }
  
                      field.set(null, value);
                  }
              }
          } catch (Throwable t) {
              // Ignore
              LOGGER.debug("Error setting field.", t);
              throw new IllegalStateException(t);
          }
      }
docs: /*
  There are no public API to reset the InitialContextFactoryBuilder or
  ObjectFactoryBuilder on the NamingManager so try to use reflection.