rharter / auto-value-gson

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

work with the room architecture component #187

Closed wuyang102 closed 5 years ago

wuyang102 commented 5 years ago

The room architecture component has support auto value now, but it can not work this library. There will be a medium class like $AutoValue_xxx.java generated by this library if the original abstract class has a static method named typeAdpter.

// the original class
@AutoValue
@AutoValue.CopyAnnotations
@Entity
public abstract class Animal {

    @AutoValue.CopyAnnotations
    @PrimaryKey
    @SerializedName(value = "id")
    public abstract int id();

    @AutoValue.CopyAnnotations
    @SerializedName(value = "name")
    public abstract String name();

    static TypeAdapter<Animal> typeAdapter(Gson gson) {
        return new AutoValue_Animal.GsonTypeAdapter(gson);
    }
}

// the medium class created by auto value but modified by this library
@Entity
// Generated by com.google.auto.value.processor.AutoValueProcessor
abstract class $AutoValue_Animal extends Animal {

  @PrimaryKey
  @SerializedName(value = "id")
  private final int id;

  @SerializedName(value = "name")
  private final String name;

  ... ... ...
}

// the final class created by this library
final class AutoValue_Animal extends $AutoValue_Animal {
  AutoValue_Animal(int id, String name) {
    super(id, name);
  }

  public static final class GsonTypeAdapter extends TypeAdapter<Animal> {
  ... ... ...
  }
}
JakeWharton commented 5 years ago

Isn't this a Room bug then?

wuyang102 commented 5 years ago

Isn't this a Room bug then?

No. The abstract medium class start with $ symbol is created by this library.

JakeWharton commented 5 years ago

Any extension will create that type (and potentially more) and it's perfectly valid since it sits between the AutoValue_-prefixed type and the user-written type. Please file a Room bug.

On Tue, Oct 16, 2018 at 10:06 AM wuyang102 notifications@github.com wrote:

Isn't this a Room bug then?

No. The abstract medium class start with $ symbol is created by this library.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/rharter/auto-value-gson/issues/187#issuecomment-430251157, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEETUf35a1euJ4bDkiwWOXGCGYMfnqks5ulef6gaJpZM4XeYOn .

rharter commented 5 years ago

This is how the architecture of AutoValue is intended to work.

Room should be using the builder, creating their own implementation of the abstract class, or using the package private constructor if it exists to comply with the first tenet of AutoValue, that the classes be API-invisible (callers cannot become dependent on your choice to use it).

If Room doesn't work with this extension, please file a bug with Room, as this extension works as AutoValue extensions are intended to work..