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