Below is the documentation of @Deprecated annotation.
/**
* A program element annotated @Deprecated is one that programmers
* are discouraged from using, typically because it is dangerous,
* or because a better alternative exists. Compilers warn when a
* deprecated program element is used or overridden in non-deprecated code.
*
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
My question is: @Deprecated (a marker annotation) is for the programmer to indicate that this method/ field/ package... is discouraged from using it, then why RetentionPolicy is set to RUNTIME why not RetentionPolicy.CLASS is used?
Reflection in Java can be used to read annotations of a class at runtime. If RetentionPolicy.CLASS were used then the @Deprecated annotation would not be available to be read like this.
As an example, a reflective program might read in a user-provided class, and execute all non-deprecated static methods without any arguments. Maybe someone can come up with a better example than that. Some reason why a unit testing framework might make use of it perhaps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With