Pages

Showing posts with label Annotations. Show all posts
Showing posts with label Annotations. Show all posts

Wednesday, 27 July 2016

Create an annotation in Java

CustomAnnotation.java
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
     int studentAge() default 21;
     String studentName();
     String stuAddress();
     String stuStream() default "CS";
}

How to use the field of Annotation in Java?

TestCustomAnnotation.java
package annotations;
import java.lang.reflect.Method;
public class TestCustomAnnotation {
     public static void main(String[] args) {
           newTestCustomAnnotation().testAnnotation();
     }
     @CustomAnnotation(
                studentName="Rajesh",
                stuAddress="Mathura, India"
     )
     public voidtestAnnotation() {
           try {
                Class<? extends TestCustomAnnotation> cls = this.getClass();
                Method method = cls.getMethod("testAnnotation");
               
                CustomAnnotation myAnno = method.getAnnotation(CustomAnnotation.class);
               
                System.out.println("Name: "+myAnno.studentName());
                System.out.println("Address: "+myAnno.stuAddress());
                System.out.println("Age: "+myAnno.studentAge());
                System.out.println("Stream: "+myAnno.stuStream());
               
           } catch(NoSuchMethodException e) {
           }
     }
}
Output:
Name: Rajesh
Address: Mathura, India
Age: 21
Stream: CS

Java Annotations

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

Uses of Annotations:
Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
Runtime processing — some annotations are available to be examined at runtime.

Example of Java annotations:
To describe constraints or usage of an element: e.g. @Deprecated, @Override, or @NotNull.
To describe the "nature" of an element: e.g. @Entity, @TestCase, @WebService.
To describe the behavior of an element: @Statefull, @Transaction.
To describe how to process the element:  @Column, @XmlElement.

In all the above cases, annotations are used to describe the element and clarify its meaning.

Note:
Prior to JDK5, information that is now expressed with annotations needed to be stored somewhere else, and XML files were frequently used. However it is more convenient to use annotations because they will belong to the Java code itself, and are hence much easier to manipulate than XML.

Advantages of the annotation:
1) All the information is in a single file (no need to open two files to configure a given behavior).
2) When the class changes, no need to modify the xml file.