Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] AspectJ and memory management

Adrian Colyer wrote:
but as I'm sure Wes pointed out, this only works when 'dispatchTarget' is an instance of the same concrete class as 'y' (in this case, an instance of A, which is not what you want).

Correct.

You can however write this:

pointcut invocationOfYonA(Y y) : execution(* Y.*(..)) && target(y) && target(A);

Actually, if you can skip "target(A)" that is even better. I don't care which object Y is added to. If you additionally can skip "target(y)" it is also even better. I do not want to write this stuff for each interface, and certainly not for each A. We've got hundreds of interfaces and about fifty A-type classes!!!

  Object around(Y y) : invocationOfYonA(y) {
      // ask the framework for the real dispatch target, which should
      // create one if necessary...
        Y dispatchTarget = framework.getDispatchTargetFor(y);
      Object ret = dispatch(dispatchTarget,thisJoinPoint);
      return ret;
// note the absence of a call to proceed in this advice body, which prevents the
      // original method on A from executing
  }

  // needs suitable exception handling...
  private Object dispatch(Object target, JoinPoint jp) {
CodeSignature sig = (CodeSignature) jp.getSignature(); // assumes only called from above Method m = target.getClass().getMethod(sig.getName(),sig.getParameterTypes());
    return m.invoke(target,jp.getArgs());
  }
which is not as pretty, but if its part of your framework code it could be optimised etc....

This is ok-ish, as long as I only have to write it once. If the pointcut can be simplified to be more generic I think it might be workable. If it is easier if the interfaces extend some marker interface that's ok. I can also let the A-type classes implement some marker interface if that helps.

/Rickard



Back to the top