Tag: Custom Annotations in Java

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

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 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:

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.