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: import java.lang.reflect.Type;
21:
22: import org.springframework.aop.AfterAdvice;
23: import org.springframework.aop.AfterReturningAdvice;
24: import org.springframework.util.ClassUtils;
25: import org.springframework.util.TypeUtils;
26:
27: /**
28: * Spring AOP advice wrapping an AspectJ after-returning advice method.
29: *
30: * @author Rod Johnson
31: * @author Juergen Hoeller
32: * @author Ramnivas Laddad
33: * @since 2.0
34: */
35: public class AspectJAfterReturningAdvice extends AbstractAspectJAdvice
36: implements AfterReturningAdvice, AfterAdvice {
37:
38: public AspectJAfterReturningAdvice(
39: Method aspectJBeforeAdviceMethod,
40: AspectJExpressionPointcut pointcut,
41: AspectInstanceFactory aif) {
42:
43: super (aspectJBeforeAdviceMethod, pointcut, aif);
44: }
45:
46: public boolean isBeforeAdvice() {
47: return false;
48: }
49:
50: public boolean isAfterAdvice() {
51: return true;
52: }
53:
54: public void setReturningName(String name) {
55: setReturningNameNoCheck(name);
56: }
57:
58: public void afterReturning(Object returnValue, Method method,
59: Object[] args, Object target) throws Throwable {
60: if (shouldInvokeOnReturnValueOf(method, returnValue)) {
61: invokeAdviceMethod(getJoinPointMatch(), returnValue, null);
62: }
63: }
64:
65: /**
66: * Following AspectJ semantics, if a returning clause was specified, then the
67: * advice is only invoked if the returned value is an instance of the given
68: * returning type and generic type parameters, if any, match the assignment
69: * rules. If the returning type is Object, the advice is *always* invoked.
70: * @param returnValue the return value of the target method
71: * @return whether to invoke the advice method for the given return value
72: */
73: private boolean shouldInvokeOnReturnValueOf(Method method,
74: Object returnValue) {
75: Class type = getDiscoveredReturningType();
76: Object genericType = getDiscoveredReturningGenericType();
77: // If we aren't dealing with a raw type, check if generic parameters are assignable.
78: return (ClassUtils.isAssignableValue(type, returnValue) && (genericType == null
79: || genericType == type || GenericTypeMatcher
80: .isAssignable(genericType, method)));
81: }
82:
83: /**
84: * Inner class to avoid a static JDK 1.5 dependency for generic type matching.
85: */
86: private static class GenericTypeMatcher {
87:
88: public static boolean isAssignable(Object genericType,
89: Method method) {
90: return TypeUtils.isAssignable((Type) genericType, method
91: .getGenericReturnType());
92: }
93: }
94:
95: }
|