8 Jun 2018

Custom Annotation in Java

Create Custom Annotation

In java, creating an annotation is @interface is used to create an Annotation.
public @interface MyAnnotation{

}

We can also define methods inside an annotation.
public @interface MyAnnotation{
   int value();
}
Note: The methods of an annotation should adhere the following rules:
  1. Method declaration should not have any parameters
  2. Method declaration should not have any throws clause
  3. Method return type should be of primitives, String, Class, Enum, Annotations, and Arrays of the preceding types

Also, we can provide a default value for the methods inside an annotation.
public @interface MyAnnotation{
   int value() default 1;
}

Types of Annotations

Based on the number of methods inside an annotation, annotations are classified into 3 types:
  1. Marker Annotation
    An annotation without any methods is called as Marker Annotation.
       public @interface MyAnnotation{
    
       }
    
    @Override and @Deprecated are the best examples of marker annotation.

  2. Single - Value Annotation
    An annotation with only one method is called as Single - Value Annotation.
       public @interface MyAnnotation{
          int value();
       }
    

  3. Multi - Value Annotation
    An annotation with more than one method is called as Multi - Value Annotation.
       public @interface MyAnnotation{
          int value1();
          String value2();
       }
    

The following are the built-in annotations that can be used for Custom Annotations:
  1. @Target
    It is used to specify on which type the annotation is to be applied.

    The java.lang.annotation.ElementType enum provides many constants to specify the type of element where annotation is to be applied. Here's the list of constants of ElementTypoe enum:
    Element TypeWhere the annotation can be applied
    TYPEclass, interface or enumeration
    FIELDfields
    METHODmethods
    CONSTRUCTORconstructors
    LOCAL_VARIABLElocal variables
    ANNOTATION_TYPEannotation type
    PARAMETERparameter

  2. @Retention
    It is used to specify on which level annotation will be available.
    Retention PolicyAvailability
    RetentionPolicy.SOURCEIt is used to make the annotation to refer only to the source code and it will be discarded during compilation. So, it will not be available in the compiled .class file.
    RetentionPolicy.CLASSIt is used to make the annotation available to java compiler, but not to JVM. So, it is included in the compiled .class file.
    RetentionPolicy.RUNTIMEIt is used to make the annotation available at run-time. So it is included in the compiled .class file and at run-time.

  3. @Inherited
    By default, annotations are not inherited to subclasses. The @Inherited annotation marks the annotation to be inherited to subclasses.

  4. @Documented
    It is used to mark the annotation to be included in the documentation.

Now let us see an example how to Create and Apply a Custom Annotation.

MyAnnotation.java
   @Retention(RetentionPolicy.RUNTIME)  
   @Target(ElementType.METHOD)
   public @interface MyAnnotation{
      int value1() default 0;
      String value2() default "J2EEKART";
      boolean value3() default false;
   }
AnnotationTest.java
   class AnnotationTest{
      @MyAnnotation(value1 = 10, value2 = "Hi", value3 = true)
      public void display(){

      }
   }
Test.java
   import java.lang.reflect.Method;

   class Test{
      Test obj= new Test();
      Method m = obj.getClass().getMethod("display");
      MyAnnotation annot = m.getAnnotation(MyAnnotation.class);

      System.out.println(annot.value1());
      System.out.println(annot.value2());
      System.out.println(annot.value3());
   }
The output of the above example will be as:
   10
   Hi
   true

The above example shows how to create a Custom Annotation with default values and in-built annotations, apply an annotation on method level and read the values of an annotation by using java reflection.





Popular Posts

Write to Us
Name
Email
Message