суббота, 1 августа 2020 г.

Прибавление 2-х чисел при помощи Аннотаций (Annotation)

import cf.tilgiz.annotation_test.handler.MathHandler;

import java.lang.reflect.Method;


public class Dispatcher {
public static void main(String[] args) throws NoSuchMethodException {
MathHandler mathHandler = new MathHandler();

Class<MathHandler> cl = MathHandler.class;
Method method = cl.getDeclaredMethod("addTwoNumbers", int.class, int.class);
MathAnno mathAnno = method.getAnnotation(MathAnno.class);

mathHandler.addTwoNumbers(mathAnno.num1(),mathAnno.num2());
}
}
package cf.tilgiz.annotation_test.handler;

import cf.tilgiz.annotation_test.annotation.MathAnno;

public class MathHandler {
@MathAnno(num1 = 10, num2 = 20)
public void addTwoNumbers(int num1, int num2) {
System.out.println(num1 + num2);
}
}
package cf.tilgiz.annotation_test.annotation;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MathAnno {
int num1() default 0;
int num2() default 0;

}