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.

EnsureOverriding.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // from Bug#: 28703
  2. class Base {
  3. /** extend when overriding - must call Base.lockResource() */
  4. public void lockResource(boolean dummy) { /* ... */ }
  5. }
  6. class Derived extends Base {
  7. boolean isLocked;
  8. public void lockResource(boolean callSuper) {
  9. if (callSuper) super.lockResource(true);
  10. isLocked = true;
  11. }
  12. }
  13. public aspect EnsureOverriding pertarget(mustExtend()) {
  14. boolean calledSuper = false;
  15. pointcut mustExtend() :
  16. execution(void Base+.lockResource(..)) && !within(Base);
  17. after () returning: mustExtend() {
  18. // assert(calledSuper);
  19. if (!calledSuper) { throw new RuntimeException("need super call"); }
  20. }
  21. after(Base a, Base b) returning:
  22. cflow(mustExtend() && target(a)) && execution(void Base.lockResource(..)) && target(b)
  23. {
  24. if (a == b) {
  25. //System.err.println("made call");
  26. calledSuper = true;
  27. }
  28. }
  29. public static void main(String args[]) {
  30. (new Derived()).lockResource(true);
  31. try {
  32. (new Derived()).lockResource(false);
  33. throw new Error("shouldn't get here");
  34. } catch (RuntimeException re) {
  35. if (!re.getMessage().equals("need super call")) throw re;
  36. }
  37. }
  38. }