android sınıf
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;
}
zevk X türetilmiştir ben de determin birini oluşturdu 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;
}
- B
ben görmek isterdim Bu soru cevaplandı. Şu anda aynı sorunu çözmeye çalışıyorum. Geçmişte kullandığım ek açıklama tarama çerçevesi, Yansımalar (http://code.google.com/p/reflections/), ancak derlenmiş kodda düzgün bir şekilde nasıl “işaretleyeceğimi” anlayamıyorum. base ... – Andrey
Yansımaları yalnızca derleme zamanında ek açıklamaları taramasını ve bu bilgilerle bir XML dosyası oluşturmasını (aslında destek) gerçekleştirmesini düşünebilirim ve sonra tüm bilgileri o dosyadan çalışma zamanında yükleyebilirim (bkz. Proje sayfasının alt kısmı ve ayrıntılar için http://code.google.com/p/reflections/wiki/ReflectionsMojo) – Andrey