开发者

is it possible to scan the android classpath for annotations?

I want to scan the classpath for certain annotations in Android.

I have only found one solution to this problem: http://mindtherobot.com/blog/737/android-hacks-scan-android-classpath/

And as the author writes this solution开发者_Python百科 works, but has several limitations. Are there any future-proof ways of doing this in android? Any libraries providing this functionality?


This works for me using 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;
}    

I also created one to determin if a class was derived from 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;
}

enjoy - B

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜