2015-01-05 36 views
0

我正在使用兒童URLClassLoader加載可在運行時在Android SDK(sdk/platforms /)中找到的android.jar,然後使用Google Reflections庫檢查我加載的新類。但是,似乎用下面的代碼,我只能檢查靜態字段;我也想檢查實例字段。 (我有方法沒有問題,但!)如何使用Reflection檢查使用不同ClassLoader加載的類的非靜態字段?

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()); 
    } 
} 

例如,當上面的代碼檢查android.view.View,只打印了以下內容:

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!

我在做什麼錯?

回答

2

在通過反編譯器拋出jar文件後,發現SDK中的android.jar只是一個空殼,因此Eclipse和IntelliJ等IDE可以使用較小的jar來幫助自動完成。代碼很好,但我需要檢查完整的罐子。我發現了一個Github回購站,它包含了這些完整的罐子:https://github.com/Sable/android-platforms,如果有人感興趣的話。

相關問題