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.

CflowOrderOriginal.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package bugs;
  2. import java.io.PrintStream;
  3. import java.lang.annotation.*;
  4. import org.aspectj.lang.JoinPoint;
  5. public class CflowOrderOriginal {
  6. public static void main(String[] args) {
  7. Log.print("Starting CflowOrder.main(..)");
  8. A.main(null);
  9. Log.print("Ending CflowOrder.main(..)");
  10. }
  11. @Retention(value = RetentionPolicy.RUNTIME)
  12. @interface Annotation {
  13. String value();
  14. }
  15. static class A {
  16. @Annotation("A.foo")
  17. void foo() {
  18. new B().foo();
  19. Log.print("A.foo()");
  20. }
  21. public static void main(String[] args) {
  22. new A().foo();
  23. Log.print("A.main(..)");
  24. }
  25. }
  26. static class B {
  27. @Annotation("B.foo")
  28. void foo() {
  29. Log.print("B.foo()");
  30. }
  31. }
  32. static class Log implements IAspect {
  33. static final PrintStream out = System.err;
  34. static void print(String label) {
  35. out.println(label);
  36. }
  37. static void print(String label, JoinPoint tjp, JoinPoint.StaticPart sp,
  38. Object a) {
  39. out.println(label);
  40. // out.println(" Join point: " + tjp);
  41. // out.println(" Enclosing join point: " + sp);
  42. // out.println(" Annotation: " + a);
  43. }
  44. }
  45. static aspect Logger implements IAspect {
  46. //declare error: execution(* *(..)) && !within(Log) : "er";
  47. // before() : cflow(execution(void CflowOrder.main(String[])))
  48. // && !call(* IAspect+.*(..)) && ! within(IAspect+) {
  49. // Log.print("cflow(..main(..))", thisJoinPoint,
  50. // thisEnclosingJoinPointStaticPart, null);
  51. // }
  52. }
  53. interface IAspect {}
  54. static aspect MyAspect implements IAspect {
  55. pointcut annotated(Annotation a) :
  56. call(@Annotation * *(..)) && @annotation(a);
  57. pointcut belowAnnotated() :
  58. cflowbelow(annotated(Annotation));
  59. pointcut topAnnotated(Annotation a) : annotated(a)
  60. && !belowAnnotated();
  61. pointcut notTopAnnotated(Annotation a, Annotation aTop) : annotated(a)
  62. && cflowbelow(annotated(aTop));
  63. // pointcut topAnnotated(Annotation a) : annotated(a)
  64. // && !cflowbelow(annotated(Annotation));
  65. //
  66. // pointcut notTopAnnotated(Annotation a, Annotation aTop) : annotated(a)
  67. // && cflowbelow(topAnnotated(aTop));
  68. // if this first, then no nonTopAnnotated advice
  69. before(Annotation a) : topAnnotated(a) {
  70. Log.print("topAnnotated", thisJoinPoint,
  71. thisEnclosingJoinPointStaticPart, a);
  72. }
  73. // if topAnnotated is first, this does not run
  74. before(Annotation a, Annotation aTop) : notTopAnnotated(a, aTop) {
  75. Log.print("nonTopAnnotated", thisJoinPoint,
  76. thisEnclosingJoinPointStaticPart, a);
  77. }
  78. }
  79. }