views:

24

answers:

1

I have a trace aspect that should log:

  • Entering
  • Exiting (return type is void)
  • Returning [returned object]
  • Throwinig [Exception message]

I am having problems with the second. How do I create an advice for this case without double-logging all exits that also return something as is the case now when I have one @After advice and one @AfterReturning(value = "publicMethodCall()", returning = "o"). Can I somehow have the @AfterReturning advice be called for void returns and still retrieving its value when it returns are non-void (probably not as it would be impossible to tell if the method returned null or if the return type was void).

I a, guessing this should be easy but I can't see it...

A: 

It would be simpler to use around advice. One pointcut/advice pair. (I am using the code style aspectj syntax here because I prefer it). I can translate to @AspectJ style if you need:

Object around() : publicMethodCall() {
  try {
    Object result = proceed();
    log(result, thisJoinPoint);
    return result;
  } catch (Throwable t) {
    log(t, thisJoinPoint);
    throw t;
  }
}

Here, if your method returns void, then the result will be null.

Andrew Eisenberg
Yes, but how do I determine if the returned value is null or if the method return type is void without string parsing the jp.getSignature().toLongString() value? I need to distinguish between the two cases.
Kristofer
you can do something like ((MethodSignature) jp.getSignature()).getReturnType();You know that it will be a method signature based on the pointcut
Andrew Eisenberg

related questions