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
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.
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.
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.
Annotation Type RetentionIndicates how long annotations with the annotated type are to be retained.
@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 {
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:
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.
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