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 []
What am I missing?
Because the
@Overrideannotation hasRetention=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.