Skip to content

Commit d7cf2c8

Browse files
committed
MethodBeforeAdviceInterceptor implements BeforeAdvice marker interface
Includes related polishing in the advice interceptor implementations. Issue: SPR-17088 (cherry picked from commit 4e03d3f)
1 parent c89fb74 commit d7cf2c8

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/adapter/AfterReturningAdviceInterceptor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@
3131
* to use this class directly.
3232
*
3333
* @author Rod Johnson
34+
* @see MethodBeforeAdviceInterceptor
35+
* @see ThrowsAdviceInterceptor
3436
*/
3537
@SuppressWarnings("serial")
3638
public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice, Serializable {
@@ -47,6 +49,7 @@ public AfterReturningAdviceInterceptor(AfterReturningAdvice advice) {
4749
this.advice = advice;
4850
}
4951

52+
5053
@Override
5154
public Object invoke(MethodInvocation mi) throws Throwable {
5255
Object retVal = mi.proceed();

spring-aop/src/main/java/org/springframework/aop/framework/adapter/MethodBeforeAdviceInterceptor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.aopalliance.intercept.MethodInterceptor;
2222
import org.aopalliance.intercept.MethodInvocation;
2323

24+
import org.springframework.aop.BeforeAdvice;
2425
import org.springframework.aop.MethodBeforeAdvice;
2526
import org.springframework.util.Assert;
2627

@@ -30,11 +31,13 @@
3031
* to use this class directly.
3132
*
3233
* @author Rod Johnson
34+
* @see AfterReturningAdviceInterceptor
35+
* @see ThrowsAdviceInterceptor
3336
*/
3437
@SuppressWarnings("serial")
35-
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {
38+
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable {
3639

37-
private MethodBeforeAdvice advice;
40+
private final MethodBeforeAdvice advice;
3841

3942

4043
/**
@@ -46,6 +49,7 @@ public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
4649
this.advice = advice;
4750
}
4851

52+
4953
@Override
5054
public Object invoke(MethodInvocation mi) throws Throwable {
5155
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());

spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptor.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,8 @@
5151
*
5252
* @author Rod Johnson
5353
* @author Juergen Hoeller
54+
* @see MethodBeforeAdviceInterceptor
55+
* @see AfterReturningAdviceInterceptor
5456
*/
5557
public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
5658

@@ -67,9 +69,8 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
6769

6870
/**
6971
* Create a new ThrowsAdviceInterceptor for the given ThrowsAdvice.
70-
* @param throwsAdvice the advice object that defines the exception
71-
* handler methods (usually a {@link org.springframework.aop.ThrowsAdvice}
72-
* implementation)
72+
* @param throwsAdvice the advice object that defines the exception handler methods
73+
* (usually a {@link org.springframework.aop.ThrowsAdvice} implementation)
7374
*/
7475
public ThrowsAdviceInterceptor(Object throwsAdvice) {
7576
Assert.notNull(throwsAdvice, "Advice must not be null");
@@ -78,13 +79,14 @@ public ThrowsAdviceInterceptor(Object throwsAdvice) {
7879
Method[] methods = throwsAdvice.getClass().getMethods();
7980
for (Method method : methods) {
8081
if (method.getName().equals(AFTER_THROWING) &&
81-
(method.getParameterCount() == 1 || method.getParameterCount() == 4) &&
82-
Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterCount() - 1])
83-
) {
84-
// Have an exception handler
85-
this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterCount() - 1], method);
86-
if (logger.isDebugEnabled()) {
87-
logger.debug("Found exception handler method: " + method);
82+
(method.getParameterCount() == 1 || method.getParameterCount() == 4)) {
83+
Class<?> throwableParam = method.getParameterTypes()[method.getParameterCount() - 1];
84+
if (Throwable.class.isAssignableFrom(throwableParam)) {
85+
// An exception handler to register...
86+
this.exceptionHandlerMap.put(throwableParam, method);
87+
if (logger.isDebugEnabled()) {
88+
logger.debug("Found exception handler method on throws advice: " + method);
89+
}
8890
}
8991
}
9092
}
@@ -95,14 +97,33 @@ public ThrowsAdviceInterceptor(Object throwsAdvice) {
9597
}
9698
}
9799

100+
101+
/**
102+
* Return the number of handler methods in this advice.
103+
*/
98104
public int getHandlerMethodCount() {
99105
return this.exceptionHandlerMap.size();
100106
}
101107

108+
109+
@Override
110+
public Object invoke(MethodInvocation mi) throws Throwable {
111+
try {
112+
return mi.proceed();
113+
}
114+
catch (Throwable ex) {
115+
Method handlerMethod = getExceptionHandler(ex);
116+
if (handlerMethod != null) {
117+
invokeHandlerMethod(mi, ex, handlerMethod);
118+
}
119+
throw ex;
120+
}
121+
}
122+
102123
/**
103-
* Determine the exception handle method. Can return null if not found.
124+
* Determine the exception handle method for the given exception.
104125
* @param exception the exception thrown
105-
* @return a handler for the given exception type
126+
* @return a handler for the given exception type, or {@code null} if none found
106127
*/
107128
@Nullable
108129
private Method getExceptionHandler(Throwable exception) {
@@ -121,24 +142,10 @@ private Method getExceptionHandler(Throwable exception) {
121142
return handler;
122143
}
123144

124-
@Override
125-
public Object invoke(MethodInvocation mi) throws Throwable {
126-
try {
127-
return mi.proceed();
128-
}
129-
catch (Throwable ex) {
130-
Method handlerMethod = getExceptionHandler(ex);
131-
if (handlerMethod != null) {
132-
invokeHandlerMethod(mi, ex, handlerMethod);
133-
}
134-
throw ex;
135-
}
136-
}
137-
138145
private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable {
139146
Object[] handlerArgs;
140147
if (method.getParameterCount() == 1) {
141-
handlerArgs = new Object[] { ex };
148+
handlerArgs = new Object[] {ex};
142149
}
143150
else {
144151
handlerArgs = new Object[] {mi.getMethod(), mi.getArguments(), mi.getThis(), ex};

0 commit comments

Comments
 (0)