0

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?

Update:

My ultimate goal is somehow find where in a bundle stored a URL or some other reference to an image. Since I don't know how the bundle was built, I need to investigate the bundle some how.

Any suggestions on how to find that image reference will be greatly appreciated

Bended
  • 494
  • 1
  • 7
  • 22
  • Why are you using reflection? You can get the set of keys using BaseBundle#keySet() function. – Varun Singh Jan 22 '15 at 08:31
  • BaseBundle was added in API 21 ...so on devices with API < 21 base class is Object ... [Bundle.java(kitkat)](https://android.googlesource.com/platform/frameworks/base.git/+/kitkat-mr2.2-release/core/java/android/os/Bundle.java) => [Bundle.java(lolipop)](https://android.googlesource.com/platform/frameworks/base.git/+/lollipop-release/core/java/android/os/Bundle.java) – Selvin Jan 22 '15 at 08:40
  • Thank you both for bringing the info to my attention. I will use KeySet to iterate over – Bended Jan 22 '15 at 08:50

1 Answers1

2

Assuming you are talking about android.os.Bundle: you don’t have to use reflection. BaseBundle provides the method keySet() that returns a list of all keys used in this bundle.

Example:

Bundle bundle = getMyBundle();
for (String key : bundle.keySet()) {
    String value = bundle.get(key);
    try {
        URL url = new URL(value);
        // no exception thrown! this is a valid URL
        // do something with ulr
    } catch (MalformedURLException exception) {
        // this item is no URL
    }
}
Robin Krahl
  • 5,268
  • 19
  • 32
  • Thank you this was a much easier way for sure and I will except your answer. However maybe you can help me further - My ultimate goal is to find inside the bundle, the string which represent an image location. Say the incoming bundle contains a url to an image - I'm trying to get that url. Do you have any idea how to find it? – Bended Jan 22 '15 at 08:59
  • @YosiMizrachi I changed the example code to find URLs. Does this help? – Robin Krahl Jan 22 '15 at 09:05
  • One of the values in the keyset was a bitmap! – Bended Jan 22 '15 at 09:32
  • 1
    I don't like the use of try-catch for data validation. Check answers [here](http://stackoverflow.com/questions/4905075/how-to-check-if-url-is-valid-in-android). I suggest you check if Url is valid using methods described there, and only then use try-catch. – Marius Jan 22 '15 at 09:43
  • Well I ended up ditching the reflection and then just investigating the bundle (I was obviously unknowledgeable about how to use it). So I'm retrieving all the keys in the keyset and I will look for the image, file or other attachments in there – Bended Jan 22 '15 at 11:29
  • @Marius I absolutely agree, but as far as I know, there is no alternative using vanilla JDK. – Robin Krahl Jan 22 '15 at 18:56