为什么我会收到注释的emtpy阵列这里 [英] Why am I getting an emtpy array of annotations here
问题描述
根据<一href=\"http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#getDeclaredAnnotations%28%29\"相对=nofollow> DOC 并为此<一个href=\"http://stackoverflow.com/questions/6460980/what-does-the-getdeclaredxyz-stands-for-in-java-reflect-package/6461010#6461010\">answer我shuold是具有覆盖(或类似的东西)在以下code:
According to the doc and to this answer I shuold 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 []
我是什么失踪?
推荐答案
由于该<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Override.html\"><$c$c>@Override$c$c>注释有附件= 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.
这篇关于为什么我会收到注释的emtpy阵列这里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!