I'm using AutoValue
with auto-value-with
extension to create a slightly altered object.
My class is :
@AutoValue
public abstract class FeedItem implements Parcelable {
public abstract String getAccountId();
public abstract String getName();
public abstract String getCompany();
@Nullable
public abstract String getAvatar();
@Nullable
public abstract String getPostImage();
@Nullable
public abstract String getThumb();
public abstract String getPostId();
public abstract String getFlaggedYes();
public abstract int getTotalFlags();
public abstract int getTotalComments();
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
public static Builder builder() {
return new AutoValue_FeedItem.Builder();
}
@AutoValue.Builder
public static abstract class Builder {
public abstract Builder setAccountId(String accountId);
public abstract Builder setName(String name);
public abstract Builder setCompany(String company);
public abstract Builder setAvatar(@Nullable String avatar);
public abstract Builder setPostImage(@Nullable String image);
public abstract Builder setThumb(@Nullable String thumb);
public abstract Builder setPostId(String postId);
public abstract Builder setFlaggedYes(String flaggedYes);
public abstract Builder setTotalFlags(int totalFlags);
public abstract Builder setTotalComments(int totalComments);
public abstract FeedItem build();
}
}
Where you can see that I'm using :
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
to create the altered object.
The problem is that when I'm trying to use it like :
FeedItem feedItem = dataset.get(position).withTotalFlags(updatedTotalFlags);
I get :
01-01 14:49:00.560 23369-23369/MY_PACKAGE E/AndroidRuntime: FATAL EXCEPTION: main
Process: MY_PACKAGE, PID: 23369
java.lang.IllegalStateException: Missing required properties: accountId name company postId flaggedYes totalComments
at MY_PACKAGE.data.models.$AutoValue_FeedItem$Builder.build($AutoValue_FeedItem.java:275)
at MY_PACKAGE.data.models.FeedItem.withTotalFlags(FeedItem.java:31)
at MY_PACKAGE.main.fragments.feed.adapter.FeedAdapter.updateItem(FeedAdapter.java:247)
at MY_PACKAGE.main.fragments.feed.FeedFragment.updateFlagState(FeedFragment.java:232)
at MY_PACKAGE.main.fragments.feed.FeedPresenter.onFlagStateRequestSuccess(FeedPresenter.java:97)
at MY_PACKAGE.main.fragments.feed.FeedRepository$1.onResponse(FeedRepository.java:72)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
You're not supposed to implement the wither methods yourself. Just declare it abstract and let
auto-value-with
do its job.Alternatively you can declare a
toBuilder()
method for your model and use it instead of creating a new builder.It will prefill all values from your original object and also resolve your error.