为什么我在这里得到一个空的注释数组 [英] Why am I getting an empty array of annotations here
本文介绍了为什么我在这里得到一个空的注释数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据 answer 我应该在以下代码中使用覆盖"(或类似的名称):
According to the doc and to this answer I should be having "Override" (or something similar) in the following code:
import java.lang.reflect.*;
import java.util.*;
import static java.lang.System.out;
class Test {
@Override
public String toString() {
return "";
}
public static void main( String ... args ) {
for( Method m : Test.class.getDeclaredMethods() ) {
out.println( m.getName() + " " + Arrays.toString( m.getDeclaredAnnotations()));
}
}
}
但是,我得到一个空数组.
But, I'm getting an empty array.
$ java Test
main []
toString []
我想念什么?
推荐答案
因为 @Override
注释具有Retention=SOURCE
,即未编译到类文件中,因此在运行时无法通过反射使用.仅在编译期间有用.
Because the @Override
annotation has Retention=SOURCE
, i.e. it is not compiled into the class files, and is therefore not available at runtime via reflection. It's useful only during compilation.
这篇关于为什么我在这里得到一个空的注释数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文