How Custom Annotaion In Java Works

Introduction

 
In this article, we discuss custom annotation in Java.
 

What is Custom Annotation?

 
Custom Annotation creation is similar to writing an interface, but it differs in the implementation since it uses a prefix, the "@" symbol. We can declare methods in annotations. We can create the user-defined annotation also. The @interface elements is used to declare an annotation.
 
Syntax
 
@interface OurAnnotation{}
 

Some points to remember

 
Programmers must remember some points about annotations; they are:
  1. Methods don't contain parameters.
  2. Methods don't contain any throws clauses.
  3. Methods must return one of the following: primitive data types, a Class, String, enum or array.
  4. We should prefix @ just before the interface keyword to define an annotation.
  5. It may assign a default value to the method.

Types of Annotations

 
There are basically three types of annotations, they are:
  1. Single-Value
  2. Multi-Value
  3. Marker Annotation

1. Marker Annotation

 
When no methods are used in an annotation, it is called a marker annotation. For example:
 
@interface OurAnnotation{}
 

2. Single-Value Annotation

 
As the name indicates, it contains only one method. For example:
  1. @interface OurAnnotation {  
  2.  int val() default ();  
  3. }  
  4. We can provide the  
  5. default value also.For example: @interface OurAnnotation {  
  6.  int val() default 0;  
  7. }  

How to apply a Single-Value Annotation?

 
Let's see the following code to apply the single value annotation.
 
@OurAnnotation (val=20)
 
The value can be anything.
 

3. Multi-Value Annotation

 
According to its name, the annotation that has more than one method is called Multi-Value annotation. For example:
  1. @interface OurAnnotation {  
  2.  int val1();  
  3.  String val2();  
  4.  String val3();  
  5. }  
  6. }  
We can provide the default value also. For example:
 
@interface OurAnnotation
  1. {  
  2.  int val1() default 1;  
  3.  String val2() default "";  
  4.  String val3() default "abc";  
  5. }  

How to apply Multi-Value Annotation?

 
Let's see the following code to apply a multi-value annotation.
 
@OurAnnotation (val1=20, val2="Jaun Pol", val3="Noida")
 

Built-In-Annotation

 
Built-In-Annotations applied to other annotations (Custom annotations) are:
  • @Inherited
  • @Documented
  • @Target
  • @retention

@Target

 
This tag specifies which type is to be used.
 
The "java.lang.annotation.ElementType" enum declares many constants to specify the type of element for annotation, such as TYPE, METHOD, FIELD, etcetera. Let's see the constants of the ElementType enum.
 
Element Types  Where the annotation can be applied
TYPE class, interface or enumeration
FIELD fields
METHOD methods
CONSTRUCTOR constructors
LOCAL_VARIABLE local variables
ANNOTATION_TYPE annotation type
PARAMETER parameter
 
Example
 
In this example, we specify annotation for a class:
  1. @Target(ElementType.TYPE) @interface OurAnnotation {  
  2.  int val1();  
  3.  String val2();  
  4. }  
Example
 
In this example, we specify an annotation for methods, fields or classes.
  1. @Target({  
  2.  ElementType.METHOD,  
  3.  ElementType.TYPE,  
  4.  ElementType.FIELD  
  5. }) @interface OurAnnotation {  
  6.  int val1();  
  7.  String val2();  
  8. }  

@Ratention

 
This annotation is used to specify what level of annotation will be available.
 
RetentionPolicy Availability
RetentionPolicy.SOURCE refers to the source code, discarded during compilation. It will not be available in the compiled class.
RetentionPolicy.CLASS refers to the .classfile, available to the Java compiler but not to JVM. It is included in the class file
RetentionPolicy.RUNTIME refers to the runtime, available to the Java compiler and JVM.
 
Example
 
This example shows how to specify RetentionPolicy:
  1. @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @interface OurAnnotation {  
  2.  int val1();  
  3.  String val2()  
  4. }  
Example
 
In this example, we use a custom annotation; creating, applying and accessing an annotation:
  1. import java.lang.reflect.*;  
  2. import java.lang.annotation.*;  
  3. @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @interface OurAnnotation {  
  4.  int val();  
  5. }  
  6. class Parent {  
  7.  @OurAnnotation(val = 20)  
  8.  public void sayParent() {  
  9.   System.out.println("Parent annotation");  
  10.  }  
  11. }  
  12. class CustomAnnEx {  
  13.  public static void main(String args[]) throws Exception {  
  14.   Parent p = new Parent();  
  15.   Method m = p.getClass().getMethod("sayParent");  
  16.   OurAnnotation oanno = m.getAnnotation(OurAnnotation.class);  
  17.   System.out.println("value passed is: " + oanno.val());  
  18.  }  
  19. }   
Output
Custom annotation.jpg