Java Custom Annotation Example

This section will describe you how to create custom annotation in Java.

Java Custom Annotation Example

This section will describe you how to create custom annotation in Java.

Java Custom Annotation Example

Java Custom Annotation Example

In this tutorial we will learn about the annotation in Java and how it can be created.

Annotations in Java contains the information i.e. data, can be called metadata, of a program. Annotations supplies the metadata (apart from the program itself) to a program.

Uses Of Annotations :

Annotations are useful in various ways. Some of them are as follows :

  • Annotations can be used for providing information to the compiler for detecting errors, or more purposes.
  • Annotations can be used by the software tools for generating code, XML files etc at compile time or runtime.
  • Annotations can be examined at runtime.

Syntax For Defining Annotation

@interface annotationName {
method declaration;
}

Types Of Annotation

Following are the annotation types. These types of annotations are applied on the other annotations, called meta-annotations. Java has provided the corresponding built-in annotations for meta-annotations.

  • Documented : This type of annotation marks the another annotation to include in the documentation and documented using the Javadoc tool. @Documented annotation is the built-in meta annotation for the Documented type annotation.
     
  • Inherited : This type of annotation marks the another annotation that this type of annotation can be inherited from the parent class. Annotations can't be inherited by default. @Inherited annotation is the built-in meta annotation for the Inherit type annotation.
     
  • Retention : This type of annotation marks the another annotation that the level till which it will be carried. @Retention annotation is the built-in meta annotation for the Retention type annotation. To specify the carried level of annotation @Retention annotation may be specified by the following possible three retention policies :
     
    • RetentionPolicy.SOURCE : The annotation marked with the source retention policy is only carried at the source level and is ignored by the compiler.
       
    • RetentionPolicy.CLASS : The annotation marked with the class retention policy is carried till the compiler at compile time but, is ignored by the JVM.
       
    • RetentionPolicy.RUNTIME : The annotation marked with the runtime retention policy is carried till the JVM and is used by the runtime environment.
       
  • Target : This type of annotation marks the another annotation to restrict the use of type of Java elements. @Target annotation is the built-in meta annotation for the Target type annotation. A @Target annotation can specify the following element types as its value :
     
    • ElementType.ANNOTATION_TYPE
    • ElementType.CONSTRUCTOR
    • ElementType.FIELD
    • ElementType.LOCAL_VARIABLE
    • ElementType.METHOD
    • ElementType.PACKAGE
    • ElementType.PARAMETER
    • ElementType.TYPE
       
  • Repeatable : Part of Java SE 8. This type of annotation marks the another annotation that the such annotation can be repeated to the same declaration or type use. @Repeatable annotation is the built-in meta annotation for the Repeatable type annoation.

Some more built-in annotations are as follows :

  • @Override
  • @Deprecated
  • @SuppressWarnings
  • @SafeVarargs
  • @FunctionalInterface (Introduced in Java SE 8)

Example

Here I am giving a simple example which will demonstrate you about how a custom annotation can be created. In this example we will first define an annotation named RunAnnotation and we will defined its target and retention using built-in meta annotations @Target and @Retention respectively. Then we will create a class to apply the annotation and then we will create a main class to execute the application.

RunAnnotation.java

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

@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface RunAnnotation {

} 

AnnotationRunner.java

public class AnnotationRunner {

    @RunAnnotation
    public void method1() {
        System.out.println("Annotated method1 called.");
    }

    @RunAnnotation
    public void method2() {
        System.out.println("Annotated method2 called.");
    }

    public void method3() {
        System.out.println("Method withoud annotation");
    }  
}

TestAnnotation.java

import java.lang.reflect.Method;

public class TestAnnotation {

    public static void main(String[] args) {

        AnnotationRunner runner = new AnnotationRunner();
        Method[] methods = runner.getClass().getMethods();

        for (Method method : methods) {
            RunAnnotation run = method.getAnnotation(RunAnnotation.class);
            if (run != null) {
                try {
                    method.invoke(runner);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
} 

Output

When you will compile and execute the TestAnnotation.java file then the output will be as follows :

Download Source Code