2011-10-02 76 views
5

我想在Android中掃描類路徑中的某些註釋。是否有可能掃描Android類路徑的註釋?

我只找到一個解決這個問題:http://mindtherobot.com/blog/737/android-hacks-scan-android-classpath/

正如筆者寫此解決方案有效,但也有一些侷限性。有沒有任何未來的方法在android中做到這一點?任何提供這種功能的庫?使用

+0

我喜歡看到這個問題回答了。我目前正試圖解決完全相同的問題。我以前使用過的註釋掃描框架是Reflections(http://code.google.com/p/reflections/),但我無法弄清楚如何在編譯後的代碼中正確「指向它」 base ... – Andrey

+0

我只能想到讓Reflections在編譯時掃描註釋並生成一個包含該信息的XML文件(實際上它支持),然後在運行時從該文件加載所有信息(請參閱項目頁面的底部,http://code.google.com/p/reflections/wiki/ReflectionsMojo詳細信息) – Andrey

回答

1

這對我的作品的Android 3.0

public static <T extends Annotation> List<Class> getClassesAnnotatedWith(Class<T> theAnnotation){ 

    // In theory, the class loader is not required to be a PathClassLoader 
    PathClassLoader classLoader = (PathClassLoader) Thread.currentThread().getContextClassLoader(); 
    Field field = null; 
    ArrayList<Class> candidates = new ArrayList<Class>(); 

    try { 
     field = PathClassLoader.class.getDeclaredField("mDexs"); 
     field.setAccessible(true); 
    } catch (Exception e) { 
     // nobody promised that this field will always be there 
     Log.e(TAG, "Failed to get mDexs field", e); 
    } 

    DexFile[] dexFile = null; 
    try { 
     dexFile = (DexFile[]) field.get(classLoader); 
    } catch (Exception e) { 
     Log.e(TAG, "Failed to get DexFile", e); 
    } 

    for (DexFile dex : dexFile) { 
     Enumeration<String> entries = dex.entries(); 
     while (entries.hasMoreElements()) { 
     // Each entry is a class name, like "foo.bar.MyClass" 
     String entry = entries.nextElement(); 

     // Load the class 
     Class<?> entryClass = dex.loadClass(entry, classLoader); 
     if (entryClass != null && entryClass.getAnnotation(theAnnotation) != null) { 
      Log.d(TAG, "Found: " + entryClass.getName()); 
      candidates.add(entryClass); 
     } 
     } 
    } 

    return candidates; 
}  

我還創建了一個determin如果一個類從X衍生

public static List<Class> getClassesSuperclassedOf(Class theClass){ 

    // In theory, the class loader is not required to be a PathClassLoader 
    PathClassLoader classLoader = (PathClassLoader) Thread.currentThread().getContextClassLoader(); 
    Field field = null; 
    ArrayList<Class> candidates = new ArrayList<Class>(); 

    try { 
     field = PathClassLoader.class.getDeclaredField("mDexs"); 
     field.setAccessible(true); 
    } catch (Exception e) { 
     // nobody promised that this field will always be there 
     Log.e(TAG, "Failed to get mDexs field", e); 
    } 

    DexFile[] dexFile = null; 
    try { 
     dexFile = (DexFile[]) field.get(classLoader); 
    } catch (Exception e) { 
     Log.e(TAG, "Failed to get DexFile", e); 
    } 

    for (DexFile dex : dexFile) { 
     Enumeration<String> entries = dex.entries(); 
     while (entries.hasMoreElements()) { 
     // Each entry is a class name, like "foo.bar.MyClass" 
     String entry = entries.nextElement(); 

     // Load the class 
     Class<?> entryClass = dex.loadClass(entry, classLoader); 
     if (entryClass != null && entryClass.getSuperclass() == theClass) { 
      Log.d(TAG, "Found: " + entryClass.getName()); 
      candidates.add(entryClass); 
     } 
     } 
    } 

    return candidates; 
} 

享受 - B

+0

正如您可能已經注意到的,這與我在鏈接中添加的代碼基本相同。我正在使用它,它似乎工作,但它是非常實驗性的。無論如何,感謝您的答案(雖然它似乎工作正常),而不是我尋找的理想解決方案。 –

相關問題