Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the RetentionPolicy for @deprecated is RUNTIME?

Why at runtime is anyone interested in knowing that a method is deprecated? Can some provide me with some examples?

like image 876
escalon Avatar asked Oct 24 '09 18:10

escalon


People also ask

What is RetentionPolicy runtime?

RetentionPolicy. RUNTIME: The annotations annotated using the RUNTIME retention policy are retained during runtime and can be accessed in our program during runtime.

What is @retention RetentionPolicy source?

The RetentionPolicy. SOURCE function determines when an annotation is discarded. We apply @Retention as a meta-annotation to other annotations. The @Retention annotation indicates how long the annotated type should be kept in the program's lifecycle.

What is RetentionPolicy in Java?

The RetentionPolicy in Java determines the point at which an annotation should be discarded. @Retention is a meta-annotation that can be applied to other annotations. @Retention indicates how long the annotated type should be kept in the program's lifecycle.

What is the use of @retention annotation?

Annotation Type RetentionIndicates how long annotations with the annotated type are to be retained. If no Retention annotation is present on an annotation type declaration, the retention policy defaults to RetentionPolicy.


2 Answers

There are some frameworks and tools that instantiate objects to work with them.

For example, many JavaBean UI editors create instances of the beans and interact with them as the user manipulates the UI they're designing.

Having the @Deprecated annotation available at runtime allows tools such as this to flag deprecated methods, events, properties for the user.

like image 103
Scott Stanchfield Avatar answered Oct 22 '22 05:10

Scott Stanchfield


You're assuming that @deprecated is only of interest in the compile phase (IDE, compiler), but its not a stretch to imaging instrumentation scenarios where you require that information.

For example, an IDE can inform you of the number of call sites for a deprecated method, but how would you go about determining the percentage of time your application spends in deprecated methods?

like image 28
alphazero Avatar answered Oct 22 '22 06:10

alphazero