Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Annotations - looking for an example of RetentionPolicy.CLASS

according to Java Annotation API:

  • RetentionPolicy.CLASS Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

  • RetentionPolicy.RUNTIME Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

I am looking for a sample of "CLASS" retention policy. when we need to use this policy instead of RUNTIME policy.

like image 809
mhshams Avatar asked Oct 03 '10 11:10

mhshams


People also ask

What is RetentionPolicy in Java?

Retention Policy: A retention policy determines at what point an annotation is discarded. It is s specified using Java's built-in annotations: @Retention [About] 1. SOURCE: annotation retained only in the source file and is discarded during compilation.

What is the use of @retention annotation?

Annotation Type RetentionIndicates how long annotations with the annotated type are to be retained.

What is Java annotation example?

Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.

What is the use of @interface annotation?

Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition. Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name.


1 Answers

CLASS Annotations are used in obfuscator tools like http://proguard.sourceforge.net . For example annotation @KeepName disables name mangling when you need to have your class name unchanged to be able to call methods like Class.forName().

like image 171
Marek Fort Avatar answered Sep 21 '22 11:09

Marek Fort