Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to scan the android classpath for annotations?

Tags:

java

android

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 works, but has several limitations. Are there any future-proof ways of doing this in android? Any libraries providing this functionality?

like image 985
Jarle Hansen Avatar asked Nov 14 '22 16:11

Jarle Hansen


1 Answers

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

like image 199
Deofol Avatar answered Dec 25 '22 07:12

Deofol