MethodBeforeAdviceInterceptor implements BeforeAdvice marker interface
Includes related polishing in the advice interceptor implementations. Issue: SPR-17088
This commit is contained in:
parent
9ab63b8494
commit
4e03d3fdcb
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -31,6 +31,8 @@ import org.springframework.util.Assert;
|
|||
* to use this class directly.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @see MethodBeforeAdviceInterceptor
|
||||
* @see ThrowsAdviceInterceptor
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice, Serializable {
|
||||
|
|
@ -47,6 +49,7 @@ public class AfterReturningAdviceInterceptor implements MethodInterceptor, After
|
|||
this.advice = advice;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation mi) throws Throwable {
|
||||
Object retVal = mi.proceed();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -21,6 +21,7 @@ import java.io.Serializable;
|
|||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.aop.BeforeAdvice;
|
||||
import org.springframework.aop.MethodBeforeAdvice;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
|
@ -30,11 +31,13 @@ import org.springframework.util.Assert;
|
|||
* to use this class directly.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @see AfterReturningAdviceInterceptor
|
||||
* @see ThrowsAdviceInterceptor
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {
|
||||
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable {
|
||||
|
||||
private MethodBeforeAdvice advice;
|
||||
private final MethodBeforeAdvice advice;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -46,6 +49,7 @@ public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Seriali
|
|||
this.advice = advice;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation mi) throws Throwable {
|
||||
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
|
||||
|
|
|
|||
|
|
@ -51,6 +51,8 @@ import org.springframework.util.Assert;
|
|||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @see MethodBeforeAdviceInterceptor
|
||||
* @see AfterReturningAdviceInterceptor
|
||||
*/
|
||||
public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
|
||||
|
||||
|
|
@ -67,9 +69,8 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
|
|||
|
||||
/**
|
||||
* Create a new ThrowsAdviceInterceptor for the given ThrowsAdvice.
|
||||
* @param throwsAdvice the advice object that defines the exception
|
||||
* handler methods (usually a {@link org.springframework.aop.ThrowsAdvice}
|
||||
* implementation)
|
||||
* @param throwsAdvice the advice object that defines the exception handler methods
|
||||
* (usually a {@link org.springframework.aop.ThrowsAdvice} implementation)
|
||||
*/
|
||||
public ThrowsAdviceInterceptor(Object throwsAdvice) {
|
||||
Assert.notNull(throwsAdvice, "Advice must not be null");
|
||||
|
|
@ -78,13 +79,14 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
|
|||
Method[] methods = throwsAdvice.getClass().getMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(AFTER_THROWING) &&
|
||||
(method.getParameterCount() == 1 || method.getParameterCount() == 4) &&
|
||||
Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterCount() - 1])
|
||||
) {
|
||||
// Have an exception handler
|
||||
this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterCount() - 1], method);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Found exception handler method: " + method);
|
||||
(method.getParameterCount() == 1 || method.getParameterCount() == 4)) {
|
||||
Class<?> throwableParam = method.getParameterTypes()[method.getParameterCount() - 1];
|
||||
if (Throwable.class.isAssignableFrom(throwableParam)) {
|
||||
// An exception handler to register...
|
||||
this.exceptionHandlerMap.put(throwableParam, method);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Found exception handler method on throws advice: " + method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -95,14 +97,33 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the number of handler methods in this advice.
|
||||
*/
|
||||
public int getHandlerMethodCount() {
|
||||
return this.exceptionHandlerMap.size();
|
||||
}
|
||||
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the exception handle method. Can return null if not found.
|
||||
* Determine the exception handle method for the given exception.
|
||||
* @param exception the exception thrown
|
||||
* @return a handler for the given exception type
|
||||
* @return a handler for the given exception type, or {@code null} if none found
|
||||
*/
|
||||
@Nullable
|
||||
private Method getExceptionHandler(Throwable exception) {
|
||||
|
|
@ -121,24 +142,10 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
|
|||
return handler;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable {
|
||||
Object[] handlerArgs;
|
||||
if (method.getParameterCount() == 1) {
|
||||
handlerArgs = new Object[] { ex };
|
||||
handlerArgs = new Object[] {ex};
|
||||
}
|
||||
else {
|
||||
handlerArgs = new Object[] {mi.getMethod(), mi.getArguments(), mi.getThis(), ex};
|
||||
|
|
|
|||
Loading…
Reference in New Issue