Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- class TestRunner {
- public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
- TestClass testClass = new TestClass();
- Class<?> cls = TestClass.class;
- Method[] methods = cls.getDeclaredMethods();
- for (Method method : methods) {
- if(method.isAnnotationPresent(Test.class)) {
- Test test = method.getAnnotation(Test.class);
- int result = (Integer) method.invoke(testClass, test.a(), test.b());
- System.out.println(result);
- }
- }
- }
- static class TestClass {
- @Test(a = 2, b = 5)
- public int sum(int a, int b) {
- return a + b;
- }
- }
- }
- @Target(value = ElementType.METHOD)
- @Retention(value = RetentionPolicy.RUNTIME)
- @interface Test {
- int a();
- int b();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement