2011-10-02 50 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

正如您可能已经注意到的,这与我在链接中添加的代码基本相同。我正在使用它,它似乎工作,但它是非常实验性的。无论如何,感谢您的答案(虽然它似乎工作正常),而不是我寻找的理想解决方案。 –