jeudi 22 janvier 2015

Get data from a Bundle using reflection

I'm trying to read the data stored in an incoming bundle for which I don't know the keys to get.


I'm using reflection to read the methods and fields at run time from the bundle so I will be able to get a hold of the collections which holds the keys and values and then I will be able to iterate over them.


So once I get a hold of the bundle, I'm using reflection:



Class<?> bundleClass = bundle.getClass();


Now, the values are stored on a ArrayMap in the superclass (BaseBundle) so I call this to get the superclass:



Class<?> superClass = bundleClass.getSuperclass();


But iterating over superClass.getDeclaredMethods(); returns only methods of type Object which means I need to cast it to BaseBundle.


I'm unable to cast it to the class BaseBundle - I've tried using superClass.cast and explicit cast but the compiler complains.


What is the best way to cast the superclass object to it's type(BaseBundle). Also, am I right that using reflection I will be able to read the values stored in a bundle?






Aucun commentaire:

Enregistrer un commentaire