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 to the following rules:
- Method declaration should not have any parameters
- Method declaration should not have any throws clause
- 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:
- Marker Annotation
- Single – Value Annotation
- Multi – Value Annotation
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.
Single Value Annotation:
An annotation with only one method is called as single – Value Annotation.
public @interface MyAnnotation{ int value(); }
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:
- @Target
- @Retention
- @Inherited
- @Documented
@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 Type | Element Type |
---|---|
TYPE | class, interface or enumeration |
FIELD | fields |
METHOD | methods |
CONSTRUCTOR | constructors |
LOCAL_VARIABLE | local variables |
ANNOTATION_TYPE | annotation type |
PARAMETER | parameter |
@Retention
It is used to specify on which level annotation will be available.
Retention Policy | Availability |
---|---|
RetentionPolicy.SOURCE | It 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.CLASS | It is used to make the annotation available to java compiler, but not to JVM. So, it is included in the compiled .class file. |
RetentionPolicy.RUNTIME | It is used to make the annotation available at run-time. So it is included in the compiled .class file and at run-time. |
@Inherited
By default, annotations are not inherited to subclasses. The @Inherited annotation marks the annotation to be inherited to subclasses.
@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.
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation{ int value1() default 0; String value2() default "J2EEKART"; boolean value3() default false; }
class AnnotationTest{ @MyAnnotation(value1 = 10, value2 = "Hi", value3 = true) public void display(){ } }
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.
Leave a Reply