Skip to content Skip to sidebar Skip to footer

Generic Class With Constraint Access Issue

This a general JAVA question. In android, there is an interface Parcelable: This is an example inside the official documentation: public class MyParcelable implements Parcelable {

Solution 1:

Instead of <T extends Parcelable> you need <T extends ShushContainer<T> & Parcelable> this way you specify that T is ShushContainer so you can access is methods and variables.

publicclassShushContainer<T extendsShushContainer<T> & Parcelable> 
implementsParcelable

Here is the example using Serializable

classSample<TextendsSample<T> & Serializable> implementsSerializable{

  publicstaticintCONST = 0;

   publicvoid foo()
   {
     T.CONST = 5;
   }
}

Update

If I understand correctly threre is another class which implements Parcelable which has CREATOR You are trying dynamic polymorphism for Static variables which is not possible.

Sample example to show how it fails

publicclassBase {
    publicstaticint count = 10;
}

publicclassChildextendsBase {
    publicstaticint count = 20;
}


classSample<TextendsBase> {
    T t = null;
    publicvoidprintCount() {
        System.out.println(T.count);
    }
    publicSample(T t) {
        this.t = t;
    }

    publicstaticvoidmain(String[] args) {
        Sample<Child> sample = new Sample<Child>(new Child());
        Sample<Base> sample1 = new Sample<Base>(new Base());
        sample.printCount();//Child value printed as 10
        sample1.printCount();//Same for parent value printed as  10
    }

}

This program fails because static fields are bound to Class rather than instance so there are two separate count one for Base and one for Child if you access value of Base then it will always be 10.

You can use reflection to check whether CREATOR field is present and access it.Which will not be possible without object or class object.

Or You can do something like below using TypeToken

classSample<T extendsSerializable> implementsSerializable {

    publicintabc=0;
    publicvoidfoo() {
    }
    publicstaticvoidmain(String[] args)throws SecurityException, NoSuchFieldException {
        TypeToken<Sample<Integer>> token = newTypeToken<Sample<Integer>>() {
        };
        Class<?> t = token.getRawType();
        Fieldfield= t.getDeclaredField("abc");
        field.setAccessible(true);
        System.out.println(field.getName());

    }
}

Solution 2:

This doesn't answer your question as to how to access the static CREATOR property, however you do not need to access this property implement Parcelable. See below for example:

publicclassTestModel<T extendsParcelable> implementsParcelable {

privateList<T> items;
privateString someField;

publicList<T> items() {
    return items;
}

publicvoidsetItems(List<T> newValue) {
    items = newValue;
}

publicStringsomeField() {
    return someField;
}

publicvoidsetSomeField(String newValue) {
    someField = newValue;
}

//region: Parcelable implementationpublicTestModel(Parcelin) {
    someField = in.readString();

    int size = in.readInt();
    if (size == 0) {
        items = null;
    }

    else {

        Class<?> type = (Class<?>) in.readSerializable();

        items = newArrayList<>(size);
        in.readList(items, type.getClassLoader());
    }
}

@Overridepublic int describeContents() {
    return0;
}

@OverridepublicvoidwriteToParcel(Parcel dest, int flags) {
    dest.writeString(someField);

    if (items == null || items.size() == 0)
        dest.writeInt(0);

    else {
        dest.writeInt(items.size());

        final Class<?> objectsType = items.get(0).getClass();
        dest.writeSerializable(objectsType);

        dest.writeList(items);
    }

    dest.writeInt(items.size());
    for(int i=0;i<items.size();i++)
        items.get(i).writeToParcel(dest, flags);
}

publicstatic final Parcelable.Creator<TestModel> CREATOR = newParcelable.Creator<TestModel>() {
    publicTestModelcreateFromParcel(Parcel in) {
        returnnewTestModel(in);
    }

    publicTestModel[] newArray(int size) {
        returnnewTestModel[size];
    }
};

//endregion
}

Post a Comment for "Generic Class With Constraint Access Issue"