@CanIgnoreReturnValue
  public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
    Objects.requireNonNull(baseType);
    $Gson$Preconditions.checkArgument(
        typeAdapter instanceof JsonSerializer<?>
            || typeAdapter instanceof JsonDeserializer<?>
            || typeAdapter instanceof TypeAdapter<?>);

    if (JsonElement.class.isAssignableFrom(baseType)) {
      throw new IllegalArgumentException("Cannot override built-in adapter for " + baseType);
    }

    if (typeAdapter instanceof JsonDeserializer || typeAdapter instanceof JsonSerializer) {
      hierarchyFactories.add(TreeTypeAdapter.newTypeHierarchyFactory(baseType, typeAdapter));
    }
    if (typeAdapter instanceof TypeAdapter<?>) {
      @SuppressWarnings({"unchecked", "rawtypes"})
      TypeAdapterFactory factory =
          TypeAdapters.newTypeHierarchyFactory(baseType, (TypeAdapter) typeAdapter);
      factories.add(factory);
    }
    return this;
  }
@Test
  public void testRegisterTypeHierarchyAdapterJsonElements() {
    final String ERROR_MESSAGE = "Cannot override built-in adapter for ";
    Class<?>[] types = {
      JsonElement.class, JsonArray.class,
    };
    GsonBuilder gsonBuilder = new GsonBuilder();
    for (Class<?> type : types) {
      IllegalArgumentException e =
          assertThrows(
              IllegalArgumentException.class,
              () -> gsonBuilder.registerTypeHierarchyAdapter(type, NULL_TYPE_ADAPTER));

      assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type);
    }
    // But registering type hierarchy adapter for Object should be allowed
    gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER);
  }