Annotation Concept Simplified in Java



Annotation is known as metadata for a code. We can use this feature to merge additional java elements with programming code like in classes,methods,parameters,local variables,packages,fields etc. Annotations viz metadata are stored in java class file(source file)by the compiler.These class files are used by the JVM or by the program to find out the metadata for interacting with the code elements.Java.lang.annotation package contains all annotation classes.The classes that implement Annotation element interface are:

  • java.lang.reflect.accesibleObject
  • java.lang. class
  • java.lang.reflect.Constructor
  • java.lang.reflect.field
  • java.lang.package
  • java.lang.reflect.method
Standard Annotation:

Annotation Comments
@Depreciated Compiler warns when deprecated java elements are used in non deprecated program or context.
@Overrides Compiler generated error when the method uses this annotation type does not override the methods present in the super class
@Documented Indicates annotation of this type to be documented by javadoc
@Inherited Indicates that this type is automatically inherited
@Retention Indicates the extended period using annotation type.
@Target Indicates to which program element the annotation is applicable

The annotation declaration is similar to interface declaration. we need to use @<annotation name> or keyword and we need to use @before keyword interface.


package myPackage.annotation;
import java.lang.annotation*;
@Retention(Retentionpolicy.RUNTIME);
@Target(ElementType.METHOD)
public @interface MyTest{
    Integer calculate();
}
 
//Meta annotations which declares that the @MyTest annotations must be stored in a class file.
@Target meta annotation is used to declare the @MyTest annotation which annotates the methods in the java class file.
@interface meta annotation is used to declare the @MyTest annotation with the member called calculate which returns Integer as an object.

Generic Guideline to Create Annotation:

  • Do not use extends clause.It automatically extends the market interface java.lang.annotation.Annotation.
  • Do not use any parameter for method
  • Do not use generic methods
  • Do not use throws clause
Annotations are majorly used to test the class,method,variable etc.

import junit.annotation.*;
public class TestMyMethods{
    @UnitTest(value="Test positive")
    public void positiveTest(int no)
  {
        assert no>0;
   }
@UnitTest(value="Test negative")
    public void negativeTest(int no)
 {
        assert no<0
 }
We can merge annotation with code elements.We can also use the methods available in the marker interface,java.lang.reflect.Annotated element query about the existence of programming element and get their values.
The methods of the Annotated Element interface are:

  1. IsAnnotationPresent();
  2. getAnnotations();
  3. getAnnotation();
  4. getDectaredAnnotations()
Use of Annotation in real code:


import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@interface MySingle{
int value();
}
public class Single{
//annotate a method using a marker
@MySingle(100)
public static void myMethod(){
Single ob=new Single();
try{
Method m=ob.getClass().getMethod("myMethod");
MySingle assno=m.getAnnotation(MySingle.class);
System.out.println("The value is"+assno.value());//displays 100
}
catch(NoSuchMethodException e)
{
System.out.println("Metod not found");
}
}
public static void main(String args[])
{
MyMethod();
}
}

 

Annotation Concept Simplified in Java Annotation Concept Simplified in Java Reviewed by Animesh Chatterjee on November 27, 2018 Rating: 5

No comments:

Powered by Blogger.