rharter / auto-value-gson

AutoValue Extension to add Gson De/Serializer support
Apache License 2.0
607 stars 103 forks source link

Generic base types cause compile errors #240

Closed ZacSweers closed 4 years ago

ZacSweers commented 4 years ago

The following code will fail to build.

Given a generic base class

public abstract class GenericBase<T> {

  public abstract T generic();

  public abstract static class Builder<T, R extends GenericBase<T>, B extends Builder<T, R, B>> {
    public abstract B generic(T generic);

    protected abstract R autoBuild();
  }
}

And autovalue class that uses it

@GenerateTypeAdapter
@AutoValue
public abstract class AddressGenericBase extends GenericBase<Address> {

  public abstract String topLevelNonGeneric();

  @AutoValue.Builder public abstract static class Builder
      extends GenericBase.Builder<Address, AddressGenericBase, Builder> {
    public abstract Builder topLevelNonGeneric(String topLevel);
  }
}

This is due to use not using the propertyTypes() API from extensions, so we try to write type variables rather than their materialized types. The fix is to use the propertyTypes() API for this. Note that we'll need to make a couple adjustments to how builder methods are looked up, as they still try to match based on the return type since they override the source ExecutableElement.

ZacSweers commented 4 years ago

I've got a fix for this locally, based on #239. Will PR once that's in

ZacSweers commented 4 years ago

Went ahead and PRd, will merge whichever one is seen first and just rebase the other as needed after