/examples/kilim/examples/Reflect.java

http://github.com/kilim/kilim · Java · 51 lines · 41 code · 8 blank · 2 comment · 2 complexity · f94cd4dbbcb1776e5d787d91338679e9 MD5 · raw file

  1. package kilim.examples;
  2. import java.lang.reflect.Method;
  3. import kilim.Pausable;
  4. import kilim.Task;
  5. public class Reflect extends Task {
  6. @Override
  7. public void execute() throws Pausable, Exception {
  8. int n = test();
  9. System.out.println("test (normal invocation of instance method): " + n);
  10. // Invoking test() via reflection
  11. Method mthd = Reflect.class.getDeclaredMethod("test", new Class[0]);
  12. Object ret = Task.invoke(mthd, this, /* no args */(Object[])null);
  13. System.out.println("test (reflect invocation of instance method): " + ret);
  14. n = static_test();
  15. System.out.println("test (normal invocation of static method): " + n);
  16. // Invoking test() via reflection
  17. mthd = Reflect.class.getDeclaredMethod("static_test", new Class[0]);
  18. ret = Task.invoke(mthd, /* target = */ null, /* no args */(Object[])null);
  19. System.out.println("test (reflect invocation of static method): " + ret);
  20. System.exit(0);
  21. }
  22. public int test() throws Pausable {
  23. int m = 10;
  24. for (int i = 0; i < 2; i++) { // Force multiple yields
  25. Task.sleep(100);
  26. m *= 2;
  27. }
  28. return m; // must return 40 if all goes well.
  29. }
  30. public static int static_test() throws Pausable {
  31. int m = 10;
  32. for (int i = 0; i < 2; i++) { // Force multiple yields
  33. Task.sleep(100);
  34. m *= 2;
  35. }
  36. return m; // must return 40 if all goes well.
  37. }
  38. public static void main(String args[]) {
  39. Reflect ref = new Reflect();
  40. ref.start();
  41. ref.joinb();
  42. }
  43. }