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.

Counting1.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import java.util.Vector;
  2. import org.aspectj.testing.*;
  3. public class Counting1 {
  4. public static void main(String[] args) {
  5. Point pt1 = new Point(0, 0);
  6. Point pt2 = new Point(4, 4);
  7. Line ln1 = new Line(pt1, pt2);
  8. System.out.println(MoveTracking.testAndClear());
  9. ln1.translate(3, 6);
  10. System.out.println(MoveTracking.testAndClear());
  11. System.out.println(pt1.getX());
  12. Mobility.disableMoves();
  13. ln1.translate(3, 6);
  14. System.out.println(pt1.getX());
  15. }
  16. static class System {
  17. static O out = new O();
  18. static class O {
  19. public void println(Object o) {}
  20. public void println(int i) {}
  21. public void println(boolean b) {}
  22. }
  23. }
  24. }
  25. class FigureEditor {
  26. //...
  27. }
  28. class Figure {
  29. Vector elements = new Vector();
  30. //...
  31. }
  32. interface FigureElement {
  33. public void translate(int dx, int dy);
  34. //...
  35. }
  36. class Point implements FigureElement {
  37. private int _x = 0, _y = 0;
  38. Point(int x, int y) {
  39. _x = x;
  40. _y = y;
  41. }
  42. public void translate(int dx, int dy) {
  43. setX(getX() + dx);
  44. setY(getY() + dy);
  45. }
  46. int getX() { return _x; }
  47. int getY() { return _y; }
  48. void setX(int x) { _x = x; }
  49. void setY(int y) { _y = y; }
  50. //...
  51. }
  52. class Line implements FigureElement {
  53. private Point _p1, _p2;
  54. Line (Point p1, Point p2) {
  55. _p1 = p1;
  56. _p2 = p2;
  57. }
  58. public void translate(int dx, int dy) {
  59. _p1.translate(dx, dy);
  60. _p2.translate(dx, dy);
  61. }
  62. Point getP1() { return _p1; }
  63. Point getP2() { return _p2; }
  64. void setP1(Point p1) { _p1 = p1; }
  65. void setP2(Point p2) { _p2 = p2; }
  66. //...
  67. }
  68. aspect JoinPointCounting {
  69. static int n = 0;
  70. static boolean enable = true;
  71. pointcut points():
  72. /*
  73. instanceof(*) &&
  74. !(receptions(* *.new(..)) ||
  75. executions(* *.new(..)));
  76. */
  77. call(* *.*(..)) ||
  78. //receptions(* *.*(..)) ||
  79. execution(* *.*(..));/* ||
  80. gets(* *.*) ||
  81. sets(* *.*);
  82. */
  83. before(): points() && !within(JoinPointCounting) {
  84. if ( enable ) {
  85. String s = thisJoinPoint + "";
  86. Tester.check(s.indexOf("$") == -1, s + " contains a $");
  87. }
  88. }
  89. }
  90. aspect MoveTracking {
  91. static boolean flag = false;
  92. static boolean testAndClear() {
  93. boolean result = flag;
  94. flag = false;
  95. return result;
  96. }
  97. pointcut moves():
  98. call(void FigureElement.translate(int, int)) ||
  99. call(void Line.setP1(Point)) ||
  100. call(void Line.setP2(Point)) ||
  101. call(void Point.setX(int)) ||
  102. call(void Point.setY(int));
  103. after(): moves() {
  104. flag = true;
  105. }
  106. }
  107. aspect Mobility { declare precedence: Mobility, MoveTracking;
  108. private static boolean enableMoves = true;
  109. static void enableMoves() { enableMoves = true; }
  110. static void disableMoves() { enableMoves = false; }
  111. private int getSomething() { return 10; }
  112. void around(): MoveTracking.moves() {
  113. int x = getSomething();
  114. if ( enableMoves || enableMoves )
  115. proceed(); //!!! in versions prior to 0.7b10 runNext is a
  116. //!!! method on the join point object, so the
  117. //!!! syntax of this call is slightly different
  118. //!!! than in the paper
  119. }
  120. void around(int i): args(i) && call(void *gaoijbal()) {
  121. if (enableMoves) throw new RuntimeException("bad things");
  122. }
  123. }
  124. privileged aspect Foo {
  125. public static boolean getEnableMoves() {
  126. return Mobility.enableMoves;
  127. }
  128. }