01: /*
02: * Copyright 2002-2006 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 org.aopalliance.aop.Advice;
20:
21: import org.springframework.aop.ClassFilter;
22: import org.springframework.aop.IntroductionAdvisor;
23: import org.springframework.aop.support.ClassFilters;
24: import org.springframework.aop.support.DelegatePerTargetObjectIntroductionInterceptor;
25:
26: /**
27: * Introduction advisor delegating to the given object.
28: * Implements AspectJ annotation-style behavior for the DeclareParents annotation.
29: *
30: * @author Rod Johnson
31: * @since 2.0
32: */
33: public class DeclareParentsAdvisor implements IntroductionAdvisor {
34:
35: private final Class introducedInterface;
36:
37: private final ClassFilter typePatternClassFilter;
38:
39: private final Advice advice;
40:
41: /**
42: * Create a new advisor for this DeclareParents field.
43: * @param interfaceType static field defining the introduction
44: * @param typePattern type pattern the introduction is restricted to
45: * @param defaultImpl default implementation class
46: */
47: public DeclareParentsAdvisor(Class interfaceType,
48: String typePattern, Class defaultImpl) {
49: this .introducedInterface = interfaceType;
50: ClassFilter typePatternFilter = new TypePatternClassFilter(
51: typePattern);
52:
53: // Excludes methods implemented.
54: ClassFilter exclusion = new ClassFilter() {
55: public boolean matches(Class clazz) {
56: return !(introducedInterface.isAssignableFrom(clazz));
57: }
58: };
59:
60: this .typePatternClassFilter = ClassFilters.intersection(
61: typePatternFilter, exclusion);
62: this .advice = new DelegatePerTargetObjectIntroductionInterceptor(
63: defaultImpl, interfaceType);
64: }
65:
66: public ClassFilter getClassFilter() {
67: return this .typePatternClassFilter;
68: }
69:
70: public void validateInterfaces() throws IllegalArgumentException {
71: // Do nothing
72: }
73:
74: public boolean isPerInstance() {
75: return true;
76: }
77:
78: public Advice getAdvice() {
79: return advice;
80: }
81:
82: public Class[] getInterfaces() {
83: return new Class[] { this.introducedInterface };
84: }
85:
86: }
|