01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.aop.aspectj;
18:
19: import java.lang.reflect.Method;
20:
21: import org.aopalliance.intercept.MethodInterceptor;
22: import org.aopalliance.intercept.MethodInvocation;
23:
24: import org.springframework.aop.AfterAdvice;
25:
26: /**
27: * Spring AOP advice wrapping an AspectJ after-throwing advice method.
28: *
29: * @author Rod Johnson
30: * @since 2.0
31: */
32: public class AspectJAfterThrowingAdvice extends AbstractAspectJAdvice
33: implements MethodInterceptor, AfterAdvice {
34:
35: public AspectJAfterThrowingAdvice(Method aspectJBeforeAdviceMethod,
36: AspectJExpressionPointcut pointcut,
37: AspectInstanceFactory aif) {
38:
39: super (aspectJBeforeAdviceMethod, pointcut, aif);
40: }
41:
42: public boolean isBeforeAdvice() {
43: return false;
44: }
45:
46: public boolean isAfterAdvice() {
47: return true;
48: }
49:
50: public void setThrowingName(String name) {
51: setThrowingNameNoCheck(name);
52: }
53:
54: public Object invoke(MethodInvocation mi) throws Throwable {
55: try {
56: return mi.proceed();
57: } catch (Throwable t) {
58: if (shouldInvokeOnThrowing(t)) {
59: invokeAdviceMethod(getJoinPointMatch(), null, t);
60: }
61: throw t;
62: }
63: }
64:
65: /**
66: * In AspectJ semantics, after throwing advice that specifies a throwing clause
67: * is only invoked if the thrown exception is a subtype of the given throwing type.
68: */
69: private boolean shouldInvokeOnThrowing(Throwable t) {
70: Class throwingType = getDiscoveredThrowingType();
71: return throwingType.isAssignableFrom(t.getClass());
72: }
73:
74: }
|