Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent class CREATOR is miss when i parcelable with it in child class #55

Open
beilly opened this issue May 16, 2016 · 6 comments
Open

Comments

@beilly
Copy link

beilly commented May 16, 2016

Hi, Parent Class impliment Parcelable, and i parcelable with it, it's ok.
But when i parcelable with it in child class,Parent's CREATOR is gone.i must reparcelable in Parent class.is it a bug?

@DariusL
Copy link

DariusL commented May 16, 2016

Could you post some failing code?

@beilly
Copy link
Author

beilly commented May 17, 2016

This Parent Class parcelabled ok :

public class ResultInfo implements Parcelable {
    @SerializedName("status")
    protected Integer status;
    @SerializedName("msg")
    protected String msg;

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(this.status);
        dest.writeString(this.msg);
    }

    protected ResultInfo(Parcel in) {
        this.status = (Integer) in.readValue(Integer.class.getClassLoader());
        this.msg = in.readString();
    }

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

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

When i parce the child class,child is ok:

public class User extends ResultInfo{
    protected  String name;
    protected  Integer id;

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.name);
        dest.writeValue(this.id);
    }

    protected User(Parcel in) {
        super(in);
        this.name = in.readString();
        this.id = (Integer) in.readValue(Integer.class.getClassLoader());
    }

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

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

But the parent class has been changed like this(public static final Parcelable.Creator CREATOR is missing):

public class ResultInfo implements Parcelable {
    @SerializedName("status")
    protected Integer status;
    @SerializedName("msg")
    protected String msg;

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(this.status);
        dest.writeString(this.msg);
    }

    protected ResultInfo(Parcel in) {
        this.status = (Integer) in.readValue(Integer.class.getClassLoader());
        this.msg = in.readString();
    }

}

@beilly
Copy link
Author

beilly commented May 17, 2016

I think this problem may caused by CodeGenerator.java (the removeExistingParcelableImplementation method).It will delete it's parent CREATOR.Or you may want to delete child's CREATOR?

    /**
     * Strips the
     *
     * @param psiClass
     */
    private void removeExistingParcelableImplementation(PsiClass psiClass) {
        PsiField[] allFields = psiClass.getAllFields();

        // Look for an existing CREATOR and remove it
        for (PsiField field : allFields) {
            if (field.getName().equals(CREATOR_NAME)) {
                // Creator already exists, need to remove/replace it
                field.delete();
            }
        }

        findAndRemoveMethod(psiClass, psiClass.getName(), TYPE_PARCEL);
        findAndRemoveMethod(psiClass, "describeContents");
        findAndRemoveMethod(psiClass, "writeToParcel", TYPE_PARCEL, "int");
    }

@DariusL

@fthdgn
Copy link

fthdgn commented Jul 24, 2017

Hi,
The problem is "getAllFields" method. It returns fields of parent classes also. I replaced it with "getFields" method.

@beilly
Copy link
Author

beilly commented Jul 25, 2017

Hi,
@fthdgn If you test it,give a pull request please

@fthdgn
Copy link

fthdgn commented Jul 25, 2017

Hi @shibenli
I've already created a pull request before my comment. #67 But I think there is a problem with my PR. It also shows my other commits after PR created. I will create a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants