sockeqwe / fragmentargs

Annotation Processor for setting arguments in android fragments
http://hannesdorfmann.com/android/fragmentargs
Apache License 2.0
1.08k stars 87 forks source link

CastedArrayListArgsBundler bug to respect autovalue parcelable classes #75

Closed ersin-ertan closed 7 years ago

ersin-ertan commented 7 years ago

Example project with FragmentArgs, AutoValueParcel, where fragmentArgs is not respecting @AutoValue public abstract class AutoTitle implements Parcelable https://github.com/ersin-ertan/FragmentArgsAutoValue

public final class MyFragmentBuilder {

  private static final com.hannesdorfmann.fragmentargs.bundler.CastedArrayListArgsBundler bundler1 = new com.hannesdorfmann.fragmentargs.bundler.CastedArrayListArgsBundler();

  private final Bundle mArguments = new Bundle();

  public MyFragmentBuilder(@NonNull AutoTitle autoTitle, @NonNull java.util.List<AutoTitle> autoTitleList) {

    mArguments.putParcelable("autoTitle", autoTitle);

    mArguments.putBoolean("com.hannesdorfmann.fragmentargs.custom.bundler.2312A478rand.autoTitleList", true);
    bundler1.put("autoTitleList", autoTitleList, mArguments);
  }

  @NonNull
  public static MyFragment newMyFragment(@NonNull AutoTitle autoTitle, @NonNull java.util.List<AutoTitle> autoTitleList) {
    return new MyFragmentBuilder(autoTitle, autoTitleList).build();
  }

  public static final void injectArguments(@NonNull MyFragment fragment) {
    Bundle args = fragment.getArguments();
    if (args == null) {
      throw new IllegalStateException("No arguments set. Have you setup this Fragment with the corresponding FragmentArgs Builder? ");
    }

    if (!args.containsKey("com.hannesdorfmann.fragmentargs.custom.bundler.2312A478rand.autoTitleList")) {
      throw new IllegalStateException("required argument autoTitleList is not set");
    }
    fragment.autoTitleList = bundler1.get("autoTitleList", args); // **ERROR HERE**

    if (!args.containsKey("autoTitle")) {
      throw new IllegalStateException("required argument autoTitle is not set");
    }
    fragment.autoTitle = args.getParcelable("autoTitle");
  }

  @NonNull
  public MyFragment build() {
    MyFragment fragment = new MyFragment();
    fragment.setArguments(mArguments);
    return fragment;
  }

  @NonNull
  public <F extends MyFragment> F build(@NonNull F fragment) {
    fragment.setArguments(mArguments);
    return fragment;
  }
}

Error:(34, 42) error: incompatible types: List<CAP#1> cannot be converted to List where CAP#1 is a fresh type-variable: CAP#1 extends Parcelable from capture of ? extends Parcelable

Also tested creating own bundler, but ran into the same problem

ersin-ertan commented 7 years ago

There is no compilation error if you explicitly use ArrayList and CastedArrayListArgsBundler.class is not required for an ArrayList of @Value public abstract class AutoTitle implements Parcelable objects.