Parceler use inheriting generic type

494 views Asked by At

I am struggling to find a way to use parceler library in a specific case.

Having an abstract class with generic types, like this

public abstract class ParentClass<O> {

    private String id;
    private O options;

    public String getId() {
        return id;
    }

    public void setId(String id) {
       this.id = id;
    }

    public O getOptions() {
        return options;
    }

    public void setOptions(O options) {
        this.options = options;
    }

}

And another class which we will use as options (which we will serialize)...

@Parcel(Parcel.Serialization.BEAN)
public class MyOptions {

    private boolean option1;

    public boolean getOption1() {
        return option1;
    }

    public void setOption1(boolean option1) {
        this.option1 = option1;
    }
}

And a 3rd class that we want to serialize called ChildClass

@Parcel(Parcel.Serialization.BEAN)
public class ChildClass extends ParentClass<MyOptions>{}

Despite I am not serializing ParentClass, but just ChildClass, I get a compiler error when I try to build the project

Parceler: Unable to find read/write generator for type java.lang.Object for ParentClass#options
    public void setOptions(O options) {

I am aware that the documentation states that "Parcel will error if the generic parameter is not mapped.", but in this case I am only interested on serializing ChildClass while keeping the attributes from ParentClass (that is, id and the instance of the class MyOptions). Is there any possibility to make it possible? Tried to follow some of the advices here but had no success.

1

There are 1 answers

1
John Ericksen On BEST ANSWER

Not the best option, but you can get around this issue by overriding the getOptions() and setOptions() methods in your base class. This specifies the type to Parceler:

@Parcel(Parcel.Serialization.BEAN)
public class ChildClass extends ParentClass<MyOptions>{

    @Override
    public MyOptions getOptions() {
        return super.getOptions();
    }

    @Override
    public void setOptions(MyOptions options) {
        super.setOptions(options);
    }
}

I imagine we could determine this implied type, but for the time being this is the closest solution.