[android-developers] Re: extending parcelables
I just noticed that my workaround is not actually working. I did the following inside my Animal's CREATOR.createFromParcel: className = source.readString(); a = (Animal) Class.forName(className).newInstance(); a.readFromParcel(source); Now a has the right type (so Cat or Dog), but somehow still Animal.readFromParcel() is called. Aren't methods supposed to be "virtual" by default? On Jul 15, 4:11 pm, Bart van Wissen wrote: > > My current workaround is as follows: > - In each specific Animal subtype's writeToParcel() method, I write > the class name as a String into the Parcel as the first element. > - In the Animal's CREATOR.createFromParcel() I read the first String > out of the parcel, and based on that I create the appropriate type and > call the appropriate readFromParcel() method. > > I think this is kind of ugly though. I would expect the android > framework to take care of this for me. > > (My application is not actually dealing with animals, this is just to > make it simpler to explain the issue) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: extending parcelables
It looks like your Animal class is abstract and should never be constructed. Declare Animal 'abstract' and see if you get an instantiation error. Have your tried to put the 'public static final Parcelable.Creator CREATOR' and 'public static final Parcelable.Creator CREATOR' variables and definitions in both Cat and Dog (and not in Animal)? This will make sure that the factory-method createFromParcel creates the appropriate subclass (Cat or Dog, and not Animal). About the readFromParcel(). If Cat and Dog both implement readFromParcel() it should work fine. Are you sure that the variable 'a' is not an Animal instance? On Jul 15, 10:39 am, Bart van Wissen wrote: > I just noticed that my workaround is not actually working. I did the > following inside my Animal's CREATOR.createFromParcel: > > className = source.readString(); > a = (Animal) Class.forName(className).newInstance(); > a.readFromParcel(source); > > Now a has the right type (so Cat or Dog), but somehow still > Animal.readFromParcel() is called. > Aren't methods supposed to be "virtual" by default? > > On Jul 15, 4:11 pm, Bart van Wissen wrote: > > > > > > > My current workaround is as follows: > > - In each specific Animal subtype's writeToParcel() method, I write > > the class name as a String into the Parcel as the first element. > > - In the Animal's CREATOR.createFromParcel() I read the first String > > out of the parcel, and based on that I create the appropriate type and > > call the appropriate readFromParcel() method. > > > I think this is kind of ugly though. I would expect the android > > framework to take care of this for me. > > > (My application is not actually dealing with animals, this is just to > > make it simpler to explain the issue)- Hide quoted text - > > - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: extending parcelables
On Jul 15, 5:21 pm, Streets Of Boston wrote: > It looks like your Animal class is abstract and should never be > constructed. Declare Animal 'abstract' and see if you get an > instantiation error. It should be abstract, but I cannot make it abstract because then I cannot add the CREATOR to it, which is needed to implement Parcelable. So now it is just an empty superclass. > > Have your tried to put the 'public static final Parcelable.Creator > CREATOR' and 'public static final Parcelable.Creator > CREATOR' variables and definitions in both Cat and Dog (and not > in Animal)? This will make sure that the factory-method > createFromParcel creates the appropriate subclass (Cat or Dog, and not > Animal). I did that, but it doesn't work. Those CREATORs are never actually called. The AIDL compiler sees the List and assumes that every element in the list is an Animal, and no subclass of it, and it creates the unmarshalling code accordingly. > About the readFromParcel(). > If Cat and Dog both implement readFromParcel() it should work fine. > Are you sure that the variable 'a' is not an Animal instance? > That was my mistake, I didn't properly override the method (one was private and the other was public). I deleted my post when I noticed this. Unfortunately this was while you were typing your reply. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: extending parcelables
We deliberately don't support this kind of thing to keep the IPC protocol simple. You have basically two choices: 1. Use a concrete class type, which will result in ONLY that classes CREATOR being used to unmarshall its data. The protocol does no type inspection here, since that would introduce a lot more overhead to a very common operation. 2. Use a generic Parcelable class type, in which case the marshalling code will inspect the class type, included that in the marshalled data, and use that to re-construct it on the other side. You can also do your trickery in the CREATOR to instantiate multiple types. If you have actually overloaded a method, then yes you will call through to the most derived class. This is purely a Java thing, and it certainly does work; this kind of thing is done in a few places in the system. On Wed, Jul 15, 2009 at 7:11 AM, Bart van Wissen wrote: > > I've run into the following issue: > > Say I have an Animal class, which is parcelable, and I have a Dog > class and a Cat class, which are subclasses of Animal and also > implement Parcelable. > > Say I have a service with the following AIDL interface: > > sendAnimals(List animals); > > Now I'm calling this remote interface from some client application, > passing it a List containing Cats and Dogs. What happens now is that > android calls the Cat's and the Dog's writeToParcel() methods at the > client side, but at the server side, the Animal's CREATOR is used, > which calls Animal's readFromParcel() method. > So the right data is sent from the client, but the wrong unmarshalling > code is executed at the server. > > > I have tried to put this in the AIDL file: > > sendAnimals(List animals); > > This results in a syntax error from the aidl compiler though. > > My current workaround is as follows: > - In each specific Animal subtype's writeToParcel() method, I write > the class name as a String into the Parcel as the first element. > - In the Animal's CREATOR.createFromParcel() I read the first String > out of the parcel, and based on that I create the appropriate type and > call the appropriate readFromParcel() method. > > I think this is kind of ugly though. I would expect the android > framework to take care of this for me. > > (My application is not actually dealing with animals, this is just to > make it simpler to explain the issue) > > > -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: extending parcelables
> 2. Use a generic Parcelable class type, in which case the marshalling code > will inspect the class type, included that in the marshalled data, and use > that to re-construct it on the other side. > I don't understand this. My program deals with Condition objects, which can be put in a list to specify a series of search conditions. There are specific classes like StringCondition, TimeCondition, LocationCondition, etc. They are all subclasses of Condition, but they all have their specific sets of fields. A list of those has to be passed to a service. Now how would I use a generic Parcelable class for this? To be honest I don't have much experience with generic classes. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: extending parcelables
Just use ArrayList as your data type. On Thu, Jul 16, 2009 at 5:31 AM, Bart van Wissen wrote: > > > 2. Use a generic Parcelable class type, in which case the marshalling > code > > will inspect the class type, included that in the marshalled data, and > use > > that to re-construct it on the other side. > > > > I don't understand this. > > My program deals with Condition objects, which can be put in a list to > specify a series of search conditions. There are specific classes like > StringCondition, TimeCondition, LocationCondition, etc. They are all > subclasses of Condition, but they all have their specific sets of > fields. > > A list of those has to be passed to a service. Now how would I use a > generic Parcelable class for this? To be honest I don't have much > experience with generic classes. > > > > -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: extending parcelables
On Jul 16, 6:52 pm, Dianne Hackborn wrote: > Just use ArrayList as your data type. > Putting List in my aidl file resulted in the following errors: "The method writeBinderList(List) in the type Parcel is not applicable for the arguments (List)" for this line in the automatically generated Proxy: _data.writeBinderList(conditionList); "Type mismatch: cannot convert from ArrayList to List" for this line: _arg0 = data.createBinderArrayList(); in the onTransact method of the Stub Apparently the AIDL compiler thinks a List is a List? I ended up just making it a List with no type at all, and verifying the type in the service implementation. This works for me. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---