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.

TryAndProceed.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import org.aspectj.testing.Tester;
  2. public class TryAndProceed {
  3. public static void main(String[] args) {
  4. new C().mNoThrows();
  5. Tester.checkEqual(C.buf.toString(), "beforeAll:aroundAll:mNoThrows:");
  6. C.buf = new StringBuffer();
  7. A.aspectOf().allowThrowingAround = true;
  8. try {
  9. new C().mThrowsCheckedExc();
  10. Tester.checkFailed("should have thrown RuntimeExc");
  11. } catch (CheckedExc ce) {
  12. Tester.checkFailed("should have thrown RuntimeExc not " + ce);
  13. } catch (RuntimeException re) {
  14. //System.out.println("caught " + re);
  15. }
  16. Tester.checkEqual(C.buf.toString(),
  17. "beforeAll:aroundCheckedNoThrow:aroundAll:aroundCheckedThrow:aroundCaughtCE:");
  18. C.buf = new StringBuffer();
  19. A.aspectOf().allowThrowingBefore = true;
  20. try {
  21. new C().mThrowsCheckedExc();
  22. Tester.checkFailed("should have thrown CheckedExc");
  23. } catch (CheckedExc ce) {
  24. //System.out.println("caught " + ce);
  25. } catch (RuntimeException re) {
  26. Tester.checkFailed("should have thrown CheckedExc not RuntimeExc");
  27. }
  28. Tester.checkEqual(C.buf.toString(), "beforeChecked:");
  29. }
  30. }
  31. class C {
  32. public static StringBuffer buf = new StringBuffer();
  33. public void mThrowsCheckedExc() throws CheckedExc {
  34. C.buf.append("mThrowsCheckedExc:");
  35. }
  36. public void mNoThrows() {
  37. C.buf.append("mNoThrows:");
  38. }
  39. }
  40. aspect A {
  41. pointcut checkedCut(): call(void C.mThrowsCheckedExc());
  42. pointcut uncheckedCut(): call(void C.mNoThrows());
  43. pointcut allCut(): checkedCut() || uncheckedCut();
  44. public static boolean allowThrowingBefore = false;
  45. public static boolean allowThrowingAround = false;
  46. before() throws CheckedExc: checkedCut() && if(allowThrowingBefore) {
  47. C.buf.append("beforeChecked:");
  48. throw new CheckedExc("from before");
  49. }
  50. before(): allCut() {
  51. C.buf.append("beforeAll:");
  52. }
  53. Object around(): checkedCut() {
  54. C.buf.append("aroundCheckedNoThrow:");
  55. return proceed();
  56. }
  57. Object around(): allCut() {
  58. C.buf.append("aroundAll:");
  59. try {
  60. return proceed();
  61. } catch (CheckedExc ce) {
  62. C.buf.append("aroundCaughtCE:");
  63. throw new RuntimeException("hand-softening CheckedExc");
  64. }
  65. }
  66. Object around() throws CheckedExc: checkedCut() && if(allowThrowingAround) {
  67. C.buf.append("aroundCheckedThrow:");
  68. throw new CheckedExc("from around");
  69. }
  70. }
  71. class CheckedExc extends Exception {
  72. public CheckedExc(String m) { super(m); }
  73. }