简体   繁体   中英

Parceler - annotate interface with factories on an implementation

On android when using Parceler, is it possible to annotate an interface using @Parceler and have it pick up possible @ParcelConstructor and @ParcelFactory annotations in implementation classes?

The goal is to avoid writing a custom ParcelConverter to re-use generic field mapping in Parceler, but it appears that things like the implementations= argument to @Parcel are directed more towards mapping subtypes to supertypes (rather than the other way around):

@Parcel  // can this be used to serialize **all** implementations ...
public interface MyIface {

  String getProperty1();
  int getProperty2();
}

public class MyImpl implements MyIface {
  @ParcelFactory  // and can this be used to deserialize the interface?
  public static MyImpl fromParcelerValues(final String property1, final int property2) {
    ...
  }
}

Of course there might be ambiguities if there are multiple matching mappings, but I can't seem to find any docs around getting this functionality to work even without any ambiguities.

You are correct, Parceler only operates on classes directly annotated with @Parcel and the implementations parameter (which is now depreciated and will shortly be removed from the api) is meant for something else.

I'll update this answer if you give a concrete example of what problem you're looking to solve around "generic field mapping".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM