Custom Annotation

Custom Annotation

Similar to interface we can define Custom Annotations or user defined annotations simply by adding @interface in the declaration. For example:

@interface CustomAnnotation{}

Here, CustomAnnotationis the custom annotation name.

While writing the custom annotations few points has to be considered.

  • Method should not have any throws clauses, Similar to interface only declarations available no implementation.
  • Method should return either primitive data types, String, Class, enum or array of these data types.
  • We can assign a default value to the method.

Custom Annotations with no methods is called as Marker Annotation.

@interface CustomAnnotation{}

The @Override and @Deprecated are marker annotations.

Custom Annotations may have multiple values also for example

@interface CustomAnnotation {
    String city();
    String street();
    int pin();
}
@CustomAnnotation(city = "Vijayawda",street="stpm", pin=520011)
public static void testAnnotation() {
       System.out.println("Custom annotation test");
}

Values has to be assigned to all elements which are declared in Annotation else compiler will throw error

The annotation @AnnotationsEx.CustomAnnotation must define the attribute street

You can have default values also.

@interface CustomAnnotation {
    String city() ;
    String street();
    int pin() default 123;
}
@CustomAnnotation(city = "Vijayawda",street="stpm")
public static void testAnnotation() {
      System.out.println("Custom annotation test");
}

 

Here I have used default value for pin element, so while using annotation no need to mention the element “pin”, it will take mentioned default value.

Built-in Annotations used in custom annotations in java

@Retention

It is used to specify to what level annotation will be available.

@Retention(RetentionPolicy.RUNTIME)

Which indicates to Java compiler and JVM that the annotation should be available at runtime.

@Retention(RetentionPolicy.CLASS)

This is the default retention policy, if you don’t mention any retention policy compiler will take this as a default one, which indicates that annotation is stored in the .class file, but not available at runtime.

@Retention(RetentionPolicy.SOURCE)

Which indicates that the annotation is only available in the source code, and not in the .class files and not a runtime.

@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
  String city() ;
  String street();
  int pin() default 123;
}

@Inherited

The @Inherited annotation indicates that a custom Java annotation used in a class should be inherited by their subclasses. By default, annotations are not inherited to subclasses.

java.lang.annotation.Inherited
@Inherited
public  @interface CustomAnnotation {

}
@CustomAnnotation
public  class SuperClass { ... }
public  class SubClass  extends SuperClass{ ... }

Here SubClass inherits the @CustomAnnotationannotation from SuperClass because we have annotated the CustomAnnotation with @inherited annotation.

@Documented

It indicates the compiler  for inclusion in the documentation i.e. your Custom Java annotation should in included in JavaDoc

java.lang.annotation.Documented
@Documented
public  @interface CustomAnnotation {

}

@Target

It is used to restrict what kind of Java elements the annotation can be applied to. A target annotation specifies one of the following element types as its value:

ElementType.ANNOTATION_TYPE can be applied to an annotation type.

ElementType.CONSTRUCTOR can be applied to a constructor.

ElementType.FIELD can be applied to a field or property.

ElementType.LOCAL_VARIABLE can be applied to a local variable.

ElementType.METHOD can be applied to a method-level annotation.

ElementType.PACKAGE can be applied to a package declaration.

ElementType.PARAMETER can be applied to the parameters of a method.

ElementType.TYPE can be applied to any element of a class.

The ANNOTATION_TYPE target means Java annotation definitions, i.e. used to annotate other annotations like the @Target and @Retention.

The TYPE target means any type (class, interface, enum or annotation).

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
public  @interface CustomAnnotation { ……}

 

2 thoughts on “Custom Annotation

Leave a Reply

Your email address will not be published. Required fields are marked *