Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

Custom Annotation in Java

Posted on March 3, 2018
In this post I will explain how to write your own custom annotation in Java.

Annotations are really great. They explain a lot about your method, field or class. For example if you annotate a method as @Test, it will convey the information to Junit to run it as a test method. @Override annotation makes the compiler to check that if the method presents in the super class or not. In fact it may help you to resolve some silly mistakes. In this post I will explain how to write your own custom annotation.


Suppose we have simple class Car as below.


class Car{
private String chassisNumber;
private String engineNumber;
private String ownerName;

public String getChassisNumber(){ return chassisNumber;}
public String getEngineNumber() { return engineNumber;}
public String getOwnerName() { return ownerName;}
}

We can define an annotation as below to check if the car is Bmw or not.


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Bmw{

}

This is the simplest annotation possible. Here the @Target defines where this annotation is applicable. Is it in a method or a class or a Field. Since we have defined the Target as type, this annotation is applicable to any type.


@Retention defines a retention policy which lets the JVM know till what time the annotation needs to be retained. For example the @Override annotation is not required after compilation is Completed. So, @Override has a retention policy of SOURCE.


But since our annotation will be required at run time to check if a particular car is Bmw or not, we have put the retention policy as RUNTIME.


With our annotation, we can also provide more information like what is the launch year and series of the Bmw as below.


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Bmw{
String launchYear();
String series();
}

We can now use our annotation as shown below.

@Bmw(launchYear="2015",series="X6")
class Car{
private String chassisNumber;
private String engineNumber;
private String ownerName;

public String getChassisNumber(){ return chassisNumber;}
public String getEngineNumber() { return engineNumber;}
public String getOwnerName() { return ownerName;}
}

Check out the launchYear and series value provided along with the annotation to provide more info about the Bmw.


We can also provide default values for those attributes in the annotation itself as below, so that if you do not need those values, you don't have to provide them.


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Bmw{
String launchYear() default "2000";
String series() default "default-series";
}


We are now done with the definition and application with our annotation. Now it's our turn to check at runtime if a car is Bmw or not. Let's have a look at the below code.


class BmwChecker{

private final Car car;
public BmwChecker(Car car){
this.car = car;
}

public void run(){
Class c = car.getClass();
Annotation an = c.getAnnotation(Bmw.class);
if(an instanceof Bmw){
System.out.println("It's a Bmw");
System.out.println("launch Year: " + ((Bmw)an).launchYear() + " and series: " +
((Bmw)an).series());
}

}

public static void main(String [] args){
new BmwChecker(new Car()).run();
}
}


We have got the annotation from the Car object using reflection and printed out the launch year and series in case the Car is a Bmw.


This is a simple introduction to Java annotation. Leave your queries in the comment section.


Sharing is Caring!

Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

GET FREE UPDATES


profile image

Kaushik Baruah


ABOUT

My name is Kaushik Baruah and I am the chief blogger on this Blog. I have developed this Blog from scratch using Django as the backend and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects. You can follow me on Twitter, Facebook and Google+

GET FREE UPDATES

POPULAR POSTS

Copyright © 2018
About Us

My name is Kaushik Baruah and I am the chief blogger on this Blog. I have developed this Blog from scratch using Django as the backend and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects.

Get Free Updates