May 21, 2019

Custom annotation in Java with examples

Custom annotation in Java using Tax annotation


Here we demonstrate how you can create a custom annotation. Creating a custom annotation is very easy. You can use custom annotation in places where you are developing your own services.


The complete code will be available in the following link

Our git hub repository : Konzernites

Custom annotation interface example

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD })
public @interface Tax {
public String name();
public double amount();
public TaxMode mode() default TaxMode.PERCENTAGE;
public TaxTypes type() default TaxTypes.INCLUSIVE;
}



 What we did here?

we declared a public custom annotation named Tax using @inteface keyword

public @interface Tax

Next we added the scope and visibility using meta annotation

@Retention &  @Target is used



ElementType.TYPE implies the annotation can be applied on a class
ElementType.FIELD implies the annotation can be applied on a field
ElementType.METHOD implies the annotation can be applied on a method

We then wrote a price Modification class which works on reflection in java ( Java Reflection API)
where in run time we check the whether the annotation is added on the fields or method of a class and corresponding computation is calculated.

Below is an Insurance class where we applied the custom annotation we created.

@author steffi thomas
public class Insurance {
private String name;
private double supplierPrice;
private double maketPrice;
private double basePrice;
private double profit;
private double commision;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the supplierPrice
*/
public double getSupplierPrice() {
return supplierPrice;
}

/**
* @param supplierPrice
*            the supplierPrice to set
*/

public void setSupplierPrice(double supplierPrice) {
this.supplierPrice = supplierPrice;
}

/**
* @return the maketPrice
*/
@Tax(name = "VAT", amount = 5, mode = TaxMode.PERCENTAGE, type = TaxTypes.INCLUSIVE)
public double getMaketPrice() {
return maketPrice;
}

/**
* @param maketPrice
*            the maketPrice to set
*/
public void setMaketPrice(double maketPrice) {
this.maketPrice = maketPrice;
}

/**
* @return the basePrice
*/
@Tax(name = "VAT", amount = 5, mode = TaxMode.PERCENTAGE, type = TaxTypes.INCLUSIVE)
public double getBasePrice() {
return basePrice;
}

/**
* @param basePrice
*            the basePrice to set
*/
public void setBasePrice(double basePrice) {
this.basePrice = basePrice;
}

/**
* @return the profit
*/
@Tax(name = "VAT", amount = 100, mode = TaxMode.AMOUNT, type = TaxTypes.EXCLUSIVE)
public double getProfit() {
return profit;
}

/**
* @param profit
*            the profit to set
*/
public void setProfit(double profit) {
this.profit = profit;
}

/**
* @return the commision
*/
@Tax(name = "VAT", amount = 1, mode = TaxMode.PERCENTAGE, type = TaxTypes.EXCLUSIVE)
public double getCommision() {
return commision;
}

/**
* @param commision
*            the commision to set
*/
public void setCommision(double commision) {
this.commision = commision;
}

}


While processing the annotation we used the reflection API as we mentioned above, please refer the PriceModification.Java class to know how it works


Thanks 

Facebook comments