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
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
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