KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > aspectj > AspectJAfterThrowingAdvice


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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 implements MethodInterceptor, AfterAdvice {
33
34     public AspectJAfterThrowingAdvice(
35             Method JavaDoc aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
36
37         super(aspectJBeforeAdviceMethod, pointcut, aif);
38     }
39
40     public boolean isBeforeAdvice() {
41         return false;
42     }
43
44     public boolean isAfterAdvice() {
45         return true;
46     }
47
48     public void setThrowingName(String JavaDoc name) {
49         setThrowingNameNoCheck(name);
50     }
51
52     public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
53         try {
54             return mi.proceed();
55         }
56         catch (Throwable JavaDoc t) {
57             if (shouldInvokeOnThrowing(t)) {
58                 invokeAdviceMethod(getJoinPointMatch(), null, t);
59             }
60             throw t;
61         }
62     }
63
64     /**
65      * In AspectJ semantics, after throwing advice that specifies a throwing clause
66      * is only invoked if the thrown exception is a subtype of the given throwing type.
67      */

68     private boolean shouldInvokeOnThrowing(Throwable JavaDoc t) {
69         Class JavaDoc throwingType = getDiscoveredThrowingType();
70         return throwingType.isAssignableFrom(t.getClass());
71     }
72
73 }
74
Popular Tags