Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Documented annotation in java

What's the purpose of @Documented annotation in java?

I saw the documentation, but could not get much from it. Can someone point out with the help of an clear example

like image 954
praveenkjvs Avatar asked Apr 08 '11 08:04

praveenkjvs


People also ask

Is @documented meta annotation?

The @Documented is a type of marker meta annotation that is used to annotate the declaration of an annotation type. If an annotation type is declared with @Documented annotation then the Javadoc tool will generate documentation for all of its instances of annotation type.

What is documented annotation in Java?

Annotation Type DocumentedIndicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients.

What is @target annotation in Java?

If an @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7. 4. For example, this @Target meta-annotation indicates that the declared type is itself a meta-annotation type.

What is the use of @retention annotation?

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


2 Answers

@Documented is a meta-annotation. You apply @Documented when defining an annotation, to ensure that classes using your annotation show this in their generated JavaDoc. I've not seen much use of it, but there is an example here. An earlier question suggests that it doesn't work automatically in Eclipse, but I've tested in Eclipse 3.6, and my annotations appear in the JavaDoc popups whether or not I attach the @Documented annotation to them.

Here's an example from Spring, which ensures that transactional methods are marked as such in the JavaDoc:

@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Transactional { 
like image 144
Adrian Cox Avatar answered Oct 20 '22 21:10

Adrian Cox


If some our annotation (for example, @InWork) is @Documented, then for every class having that @InWork annotation the text generated by javadoc will contain @InWork text, as a reference to the annotation.

Annotation:

@Documented @Inherited  // for descenders of the annotation to have the @Documented feature automatically @Retention(RetentionPolicy.RUNTIME) // must be there public @interface InWork {     String value(); } 

Annotated target:

/**  * Annotated class.  */ @InWork(value = "") public class MainApp {...} 

The javadoc text:

enter image description here

So, you have to decide, if the annotation should be shown in the javadoc text, and if yes, set @Documented to it.

The information above is taken from Oracle documentation.


Please, notice, that in Eclipse you'll see in javadoc generated text ALL annotations, are they @Documented, or not.

It is still correct for 4.3 version.

like image 24
Gangnus Avatar answered Oct 20 '22 21:10

Gangnus