Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7.  
  8. class TestRunner {
  9. public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
  10. TestClass testClass = new TestClass();
  11.  
  12. Class<?> cls = TestClass.class;
  13.  
  14. Method[] methods = cls.getDeclaredMethods();
  15.  
  16. for (Method method : methods) {
  17. if(method.isAnnotationPresent(Test.class)) {
  18. Test test = method.getAnnotation(Test.class);
  19. int result = (Integer) method.invoke(testClass, test.a(), test.b());
  20. System.out.println(result);
  21. }
  22. }
  23. }
  24.  
  25. static class TestClass {
  26. @Test(a = 2, b = 5)
  27. public int sum(int a, int b) {
  28. return a + b;
  29. }
  30.  
  31. }
  32. }
  33.  
  34. @Target(value = ElementType.METHOD)
  35. @Retention(value = RetentionPolicy.RUNTIME)
  36. @interface Test {
  37. int a();
  38. int b();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement