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.

PerThis.java 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import org.aspectj.lang.*;
  2. public aspect PerThis perthis(execution(* m(..))) {
  3. before(): execution(* Foo.*(..)) {}
  4. public static void main(String []argv) {
  5. print("before");
  6. new Foo().m();
  7. print("after");
  8. }
  9. public static void print(String prefix) {
  10. System.err.println(prefix);
  11. boolean b1 = Aspects14.hasAspect(PerThis.class,null);
  12. boolean b2 = PerThis.hasAspect(null);
  13. Object o1 = (b1?Aspects14.aspectOf(PerThis.class,null):null);
  14. Object o2 = (b2?PerThis.aspectOf(null):null);
  15. System.err.println("hasAspect? "+b1+" : "+b2);
  16. System.err.println("aspectOf? "+o1+" : "+o2);
  17. }
  18. public String toString() { return "PerThisInstance"; }
  19. }
  20. class Foo {
  21. public void m() { print("during");}
  22. public void print(String prefix) {
  23. System.err.println(prefix);
  24. boolean b1 = Aspects14.hasAspect(PerThis.class,this);
  25. boolean b2 = PerThis.hasAspect(this);
  26. Object o1 = (b1?Aspects14.aspectOf(PerThis.class,this):null);
  27. Object o2 = (b2?PerThis.aspectOf(this):null);
  28. System.err.println("hasAspect? "+b1+" : "+b2);
  29. System.err.println("aspectOf? "+o1+" : "+o2);
  30. }
  31. }