Senin, 01 Desember 2014

Java Custom Annotation and Parsing using Reflection Tutorial with examples

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.

Java annotations were added to Java from Java 5 and now it’s heavily used in Java frameworks like Hibernate, Jersey, Spring.

Before annotations, program metadata was available through java comments or by javadoc but annotation offers more than that. It not only contains the metadata but it can made it available to runtime and annotation parsers can use it to determine the process flow. For example, in Jersey webservice we add PATH annotation with URI string to a method and at runtime jersey parses it to determine the method to invoke for given URI pattern.

Read here more about annotations in java.

Creating Custom Annotations in Java

Creating custom annotation is similar to writing an interface, except that it interface keyword is prefixed with @ symbol. We can declare methods in annotation. Let’s see annotation example.
package com.tutorialsdesk.annotations;

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


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
String author() default "TutorialsDesk";
int version() default 1;
}



Here , the RetentionPolicy.RUNTIME will determine whether the annotation will be present at run time or not.To test the annotation and get the values , let's create a test class
package com.tutorialsdesk.annotations;

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

public class CustomAnnotationTest {

@CustomAnnotation(author="TutorialsDesk",version=2)
public void annotationTest()
{

}


public static void main(String[] args) {
Method[] methods = CustomAnnotationTest.class.getDeclaredMethods();

for(Method m : methods)
{
for(Annotation a : m.getAnnotations())
{
if ( a instanceof CustomAnnotation)
{
CustomAnnotation ann = (CustomAnnotation)a;
System.out.println(ann.author());
System.out.println(ann.version());
}
}
}



}

}


Here annotation is on method level , we can access the annotation element values using reflection on method.If the do not specify RetentionPolicy as RUNTIME , the jvm will ignore this annotation and we will not be able to access this annotation.

Points to remember while writing Custom Annotations in Java

  • Annotation methods can’t have parameters.
  • Annotation methods return types are limited to primitives, String, Enums, Annotation or array of these.
  • Annotation methods can have default values.
  • Annotations can have meta annotations attached to them. Meta annotations are used to provide information about the annotation.
you can read here in detail about Java Built-in Annotations.

NEXT READ Why should we use @override Annotations in Java

Java Custom Annotation and Parsing using Reflection Tutorial with examples
Hope we are able to explain you Java Custom Annotation and Parsing using Reflection, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).

Please share us on social media if you like the tutorial.

Source:http://www.tutorialsdesk.com/2014/10/java-custom-annotation-and-parsing.html

Tidak ada komentar:

Posting Komentar