1

How does Spring know that ThrowsAdvice.afterThrowing needs to be called?

I found documentation on the class here but I was wondering if anyone has an elegant explanation of how exactly reflection is used to look for that "afterThrowing" method specificially. I just want to see the code that does that so I can understand it better.

A link to some source code would be a sufficient answer.

djangofan
  • 28,471
  • 61
  • 196
  • 289

1 Answers1

2

You're looking at very old documentation (though the current one doesn't say much more).

Spring uses a ThrowsAdviceInterceptor to handle ThrowsAdvice. You can find version 4.1.4.RELEASE source code here.

Its constructor

public ThrowsAdviceInterceptor(Object throwsAdvice) {
    Assert.notNull(throwsAdvice, "Advice must not be null");
    this.throwsAdvice = throwsAdvice;

    Method[] methods = throwsAdvice.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().equals(AFTER_THROWING) &&
                (method.getParameterTypes().length == 1 || method.getParameterTypes().length == 4) &&
                Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length - 1])
            ) {
            // Have an exception handler
            this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterTypes().length - 1], method);
            if (logger.isDebugEnabled()) {
                logger.debug("Found exception handler method: " + method);
            }
        }
    }

    if (this.exceptionHandlerMap.isEmpty()) {
        throw new IllegalArgumentException(
                "At least one handler method must be found in class [" + throwsAdvice.getClass() + "]");
    }
}

scans for appropriate methods and registers them. It then wraps the target method invocation

@Override
public Object invoke(MethodInvocation mi) throws Throwable {
    try {
        return mi.proceed();
    }
    catch (Throwable ex) {
        Method handlerMethod = getExceptionHandler(ex);
        if (handlerMethod != null) {
            invokeHandlerMethod(mi, ex, handlerMethod);
        }
        throw ex;
    }
}

and invokes the handler if an exception is thrown.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724