mcharmas/android-parcelable-intellij-plugin

Object is not parceled correctly if it has a field of an array of its own type

unparalleled opened this issue · 1 comments

Consider this example class:

public class Example {
    private Example[] array;
}

After generating the parcelable code, we get:

public class Example implements Parcelable {

    private Example[] array;


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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(this.array, flags);
    }

    public Example() {
    }

    protected Example(Parcel in) {
        this.array = in.readParcelable(Example[].class.getClassLoader());
    }

    public static final Parcelable.Creator<Example> CREATOR = new Parcelable.Creator<Example>() {
        @Override
        public Example createFromParcel(Parcel source) {
            return new Example(source);
        }

        @Override
        public Example[] newArray(int size) {
            return new Example[size];
        }
    };
}

writeParcelable should be writeTypedArray
readParcelable should be createTypedArray

This issue seams to be in ParcelableSerializerFactory.getSerializer() where the psiType of the class we're currently working on has not yet implemented the Parcelable interface.

A fallback to writeTypedArrayfor unidentified arrays would probably work. You'll get a compile time error if it's not a parcelable. Or, to be safe, writeParcelableArray, to handle objects of different types in the array.