In my Android application, i have the following classes:
public abstract class A implements IA {
private void findAnnotations() {
Field[] fields = getClass().getFields();
// Get all fields of the object annotated for serialization
if (fields != null && fields.length > 0) {
for (Field f : fields) {
Annotation[] a = f.getAnnotations();
if (annotation != null) {
// Do something
}
}
}
return serializationInfoList
.toArray(new SoapSerializationFieldInfo[serializationInfoList
.size()]);
}
}
and
public abstract class B extends A {
@MyAnnotation(Name="fieldDelaredInB")
public long fieldDelaredInB;
}
When i call B.findAnnotations(), i can see that getClass().getFields() returns the fields declared in B – fieldDelaredInB, e.g., but no annotations for these fields are returned – i.e, i get null when i call f.getAnnotations(), f.getDeclaredAnnotations() or whatsoever.
Is this an issue of a superclass unfamiliar with attributes of derived classes? seems weird, considering the fact that the fields of the derived class DO appear when i call getFields() from the superclass.
Any ideas of what am i missing?
Thanks,
Harel
An annotation is not loaded at runtime unless it is marked for runtime retention with
@Retention(RetentionPolicy.RUNTIME). You must place this@Retentionannotation on your@MyAnnotationannotation: