Har*_*she 2 java android annotations
在我的Android应用程序中,我有以下类:
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()]);
}
}
Run Code Online (Sandbox Code Playgroud)
和
public abstract class B extends A {
@MyAnnotation(Name="fieldDelaredInB")
public long fieldDelaredInB;
}
Run Code Online (Sandbox Code Playgroud)
当我调用时B.findAnnotations()
,我可以看到getClass().getFields()
返回B中声明的字段 - fieldDelaredInB
例如,但是没有返回这些字段的注释 - 即,当我调用时f.getAnnotations()
,f.getDeclaredAnnotations()
或者无论如何,我都会为null .
这是一个不熟悉派生类属性的超类问题吗?看起来很奇怪,考虑到当我getFields()
从超类调用时派生类DO的字段出现的事实.
我错过了什么想法?
谢谢,哈瑞尔
除非标记为运行时保留,否则不会在运行时加载注释@Retention(RetentionPolicy.RUNTIME)
.您必须将此@Retention
注释放在@MyAnnotation
注释上:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2399 次 |
最近记录: |