How to create your own custom annotations in java and how to use it..PART-II

Hi in my previous post i have explained how to approach and understand over custom annotations using java, taking the support of meta annotations already given by java. Now i explain you how to code your own annotations. In my example i would like to explain how to inject object based on annotation.

Now i write a custom annotation “@AutoInject”, this annotation should be annotated on methods which will inject the object of particular class type as shown below

private EmployeeDTO employeeDTO;

public EmployeeDTO getEmployeeDTO() {
return employeeDTO;
}

@AutoInject
public void setEmployeeDTO(EmployeeDTO employeeDTO) {
this.employeeDTO = employeeDTO;
}

Now this code to get executed first we need to design our annotation i.e. @AutoInject as shown below:

/**
*
*/
package com.spark.custom.annotations;

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

/**
* @author Mantha Pavan Kumar
* injects the object to a getter method
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoInject {

}

Now to process the annotation we have to write a class which will actually give the annotation behavior as below.

/**
*
*/
package com.spark.custom.annotations;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
* @author Mantha Pavan Kumar
*
*/
public class GenericAnnotations {
/**
* @param obj
* @return
*/
public static <T> T injectDependencies(final T obj) {
try {
Class clazz = obj.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);

for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
Method writeMethod = pd.getWriteMethod();

if (writeMethod == null) {
continue;
}

Object existingVal = pd.getReadMethod().invoke(obj);
if (existingVal == null) {
for (Annotation annotation : writeMethod.getAnnotations()) {
if (annotation instanceof AutoInject) {
Class propertyType = pd.getPropertyType();
writeMethod.invoke(obj, propertyType.newInstance());
}
}
}
}
} catch (Exception e) {
// do something intelligent :)
}
return obj;
}

}

in this way you can code your own functional behavior of annotations, Happy Coding πŸ™‚ please find the below file for the entire code.
CustomAnnotations

Advertisements

How to create your own custom annotations in java and how to use it..PART-I

Hi All
Recently i have a thought why cant i create a custom annotations and use it, and below is the solution for it.

Lets look at what are annotations:

Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.Annotations have a number of uses, among them:

Information for the compiler β€” Annotations can be used by the compiler to detect errors or suppress warnings.
Compiler-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.

Annotations can be applied to a program’s declarations of classes, fields, methods, and other program elements.

Annotation Structure

There are two main components in annotations. First is annotation type and the next is the annotation itself which we use in the code to add meaning. Every annotation belongs to a annotation type.

Annotation Type:

public @interface {
method declaration;
}
eg: public @interface AutoInject{
public injectObject() default "";
}

Annotation type is very similar to an interface with little difference.

We attach β€˜@’ just before interface keyword.
Methods will not have parameters.
Methods will not have throws clause.
Method return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.
We can assign a default value to method.

There are specific annotations which can only be used in the context of annotations. The annotations are target, retention, documented and inherited.

Meta Annotations

Annotations itself is meta information then what is meta annotations? As you have rightly guessed, it is information about annotation. When we annotate a annotation type then it is called meta annotation. For example, we say that this annotation can be used only for methods.

@Target(ElementType.METHOD)
public @interface MethodInfo { }

Annotation Types:
Documented: When a annotation type is annotated with @Documented then wherever this annotation is used those elements should be documented using Javadoc tool.

Inherited: This meta annotation denotes that the annotation type can be inherited from super class. When a class is annotated with annotation of type that is annotated with Inherited, then its super class will be queried till a matching annotation is found.

Retention: This meta annotation denotes the level till which this annotation will be carried. When an annotation type is annotated with meta annotation Retention, RetentionPolicy has three possible values:

@Retention(RetentionPolicy.RUNTIME)
public @interface Developer {
String value();
}

a.Class: When the annotation value is given as β€˜class’ then this annotation will be compiled and included in the class file.
b.Runtime: The value name itself says, when the retention value is β€˜Runtime’ this annotation will be available in JVM at runtime. We can write custom code using reflection package and parse the annotation. I have give an example below.
c.Source: This annotation will be removed at compile time and will not be available at compiled class.

Target:This meta annotation says that this annotation type is applicable for only the element (ElementType) listed. Possible values for ElementType are, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE.

in my next post i will explain how to code custom annotations and use them
Happy Coding πŸ™‚