Custom Annotation with Example

 

Annotations are only metadata and they do not contain any business logic. Prior to annotation introduced XML were used for metadata and somehow XML maintenance was getting troublesome. So something we need very loosely coupled from code and then annotations came into picture.
Custom Annotation with Example

Java by default provides built-in annotations like @Override, @Deprecated, @SuppressWarnings, @SafeVarargs and @FunctionalInterface.

  • @Override  - Used when overriding a method from super class. 
  • @Deprecated - Used to indicate a method is deprecated. When we use this annotation recommendedto provide information for why particular method deprecated and alternative solution to use it. 
  • @SuppressWarnings - Used to ignore specific warnings shown in code. 
  • @SafeVarargs - Suppress warnings for all callers of a method or constructor with a generics varargs parameter, since Java 7.
  • @FunctionalInterface - Specifies that the type declaration is intended to be a functional interface, since Java 8.

Now lets see simple example to write custom annotation which is as simple as writing interfaces. Just we need to prefix @ symbol to "interface" keyword. Some of the properties of annotation class must follow are

There a 5 types of meta annotations which gives information about the annotation which we create and they are @Documented, @Target, @Inherited, @Retention and @Repeatable

  • @Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at run-time through reflection. Various Retention policies like
    1. RetentionPolicy.CLASS (Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.)
    2. RetentionPolicy.RUNTIME (Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.)
    3. RetentionPolicy.SOURCE (Annotations are to be discarded by the compiler.)
  • @Documented - Marks another annotation for inclusion in the documentation.
  • @Target - Marks another annotation to restrict what kind of Java elements the annotation may be applied to. Various ElementTypes like 
    1. ElementType.TYPE (class, interface, enum)
    2. ElementType.TYPE_USE (Since 1.8)
    3. ElementType.TYPE_PARAMETER (Since 1.8)
    4. ElementType.FIELD (instance variable)
    5. ElementType.METHOD
    6. ElementType.PARAMETER
    7. ElementType.CONSTRUCTOR
    8. ElementType.LOCAL_VARIABLE
    9. ElementType.ANNOTATION_TYPE (on another annotation)
    10. ElementType.PACKAGE 
  • @Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).
  • @Repeatable - Specifies that the annotation can be applied more than once to the same declaration, since Java 8.

Simple annotation example:

MethodInfo.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo{
    String createdBy() default "Anand";
    String createdDate();
    String comments();
}



AnnotationTest.java

public class AnnotationTest {

 @MethodInfo(createdBy = "Anand", createdDate = "20th Nov 2015", comments = "Just saying Hello :)")
 public void sayHello(String name){
  System.out.println("Hello "+name);
 }
 
 public static void main(String[] args) {

  new AnnotationTest().sayHello("Java Discover");
  
 }
}


No comments:
Write comments