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
// Java Program to Illustrate Documented Annotations // Without using @Documented annotation // Creating a single value custom annotation @interface CustomAnnotation { String value(); } @CustomAnnotation ( "GFG" ) public class DocumentedAnnotationDemo { public static void main(String[] args) { System.out.println( "This is the main method" ); } } |
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.
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
// Java Program to Illustrate Documented Annotations // With using @Documented annotation // Importing the Documented annotation import java.lang.annotation.Documented; // Creating a single value custom annotation // which is annotated using @Documented // annotation @Documented @interface CustomAnnotation { String value(); } // This annotation will be documented @CustomAnnotation ( "GFG" ) // Main class public class GFG { // Main driver method public static void main(String[] args) { // Print and display statement on the console System.out.println( "This is the main function" ); } } |
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.