KickJava   Java API By Example, From Geeks To Geeks.

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


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.springframework.aop.AfterAdvice;
22 import org.springframework.aop.AfterReturningAdvice;
23
24 /**
25  * Spring AOP advice wrapping an AspectJ after-returning advice method.
26  *
27  * @author Rod Johnson
28  * @since 2.0
29  */

30 public class AspectJAfterReturningAdvice extends AbstractAspectJAdvice implements AfterReturningAdvice, AfterAdvice {
31
32     public AspectJAfterReturningAdvice(
33             Method JavaDoc aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
34
35         super(aspectJBeforeAdviceMethod, pointcut, aif);
36     }
37
38     public boolean isBeforeAdvice() {
39         return false;
40     }
41
42     public boolean isAfterAdvice() {
43         return true;
44     }
45
46     public void setReturningName(String JavaDoc name) {
47         setReturningNameNoCheck(name);
48     }
49
50     public void afterReturning(Object JavaDoc returnValue, Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc target) throws Throwable JavaDoc {
51         if (shouldInvokeOnReturnValueOf(returnValue)) {
52             invokeAdviceMethod(getJoinPointMatch(), returnValue, null);
53         }
54     }
55
56     /**
57      * Following AspectJ semantics, if a returning clause was specified, then the
58      * advice is only invoked if the returned value is an instance of the given
59      * returning type. Iff the returning type is object, the advice is *always* invoked.
60      */

61     private boolean shouldInvokeOnReturnValueOf(Object JavaDoc returnValue) {
62         Class JavaDoc returningType = getDiscoveredReturningType();
63         if (returningType.equals(Object JavaDoc.class) || returningType.isPrimitive()) {
64             return true;
65         }
66         else {
67             return returningType.isAssignableFrom(returnValue.getClass());
68         }
69     }
70
71 }
72
Popular Tags