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.

AdviceThrowsCf.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import org.aspectj.testing.Tester;
  2. import java.io.IOException;
  3. public class AdviceThrowsCf {
  4. public static void main(String[] args) {
  5. try {
  6. new C().m1();
  7. Tester.checkFailed("m1");
  8. } catch (CheckedExc ce) {
  9. Tester.checkEqual("b1", ce.getMessage(), "m1");
  10. }
  11. try {
  12. new C().m2();
  13. Tester.checkFailed("m2");
  14. } catch (UncheckedExc ce) {
  15. Tester.checkEqual("b3", ce.getMessage(), "m2");
  16. }
  17. try {
  18. new C().m3();
  19. Tester.checkFailed("m3");
  20. } catch (CheckedExc ce) {
  21. Tester.checkEqual("b1", ce.getMessage(), "m3");
  22. } catch (Exception e) {
  23. Tester.checkFailed("IOException");
  24. System.out.println("m3: " + e);
  25. }
  26. try {
  27. new C().m4();
  28. Tester.checkFailed("m4");
  29. } catch (UncheckedExc ce) {
  30. Tester.checkEqual("b3", ce.getMessage(), "m4");
  31. }
  32. }
  33. }
  34. class CheckedExc extends Exception {
  35. CheckedExc(String m) { super(m); }
  36. }
  37. class UncheckedExc extends RuntimeException {
  38. UncheckedExc(String m) { super(m); }
  39. }
  40. class C {
  41. int x=0;
  42. public void m1() throws CheckedExc {
  43. x += 1;
  44. }
  45. public void m2() throws UncheckedExc {
  46. }
  47. public void m3() throws IOException, CheckedExc {
  48. }
  49. public void m4() {
  50. }
  51. }
  52. aspect A {
  53. pointcut canThrowChecked(): call(* C.m1()) || call(* C.m3());
  54. pointcut canThrowChecked1(): call(* C.m*() throws CheckedExc);
  55. pointcut canThrowUnchecked(): call(* C.m*());
  56. before() throws CheckedExc: canThrowUnchecked() { // ERR: m2 and m4
  57. throw new CheckedExc("b1");
  58. }
  59. before() throws CheckedExc: get(int C.x) { //ERR: all gets
  60. }
  61. before() throws CheckedExc: set(int C.x) { //ERR: all sets
  62. }
  63. before() throws CheckedExc: staticinitialization(C) { //ERR: can't throw
  64. }
  65. void around() throws CheckedExc: canThrowChecked() {
  66. proceed();
  67. }
  68. void around() throws CheckedExc: canThrowUnchecked() { // ERR: can't throw
  69. proceed();
  70. }
  71. void around() throws UncheckedExc: canThrowUnchecked() || set(int C.x) {
  72. proceed();
  73. }
  74. }