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.

CatchAdvice.java 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.lang.reflect.*;
  3. public aspect CatchAdvice {
  4. public static void main(String[] args) { test(); }
  5. public static void test() {
  6. new C().foo();
  7. Tester.check(ranAdvice1, "advice on *'s");
  8. Tester.check(ranAdvice2, "advice on C");
  9. Tester.check(!ranAdvice3, "advice on C and Error");
  10. }
  11. private static boolean ranAdvice1 = false;
  12. before(Throwable t): handler(Throwable+) && args(t){
  13. //int n = thisJoinPoint.parameters.length;
  14. //System.out.println("caught "+t+" "+n+" params");
  15. Tester.check(((CatchClauseSignature)thisJoinPoint.getSignature()).
  16. getParameterType() == ArithmeticException.class,
  17. "right catch clause");
  18. ranAdvice1 = true;
  19. }
  20. after(Throwable t): handler(Throwable+) && args(t) {
  21. //System.out.println("after catch");
  22. }
  23. static boolean ranAdvice2 = false;
  24. after(ArithmeticException t):
  25. this(C) && handler(ArithmeticException) && args(t) {
  26. //System.out.println("(in C) caught "+t);
  27. ranAdvice2 = true;
  28. }
  29. static boolean ranAdvice3 = false;
  30. after(Error e):
  31. within(C) && handler(Error+) && args(e){
  32. //System.out.println("(in C) caught "+e);
  33. ranAdvice3 = true;
  34. }
  35. }
  36. class C {
  37. public void foo() {
  38. int x=0;
  39. try {
  40. int y = 1/x;
  41. } catch (ArithmeticException e) {
  42. //System.out.println("caught arithmetic exception");
  43. }
  44. }
  45. }