KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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 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 JavaDoc introducedInterface;
36
37     private final ClassFilter typePatternClassFilter;
38
39     private final Advice advice;
40
41
42     /**
43      * Create a new advisor for this DeclareParents field.
44      * @param interfaceType static field defining the introduction
45      * @param typePattern type pattern the introduction is restricted to
46      * @param defaultImpl default implementation class
47      */

48     public DeclareParentsAdvisor(Class JavaDoc interfaceType, String JavaDoc typePattern, Class JavaDoc defaultImpl) {
49         this.introducedInterface = interfaceType;
50         ClassFilter typePatternFilter = new TypePatternClassFilter(typePattern);
51
52         // Excludes methods implemented.
53
ClassFilter exclusion = new ClassFilter() {
54             public boolean matches(Class JavaDoc clazz) {
55                 return !(introducedInterface.isAssignableFrom(clazz));
56             }
57         };
58
59         this.typePatternClassFilter = ClassFilters.intersection(typePatternFilter, exclusion);
60         this.advice = new DelegatePerTargetObjectIntroductionInterceptor(defaultImpl, interfaceType);
61     }
62
63
64     public ClassFilter getClassFilter() {
65         return this.typePatternClassFilter;
66     }
67
68     public void validateInterfaces() throws IllegalArgumentException JavaDoc {
69         // Do nothing
70
}
71
72     public boolean isPerInstance() {
73         return true;
74     }
75
76     public Advice getAdvice() {
77         return advice;
78     }
79
80     public Class JavaDoc[] getInterfaces() {
81         return new Class JavaDoc[] {this.introducedInterface};
82     }
83
84 }
85
Popular Tags