Wednesday, December 9, 2015

Annotation in Java

Hello guys, today we will try to learn something about the Annotations.

Annotation:
    "An annotation is a metadata (e.g. a comment, explanation, presentational markup) attached to text, image, or other data.Often, annotations make reference to a specific part of the original data." - Wiki

In other words, it is a kind of code that gives the user a way to pass some data from its code to the developer of the annotation who have given annotations to use in the code so that he knows that the user wants to do what.

Oh my God, what have I read there? :O

Well let us take an example like we have to get the data from the database. So for that we :
  1. Have to create a connection 
  2. Open a connection
  3. Get the data considering the exceptions and all
  4. After getting the data, close the connection
  5. Release the memory
Now these 5 steps are going to be happened all the times whenever we have to get the data from the database.

These type of codes which are to be executed number of times are termed as boiler plate codes. And in order to get rid of boiler plate codes Java have introduced the concept of Annotation.

Now the annotation is a very useful feature. If you have used the Spring, Hibernate or any framework you would have gone through it.

Lets take an example of reading and making the annotation in Java.

You can get  the project from here.

Its basically a simple project that works in the following way:
  1. We first create an annotation that have the properties which would be available (TestAnnotation.java).
  2. Then I have created the Java class where I used the annotation created in step no: 1(TestClassWithAnnotationUsed.java).
  3. And lastly I have coded the class that will read all the functions that used our annotation(ClassThatReadsAnnotation.java)
TestAnnotation.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestAnnotation {
 public String name() default "";
}

ClassThatReadsAnnotation.java
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import com.av.demo.annotation.TestAnnotation;

public class ClassThatReadsAnnotation {

 
 public static void main(String[] args) {
  System.out.println("Lets first create a normal object of the TestClassWithAnnotationUsed class");
  TestClassWithAnnotationUsed obj = new TestClassWithAnnotationUsed();
  System.out.println("Now call the method and see whether it works or not");
  obj.functionWithAnnotationApplied_1();
  System.out.println("Now we read the annotation and try to get the value that is passed in for the string. "
    + "\nThis is usually done by reading the annotation in the runtime. "
    + "\nAnd this is done by the concept called Reflection of Java. "
    + "\nBy the we can actually manipulate the classes and objects in runtime, "
    + "\nwhich is internally by the apis like spring and hibernate. ");
  
  //Getting the reference of the class Object
  Class<Testclasswithannotationused> testClassWithAnnotationUsedClass = TestClassWithAnnotationUsed.class;
  
  for (Method method : testClassWithAnnotationUsedClass.getDeclaredMethods()) {
   //Checking for the presence of the Annotation we have made
   if(method.isAnnotationPresent(TestAnnotation.class)){
    Annotation annotation = method.getAnnotation(TestAnnotation.class);
    TestAnnotation test = (TestAnnotation) annotation;
    System.out.println(test.name());
   }
  }
  
 }
 
}

TestClassWithAnnotationUsed.java
import com.av.demo.annotation.TestAnnotation;

public class TestClassWithAnnotationUsed {
 
 @TestAnnotation(name="ACCEPTED_VALUE")
 public void functionWithAnnotationApplied_1(){
  System.out.println("I have applied the annotation TestAnnotation with value ACCEPTED_VALUE");
 }
 
 
 @TestAnnotation(name="NON_ACCEPTED_VALUE")
 public void functionWithAnnotationApplied_2(){
  System.out.println("I have applied the annotation TestAnnotation with value NON_ACCEPTED_VALUE");
 }
}

No comments:

Post a Comment