2011-10-02 25 views
5

Tôi muốn quét đường dẫn lớp cho một số chú thích nhất định trong Android.có thể quét đường dẫn lớp học android cho chú thích không?

Tôi đã chỉ tìm thấy một giải pháp cho vấn đề này: http://mindtherobot.com/blog/737/android-hacks-scan-android-classpath/

Và như tác giả viết giải pháp này hoạt động, nhưng có một số hạn chế. Có cách nào trong tương lai làm bằng chứng này trong android? Bất kỳ thư viện nào cung cấp chức năng này?

+0

Tôi rất muốn nhìn thấy câu hỏi này đã được trả lời. Tôi hiện đang cố gắng giải quyết cùng một vấn đề. Khung quét chú thích mà tôi đã sử dụng trong quá khứ là Reflections (http://code.google.com/p/reflections/), nhưng tôi không thể tìm ra cách "trỏ nó" đúng cách vào mã được biên dịch cơ sở ... – Andrey

+0

Tôi chỉ có thể nghĩ đến việc cho phép Reflections quét các chú thích tại thời gian biên dịch và tạo một tệp XML với thông tin đó (thực tế, hỗ trợ) và sau đó tải tất cả thông tin từ tệp đó vào thời gian chạy (xem dưới cùng của trang dự án và http://code.google.com/p/reflections/wiki/ReflectionsMojo để biết chi tiết) – Andrey

Trả lời

1

này làm việc cho tôi sử dụng 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; 
}  

Tôi cũng tạo ra một thạch để xác định nếu một lớp học được bắt nguồn từ 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; 
} 

thưởng thức - B

+0

Như bạn có thể đã nhận thấy, đây là cơ bản giống mã tôi đã thêm vào liên kết. Tôi đang sử dụng nó và nó có vẻ hoạt động, nhưng nó rất thử nghiệm. Dù sao, cảm ơn cho câu trả lời (và mặc dù nó có vẻ làm việc tốt) không phải là giải pháp lý tưởng tôi đang tìm kiếm. –

Các vấn đề liên quan