Java @Documented Annotations
By default, Java annotations are not shown in the documentation created using the Javadoc tool. To ensure that our custom annotations are shown in the documentation, we use @Documented annotation to annotate our custom annotations. @Documented is a meta-annotation (an annotation applied to other annotations) provided in java.lang.annotation package.
Cases:
- Without using @Documented annotation
- Using @Documented annotation
Let us discuss both the scenario to a certain depth.
Case 1: Without using @Documented annotation
In the code example shown below, we have created a custom annotation named CustomAnnotation. After that, we have annotated our class named DocumentedAnnotationDemo with it. Finally, we created documentation using the Javadoc tool. The syntax to use the Javadoc utility on the command prompt is mentioned below.
javadoc NameOfClassFile.java
Example
Java
@interface CustomAnnotation {
String value();
}
@CustomAnnotation ( "GFG" )
public class DocumentedAnnotationDemo {
public static void main(String[] args) {
System.out.println( "This is the main method" );
}
}
|
Output
This is the main method
When we create documentation of the code example shown above, the custom annotation used to annotate our DocumentedAnnotationDemo class is not shown in the documentation and depicted in the snapshot shown below.
Documentation without using @Documented annotation
Case 2: Using @Documented annotation
In the code example shown below, we have again created the same custom annotation named CustomAnnotation, but in this case, we used @Documented to annotate our custom annotation. After that, we have annotated our class named DocumentedAnnotationDemo with it. Finally, we created documentation using the Javadoc tool.
Example
Java
import java.lang.annotation.Documented;
@Documented @interface CustomAnnotation { String value(); }
@CustomAnnotation ( "GFG" )
public class GFG {
public static void main(String[] args)
{
System.out.println( "This is the main function" );
}
}
|
Output
This is the main function
When we now create documentation of the code example shown above, the custom annotation used to annotate our DocumentedAnnotationDemo class is shown in the documentation due to the use of @Documented annotation while creating it. A snapshot of the documentation created in this case is shown below.
Documentation using @Documented annotation
Last Updated :
07 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...