1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
51
51
*
52
52
* @author Rod Johnson
53
53
* @author Juergen Hoeller
54
+ * @see MethodBeforeAdviceInterceptor
55
+ * @see AfterReturningAdviceInterceptor
54
56
*/
55
57
public class ThrowsAdviceInterceptor implements MethodInterceptor , AfterAdvice {
56
58
@@ -67,9 +69,8 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
67
69
68
70
/**
69
71
* 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)
73
74
*/
74
75
public ThrowsAdviceInterceptor (Object throwsAdvice ) {
75
76
Assert .notNull (throwsAdvice , "Advice must not be null" );
@@ -78,13 +79,14 @@ public ThrowsAdviceInterceptor(Object throwsAdvice) {
78
79
Method [] methods = throwsAdvice .getClass ().getMethods ();
79
80
for (Method method : methods ) {
80
81
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
+ }
88
90
}
89
91
}
90
92
}
@@ -95,14 +97,33 @@ public ThrowsAdviceInterceptor(Object throwsAdvice) {
95
97
}
96
98
}
97
99
100
+
101
+ /**
102
+ * Return the number of handler methods in this advice.
103
+ */
98
104
public int getHandlerMethodCount () {
99
105
return this .exceptionHandlerMap .size ();
100
106
}
101
107
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
+
102
123
/**
103
- * Determine the exception handle method. Can return null if not found .
124
+ * Determine the exception handle method for the given exception .
104
125
* @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
106
127
*/
107
128
@ Nullable
108
129
private Method getExceptionHandler (Throwable exception ) {
@@ -121,24 +142,10 @@ private Method getExceptionHandler(Throwable exception) {
121
142
return handler ;
122
143
}
123
144
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
-
138
145
private void invokeHandlerMethod (MethodInvocation mi , Throwable ex , Method method ) throws Throwable {
139
146
Object [] handlerArgs ;
140
147
if (method .getParameterCount () == 1 ) {
141
- handlerArgs = new Object [] { ex };
148
+ handlerArgs = new Object [] {ex };
142
149
}
143
150
else {
144
151
handlerArgs = new Object [] {mi .getMethod (), mi .getArguments (), mi .getThis (), ex };
0 commit comments