1
1
/*
2
- * Copyright 2002-2015 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.
50
50
*
51
51
* @author Rod Johnson
52
52
* @author Juergen Hoeller
53
+ * @see MethodBeforeAdviceInterceptor
54
+ * @see AfterReturningAdviceInterceptor
53
55
*/
54
56
public class ThrowsAdviceInterceptor implements MethodInterceptor , AfterAdvice {
55
57
@@ -66,24 +68,26 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
66
68
67
69
/**
68
70
* 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)
72
73
*/
73
74
public ThrowsAdviceInterceptor (Object throwsAdvice ) {
74
75
Assert .notNull (throwsAdvice , "Advice must not be null" );
75
76
this .throwsAdvice = throwsAdvice ;
76
77
77
78
Method [] methods = throwsAdvice .getClass ().getMethods ();
78
79
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
+ }
87
91
}
88
92
}
89
93
}
@@ -94,14 +98,33 @@ public ThrowsAdviceInterceptor(Object throwsAdvice) {
94
98
}
95
99
}
96
100
101
+
102
+ /**
103
+ * Return the number of handler methods in this advice.
104
+ */
97
105
public int getHandlerMethodCount () {
98
106
return this .exceptionHandlerMap .size ();
99
107
}
100
108
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
+
101
124
/**
102
- * Determine the exception handle method. Can return null if not found .
125
+ * Determine the exception handle method for the given exception .
103
126
* @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
105
128
*/
106
129
private Method getExceptionHandler (Throwable exception ) {
107
130
Class <?> exceptionClass = exception .getClass ();
@@ -119,24 +142,10 @@ private Method getExceptionHandler(Throwable exception) {
119
142
return handler ;
120
143
}
121
144
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
-
136
145
private void invokeHandlerMethod (MethodInvocation mi , Throwable ex , Method method ) throws Throwable {
137
146
Object [] handlerArgs ;
138
147
if (method .getParameterTypes ().length == 1 ) {
139
- handlerArgs = new Object [] { ex };
148
+ handlerArgs = new Object [] {ex };
140
149
}
141
150
else {
142
151
handlerArgs = new Object [] {mi .getMethod (), mi .getArguments (), mi .getThis (), ex };
0 commit comments