Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DeclaredExcs.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import org.aspectj.testing.Tester;
  2. public class DeclaredExcs {
  3. public static void main(String[] args) { test(); }
  4. public static void test() {
  5. Foo foo = new Foo();
  6. Tester.checkEqual(foo.foo(), "success", "foo()");
  7. try {
  8. foo.bar(false);
  9. } catch (Exception e) {
  10. Tester.check(false, "shouldn't catch");
  11. }
  12. try {
  13. Bar bar = new Bar(false);
  14. } catch (MyException e) {
  15. Tester.check(false, "shouldn't catch");
  16. }
  17. try {
  18. Bar bar = Bar.getNewBar(true);
  19. Tester.check(false, "shouldn't get here");
  20. } catch (MyException e) {
  21. Tester.check(true, "should catch");
  22. }
  23. }
  24. }
  25. class Foo {
  26. public void bar(boolean throwIt) throws Exception {
  27. if (throwIt) {
  28. throw new MyException("you asked for it");
  29. }
  30. }
  31. public String foo() {
  32. try {
  33. bar(false);
  34. } catch (Exception exc) {
  35. Tester.check(false, "shouldn't catch anything");
  36. }
  37. try {
  38. bar(true);
  39. } catch (MyException exc) {
  40. return "success";
  41. } catch (Exception e1) {
  42. return "failure";
  43. }
  44. return "failure";
  45. }
  46. }
  47. class Bar {
  48. String value;
  49. public static Bar getNewBar(boolean throwIt) throws MyException {
  50. return new Bar(throwIt);
  51. }
  52. public Bar(boolean throwIt) throws MyException {
  53. if (throwIt) {
  54. throw new MyException("you asked for it from constructor");
  55. }
  56. value = "boolean";
  57. }
  58. public Bar(String value) {
  59. this.value = value;
  60. }
  61. }
  62. class MyException extends Exception {
  63. public MyException(String label) {
  64. super(label);
  65. }
  66. }
  67. aspect A {
  68. before (): (this(*) && execution(* *(..)) || execution(new(..))) && !within(A) {
  69. //System.out.println("entering: "+thisJoinPoint);
  70. }
  71. after (): (this(*) && execution(* *(..)) || execution(new(..))) && !within(A) {
  72. //System.out.println("exiting: "+thisJoinPoint);
  73. }
  74. Object around(): this(*) && call(* *(..)) {
  75. //System.out.println("start around: "+thisJoinPoint);
  76. Object ret = proceed();
  77. //System.out.println("end around: "+thisJoinPoint);
  78. return ret;
  79. }
  80. }