本文介绍了是否有可能扫描android类路径注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在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/
正如作者所写,这个解决方案有效,但有一些限制。在Android中有没有任何面向未来的方法?任何提供此功能的库?
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?
推荐答案
这适用于我使用android 3.0
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;
}
我还创建了一个来确定一个类是否来自X
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;
}
享受 - B
这篇关于是否有可能扫描android类路径注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!