Skip to content

Commit 001cecd

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 21b7dc1 commit 001cecd

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
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: 8 additions & 4 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,9 +49,10 @@ public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
4649
this.advice = advice;
4750
}
4851

52+
4953
@Override
5054
public Object invoke(MethodInvocation mi) throws Throwable {
51-
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
55+
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
5256
return mi.proceed();
5357
}
5458

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 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.
@@ -50,6 +50,8 @@
5050
*
5151
* @author Rod Johnson
5252
* @author Juergen Hoeller
53+
* @see MethodBeforeAdviceInterceptor
54+
* @see AfterReturningAdviceInterceptor
5355
*/
5456
public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
5557

@@ -66,24 +68,26 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
6668

6769
/**
6870
* Create a new ThrowsAdviceInterceptor for the given ThrowsAdvice.
69-
* @param throwsAdvice the advice object that defines the exception
70-
* handler methods (usually a {@link org.springframework.aop.ThrowsAdvice}
71-
* implementation)
71+
* @param throwsAdvice the advice object that defines the exception handler methods
72+
* (usually a {@link org.springframework.aop.ThrowsAdvice} implementation)
7273
*/
7374
public ThrowsAdviceInterceptor(Object throwsAdvice) {
7475
Assert.notNull(throwsAdvice, "Advice must not be null");
7576
this.throwsAdvice = throwsAdvice;
7677

7778
Method[] methods = throwsAdvice.getClass().getMethods();
7879
for (Method method : methods) {
79-
if (method.getName().equals(AFTER_THROWING) &&
80-
(method.getParameterTypes().length == 1 || method.getParameterTypes().length == 4) &&
81-
Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length - 1])
82-
) {
83-
// Have an exception handler
84-
this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterTypes().length - 1], method);
85-
if (logger.isDebugEnabled()) {
86-
logger.debug("Found exception handler method: " + method);
80+
if (method.getName().equals(AFTER_THROWING)) {
81+
Class<?>[] paramTypes = method.getParameterTypes();
82+
if (paramTypes.length == 1 || paramTypes.length == 4) {
83+
Class<?> throwableParam = paramTypes[paramTypes.length - 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+
}
90+
}
8791
}
8892
}
8993
}
@@ -94,14 +98,33 @@ public ThrowsAdviceInterceptor(Object throwsAdvice) {
9498
}
9599
}
96100

101+
102+
/**
103+
* Return the number of handler methods in this advice.
104+
*/
97105
public int getHandlerMethodCount() {
98106
return this.exceptionHandlerMap.size();
99107
}
100108

109+
110+
@Override
111+
public Object invoke(MethodInvocation mi) throws Throwable {
112+
try {
113+
return mi.proceed();
114+
}
115+
catch (Throwable ex) {
116+
Method handlerMethod = getExceptionHandler(ex);
117+
if (handlerMethod != null) {
118+
invokeHandlerMethod(mi, ex, handlerMethod);
119+
}
120+
throw ex;
121+
}
122+
}
123+
101124
/**
102-
* Determine the exception handle method. Can return null if not found.
125+
* Determine the exception handle method for the given exception.
103126
* @param exception the exception thrown
104-
* @return a handler for the given exception type
127+
* @return a handler for the given exception type, or {@code null} if none found
105128
*/
106129
private Method getExceptionHandler(Throwable exception) {
107130
Class<?> exceptionClass = exception.getClass();
@@ -119,24 +142,10 @@ private Method getExceptionHandler(Throwable exception) {
119142
return handler;
120143
}
121144

122-
@Override
123-
public Object invoke(MethodInvocation mi) throws Throwable {
124-
try {
125-
return mi.proceed();
126-
}
127-
catch (Throwable ex) {
128-
Method handlerMethod = getExceptionHandler(ex);
129-
if (handlerMethod != null) {
130-
invokeHandlerMethod(mi, ex, handlerMethod);
131-
}
132-
throw ex;
133-
}
134-
}
135-
136145
private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable {
137146
Object[] handlerArgs;
138147
if (method.getParameterTypes().length == 1) {
139-
handlerArgs = new Object[] { ex };
148+
handlerArgs = new Object[] {ex};
140149
}
141150
else {
142151
handlerArgs = new Object[] {mi.getMethod(), mi.getArguments(), mi.getThis(), ex};

0 commit comments

Comments
 (0)