You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import org.aspectj.testing.Tester;
  2. aspect ClientFlow percflow(entries(Client)) {
  3. pointcut entries(Client c):
  4. this(c) && (call(void Server.doService1(Object)) ||
  5. call(void Server.doService2()));
  6. Client client;
  7. before (Client c): entries(c) { client = c; }
  8. pointcut workPoints():
  9. (this(ServiceWorker1) && call(void doWorkItemA())) ||
  10. (this(ServiceWorker2) && call(void doWorkItemB())) ||
  11. (this(ServiceWorker3) && call(void doWorkItemC())) ||
  12. (this(ServiceWorker4) && call(void doWorkItemD()));
  13. Object around(): workPoints() {
  14. //System.out.println("at work: " + thisJoinPoint.methodName);
  15. client.count++;
  16. return proceed();
  17. //return;
  18. }
  19. void util(Client c) {
  20. c.count++;
  21. client.count++;
  22. }
  23. }
  24. public class Client {
  25. public static void main(String[] args) { test(); }
  26. public static void test() {
  27. Client c = new Client();
  28. Server s = new Server();
  29. c.requestServices(s);
  30. Tester.checkEqual(c.count, 5, "A+B+C+2*D");
  31. Tester.check("WorkA");
  32. Tester.check("WorkB");
  33. Tester.check("WorkC");
  34. Tester.check("WorkD");
  35. }
  36. int count;
  37. public void requestServices(Server s) {
  38. s.doService1("foo");
  39. s.doService2();
  40. }
  41. }
  42. class Server {
  43. ServiceWorker1 worker1 = new ServiceWorker1();
  44. ServiceWorker2 worker2 = new ServiceWorker2();
  45. public void doService1(Object data) {
  46. worker1.doYourPart();
  47. }
  48. public void doService2() {
  49. worker2.doYourPart();
  50. }
  51. }
  52. class ServiceWorker1 {
  53. void doYourPart() {
  54. doWorkItemA();
  55. }
  56. void doWorkItemA() { Tester.note("WorkA");}
  57. }
  58. class ServiceWorker2 {
  59. ServiceWorker3 worker3 = new ServiceWorker3();
  60. ServiceWorker4 worker4 = new ServiceWorker4();
  61. void doYourPart() {
  62. worker3.doYourPart();
  63. worker4.doYourPart();
  64. doWorkItemB();
  65. }
  66. void doWorkItemB() { Tester.note("WorkB");}
  67. }
  68. class ServiceWorker3 {
  69. void doYourPart() {
  70. doWorkItemC();
  71. }
  72. void doWorkItemC() { Tester.note("WorkC"); }
  73. }
  74. class ServiceWorker4 {
  75. void doYourPart() {
  76. doWorkItemD();
  77. }
  78. void doWorkItemD() {
  79. // charge extra for 'd' "by hand"
  80. ClientFlow.aspectOf().client.count++;
  81. Tester.note("WorkD");
  82. }
  83. }