Search code examples
javareflectionclassloaderreflections

How to use Reflection to inspect non-static fields of a class that is loaded with a different ClassLoader?


I am using a child URLClassLoader to load an android.jar that can be found in the Android SDK (sdk/platforms/) at runtime, then I use the Google Reflections library to inspect the new classes I loaded. However, it seems that with the following code, I can only inspect static fields; I would also like to inspect instance fields. (I have no problems with methods though!)

Path jarPath = fs.getPath("/android-sdk-macosx/platforms/android-10/android.jar");
URL[] urls = new URL[]{jarPath.toUri().toURL()};
Reflections r = new Reflections(new ConfigurationBuilder().setUrls(urls).setScanners(
    new SubTypesScanner(false)
));

URLClassLoader cl = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader());
for (String cls : r.getAllTypes()) {
    System.out.println(cls);
    Class<?> c = cl.loadClass(cls);
    Field[] allFields = c.getDeclaredFields();
    for (Field f : allFields) {
        System.out.println('\t' + (f.getModifiers() & Modifier.STATIC) + " " + f.getName());
    }
}

For example, when the above code inspects android.view.View, it only prints the following:

android.view.View
8 VIEW_LOG_TAG
8 NO_ID
8 VISIBLE
8 INVISIBLE
8 GONE
8 DRAWING_CACHE_QUALITY_LOW
8 DRAWING_CACHE_QUALITY_HIGH
8 DRAWING_CACHE_QUALITY_AUTO
8 SCROLLBARS_INSIDE_OVERLAY
8 SCROLLBARS_INSIDE_INSET
8 SCROLLBARS_OUTSIDE_OVERLAY
8 SCROLLBARS_OUTSIDE_INSET
8 KEEP_SCREEN_ON
8 SOUND_EFFECTS_ENABLED
8 HAPTIC_FEEDBACK_ENABLED
8 FOCUSABLES_ALL
8 FOCUSABLES_TOUCH_MODE
8 FOCUS_BACKWARD
8 FOCUS_FORWARD
8 FOCUS_LEFT
8 FOCUS_UP
8 FOCUS_RIGHT
8 FOCUS_DOWN
8 EMPTY_STATE_SET
8 ENABLED_STATE_SET
8 FOCUSED_STATE_SET
8 SELECTED_STATE_SET
8 WINDOW_FOCUSED_STATE_SET
8 ENABLED_FOCUSED_STATE_SET
8 ENABLED_SELECTED_STATE_SET
8 ENABLED_WINDOW_FOCUSED_STATE_SET
8 FOCUSED_SELECTED_STATE_SET
8 FOCUSED_WINDOW_FOCUSED_STATE_SET
8 SELECTED_WINDOW_FOCUSED_STATE_SET
8 ENABLED_FOCUSED_SELECTED_STATE_SET
8 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET
8 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_SELECTED_STATE_SET
8 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_FOCUSED_STATE_SET
8 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_FOCUSED_SELECTED_STATE_SET
8 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_ENABLED_STATE_SET
8 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_ENABLED_SELECTED_STATE_SET
8 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_ENABLED_FOCUSED_STATE_SET
8 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET
8 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET
8 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET
8 OVER_SCROLL_ALWAYS
8 OVER_SCROLL_IF_CONTENT_SCROLLS
8 OVER_SCROLL_NEVER

But there is obviously more to it!

What I am doing wrong?


Solution

  • After throwing the jars through a decompiler, it turns out the android.jar within the SDK is merely an empty shell so that IDEs such as Eclipse and IntelliJ can use a smaller jar to help with auto-complete. The code is fine but I'll need to inspect complete jars. I've found a Github repo that hosts these complete jars: https://github.com/Sable/android-platforms, if anyone is interested.