July 10, 2018

My first custom annotation in java


package com.belazy.annotations;

import java.lang.reflect.Method;

public class Test {

public static void main(String[] args) {
Test t = new Test();
t.calcuate();
try {
Method method = new Test().getClass().getMethod("calcuate");
tax annotation = method.getAnnotation(tax.class);
System.out.println(annotation.type());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@tax(type="vat", value="5",mode ="percentage")
public void calcuate() {
// TODO Auto-generated method stub
System.out.println("calculate");

}

}


package com.belazy.annotations;

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

@Retention(RetentionPolicy.RUNTIME)
public @interface tax {
   public String type ();
   public String value ();
   public String mode();
   }



://javabelazy.blogspot.in/

Facebook comments