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.

AfterThrowing.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import org.aspectj.testing.Tester;
  2. import java.util.*;
  3. public class AfterThrowing {
  4. public static void main(String[] args) { test(); }
  5. public static void test() {
  6. throwException(true);
  7. Tester.check("Throwable:throwException");
  8. Tester.check("Exception:throwException");
  9. throwRuntimeException(true);
  10. Tester.check("Throwable:throwRuntimeException");
  11. Tester.check("RuntimeException:throwRuntimeException");
  12. throwError(true);
  13. Tester.check("Throwable:throwError");
  14. throwMyException(true);
  15. Tester.check("Throwable:throwMyException");
  16. Tester.check("Exception:throwMyException");
  17. Tester.check("MyException:throwMyException");
  18. }
  19. static void throwNothing(boolean b) { }
  20. static void throwException(boolean b) throws Exception {
  21. if (b) throw new Exception();
  22. throwError(false);
  23. }
  24. static void throwRuntimeException(boolean b) {
  25. if (b) throw new RuntimeException();
  26. }
  27. static String throwError(boolean b) {
  28. int[] i = new int[10];
  29. // this line is to make sure ajc doesn't think it needs to worry about a
  30. // CloneNotSupportedException when arrays are cloned
  31. i = (int[])i.clone();
  32. if (b) throw new Error();
  33. return "foo";
  34. }
  35. static Object throwMyException(boolean b) throws MyException {
  36. if (b) throw new MyException();
  37. return new Integer(10);
  38. }
  39. public static class MyException extends Exception { }
  40. }
  41. aspect A {
  42. pointcut throwerCut(): within(AfterThrowing) && execution(* *(boolean));
  43. after () throwing (Throwable t): throwerCut() {
  44. Tester.note("Throwable:" + thisJoinPointStaticPart.getSignature().getName());
  45. }
  46. after () throwing (Exception t): throwerCut() {
  47. Tester.note("Exception:" + thisJoinPoint.getSignature().getName());
  48. }
  49. after () throwing (RuntimeException t): throwerCut() {
  50. Tester.note("RuntimeException:" + thisJoinPointStaticPart.getSignature().getName());
  51. }
  52. after () throwing (AfterThrowing.MyException t): throwerCut() {
  53. Tester.note("MyException:" + thisJoinPoint.getSignature().getName());
  54. }
  55. pointcut catchThrowsFrom():
  56. within(AfterThrowing) && call(* AfterThrowing.*(boolean));
  57. declare soft: Throwable: catchThrowsFrom();
  58. Object around(): catchThrowsFrom() {
  59. try {
  60. return proceed();
  61. } catch (Throwable t) {
  62. //System.out.println("caught " + t);
  63. return null;
  64. }
  65. }
  66. }