Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-deterministic generation of TypeAdapterFactory #257

Closed
prempalsingh opened this issue Jan 7, 2021 · 2 comments · Fixed by #260
Closed

Non-deterministic generation of TypeAdapterFactory #257

prempalsingh opened this issue Jan 7, 2021 · 2 comments · Fixed by #260

Comments

@prempalsingh
Copy link
Contributor

prempalsingh commented Jan 7, 2021

auto-value-gson version - 1.3.1
kotlin version - 1.4.21

When the project has two AutoValue classes with the same name but in different packages, the generated TypeAdapterFactory is not deterministic which creates a cache miss in Gradle because of different file contents.

Following are the two different generated TypeAdapterFactory with the same source -

import com.example.subs.TrayData;

final class AutoValueGson_HSTypeAdapterFactory extends HSTypeAdapterFactory {
  @Override
  @SuppressWarnings("unchecked")
  public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> rawType = type.getRawType();
    if (TrayData.class.isAssignableFrom(rawType)) {
      return (TypeAdapter<T>) TrayData.typeAdapter(gson);
    } else if (com.example.models.TrayData.class.isAssignableFrom(rawType)) {
      return (TypeAdapter<T>) com.example.models.TrayData.typeAdapter(gson);
    } else {
      return null;
    }  
  }
}
import com.example.models.TrayData;

final class AutoValueGson_HSTypeAdapterFactory extends HSTypeAdapterFactory {
  @Override
  @SuppressWarnings("unchecked")
  public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> rawType = type.getRawType();
    if (TrayData.class.isAssignableFrom(rawType)) {
      return (TypeAdapter<T>) TrayData.typeAdapter(gson);
    } else if (com.example.subs.TrayData.class.isAssignableFrom(rawType)) {
      return (TypeAdapter<T>) com.example.subs.TrayData.typeAdapter(gson);
    } else {
      return null;
    }  
  }
}
@prempalsingh
Copy link
Contributor Author

My hunch is that in AutoValueGsonAdapterFactoryProcessor when sorting the elements, we are not considering the package name so in case of same class name, the final order will depend on the order in which those elements were initially present which wouldn't always be the same(?)

@ZacSweers
Copy link
Collaborator

Good find. Apologies for the delay, we'd be open to a PR for this if you're interested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants