I have an Event
class that defines a private HashMap
like this:
private Map<String, Object> data = new HashMap<String, Object>();
The Event
class is a wrapped for any kind of "event". This HashMap
can contain any object instance which is referenced by a key. The class receiving an Event
instance knows what class is related to each key, so it can safely cast the Object
to its corresponding subclass.
The problem arises when I try to pass an Event
instance between 2 processes. Event
implements Parcelable
so it can be sent through a Message
:
Bundle bundle = new Bundle();
bundle.putParcelable(Event.BUNDLE_KEY, event);
// Make the message with the bundle
Message message = new Message();
message.setData(bundle);
When unmarshalling:
public void readFromParcel(Parcel in) {
idEvent = in.readInt();
priority = in.readInt();
idSource = in.readInt();
idDestination = in.readInt();
action = in.readInt();
Bundle mapBundle = in.readBundle();
if (mapBundle.getSerializable(MAP_KEY) instanceof HashMap) {
data = (Map<String, Object>) mapBundle.getSerializable(MAP_KEY);
} else {
Log.e(TAG, "Parcel reading error: not a HashMap");
}
}
The problem is this won't work since I need to specify to mapBundle
which ClassLoader
to use, e.g. mapBundle.setClassLoader(Entity.class.getClassLoader());
. But I don't know what Object
subclasses the HashMap
will have...
This is what I thought:
Write a
ClassLoader
that loads any of these classes. The problem is that I cannot get a way to obtain thebyte[]
representing the object since it's inside theHashMap
. And I cannot usemapBundle.getSerializable()
to get it because it precisely throwsClassNotFound
exception.Pass some extra information so I can know what classes are on the
HashMap
. Besides this looking as redundant information, still no go because if I set one classloader on the Bundle, it will still throw aClassNotFound
exception on the other classes...
I would really appreciate some help on this issue. Thanks in advance!