HomeWork 1.1
package com.gmail.vhrushyn;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface Test {
int valueOne ();
int valueTwo ();
}
public class TestClass {
public static void TestOne (int x, int y) {
System.out.println(x + y);
}
@Test (valueOne = 2, valueTwo = 5)
public static void TestTwo (int x, int y) {
System.out.println("This is correct method");
System.out.println(x * y);
}
public static void TestThree (int x, int y) {
System.out.println(x - y);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestClass ob = new TestClass();
Class<?> cls = ob.getClass();
Method[] methods = cls.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Test.class)) {
Test anno = method.getAnnotation(Test.class);
try {
method.invoke(method, anno.valueOne(), anno.valueTwo());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}