Intercepting only void callbacks using AspectJ

I have a trace aspect that needs to be logged:

  • Enter
  • Exit (return type is not valid)
  • Return [returned object]
  • Throwinig [Exception Message]

I have problems with the second one. How to create advice for this case without double-registering all exits that also return something, as is the case now that I have one advice @After and one @AfterReturning (value = "publicMethodCall ()", return = "o "), Can I somehow call @AfterReturning advice to get void returns and still get its value when it returns, is not void (maybe not like it would be impossible to determine if the method was returned null or if the return type was invalid).

I, assuming this should be easy, but I do not see it ...

+3
source share
1 answer

It would be easier to use tips. One pair of pointcut / advice. (I use the style style 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 it resultwill null.

+3
source

Source: https://habr.com/ru/post/1770101/


All Articles