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.

AdviceOnInheritedMethod.java 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.lang.*;
  3. public aspect AdviceOnInheritedMethod {
  4. public static void main(String[] args) { test(); }
  5. public static void test() {
  6. SuperC sc = new SubC();
  7. Tester.checkEqual(sc.doIt(":foo"),
  8. ":foo:beforeDoIt:inSubC:foo:beforeDoIt1:inSubC:doIt1",
  9. "SubC.doIt");
  10. Tester.checkEqual(new SuperC().doIt(":foo"),
  11. ":foo:beforeDoIt1:inSuperC:doIt1",
  12. "SuperC.doIt");
  13. Tester.checkEqual(new SubC().packageDoIt(":foo"),
  14. ":foo:beforePackageDoIt:inSubC:foo",
  15. "SubC.packageDoIt");
  16. }
  17. String getTargetName(JoinPoint thisJoinPoint) {
  18. return thisJoinPoint.getTarget().getClass().getName();
  19. }
  20. String around(String a):
  21. target(*) && call(String packageDoIt(String)) && args(a)
  22. {
  23. return a+
  24. ":beforePackageDoIt:in"+getTargetName(thisJoinPoint)
  25. + proceed(a);
  26. }
  27. String around(String a):
  28. target(SubC) && call(String doIt(String)) && args(a) {
  29. return a+
  30. ":beforeDoIt:in"+getTargetName(thisJoinPoint)
  31. + proceed(a);
  32. }
  33. String around(String a):
  34. target(SuperC) && call(String doIt1(String)) && args(a) {
  35. return a+
  36. ":beforeDoIt1:in"+getTargetName(thisJoinPoint)
  37. + proceed(a);
  38. }
  39. }
  40. class SuperC {
  41. public String doIt(String arg) {
  42. return doIt1(arg);
  43. }
  44. protected String doIt1(String arg) {
  45. return ":doIt1";
  46. }
  47. String packageDoIt(String arg) {
  48. return arg;
  49. }
  50. }
  51. class SubC extends SuperC {
  52. public String doIt(String arg) {
  53. return super.doIt(arg);
  54. }
  55. }