frankiesardo / auto-parcel

Android Parcelable models made easy
Eclipse Public License 1.0
1.37k stars 83 forks source link

Interprets toBuilder method as field accessor #46

Closed Tagakov closed 8 years ago

Tagakov commented 8 years ago

With given

@AutoValue
public abstract class Example implements Parcelable{
    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Example build();
    }

    public abstract Builder toBuilder();
}

AutoParcel generates

final class AutoValue_Example extends $AutoValue_Example {

  private final static ClassLoader CL = AutoValue_Example.class.getClassLoader();

  public AutoValue_Example (
    Example.Builder toBuilder
  ) {
    super(
      toBuilder
    );
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(toBuilder());
  }

  private AutoValue_Example(Parcel in) {
    this(
      (Example.Builder) in.readValue(CL)
    );
  }

  public static final Parcelable.Creator<AutoValue_Example> CREATOR = new Parcelable.Creator<AutoValue_Example>() {
    @Override
    public AutoValue_Example createFromParcel(Parcel in) {
      return new AutoValue_Example(in);
    }
    @Override
    public AutoValue_Example[] newArray(int size) {
      return new AutoValue_Example[size];
    }
  };
}

Which leads to compile errors

dpreussler commented 8 years ago

having the same issue. Any hint how to solve this? It doesnt seem related to the auto-value version. Pure auto-value works both 1.2r1 and 1.2. But as soon as using auto-parcel this breaks :/

Tagakov commented 8 years ago

Thanks to @daugeldauge for pointing out to me that this issue is connected with https://github.com/google/auto/issues/281 . So this is not the problem of AutoParcel. Workaround for this issue is to implement non-abstract toBuilder method:

public Builder toBuilder() {
    return new AutoValue_<YourClass>.Builder(this);
}