KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > definition > AspectDefinition


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package com.tirsen.nanning.definition;
8
9 import java.lang.reflect.Method JavaDoc;
10 import java.util.*;
11
12 import com.tirsen.nanning.MethodInterceptor;
13 import com.tirsen.nanning.MixinInstance;
14
15 /**
16  * Defines an interface that's to be added to an aspected object.
17  *
18  * <!-- $Id: AspectDefinition.java,v 1.9 2003/05/23 07:43:40 lecando Exp $ -->
19  *
20  * @author $Author: lecando $
21  * @version $Revision: 1.9 $
22  *
23  * @deprecated please use the new {@link com.tirsen.nanning.config.AspectSystem} framework instead.
24  */

25 public class AspectDefinition {
26     private Class JavaDoc interfaceClass;
27     private List interceptorDefinitions = new ArrayList();
28     private Class JavaDoc targetClass;
29     private Map methodsToIndex;
30
31     /**
32      * Specify interface to use.
33      *
34      * @param interfaceClass
35      */

36     public void setInterface(Class JavaDoc interfaceClass) {
37         this.interfaceClass = interfaceClass;
38         methodsToIndex = new HashMap();
39         Method JavaDoc[] methods = interfaceClass.getMethods();
40         for (int i = 0; i < methods.length; i++) {
41             Method JavaDoc method = methods[i];
42             methodsToIndex.put(method, new Integer JavaDoc(i));
43         }
44     }
45
46     /**
47      * Adds an interceptor to the chain of interceptors. Note: if you use this utility-method (that automatically
48      * creates an {@link com.tirsen.nanning.definition.InterceptorDefinition}) stateless interceptors
49      *
50      * @param interceptorClass
51      */

52     public void addInterceptor(Class JavaDoc interceptorClass) {
53         addInterceptor(new InterceptorDefinition(interceptorClass));
54     }
55
56     /**
57      * Adds an interceptor to the chain of interceptors.
58      *
59      * @param interceptorDefinition
60      */

61     public void addInterceptor(InterceptorDefinition interceptorDefinition) {
62         interceptorDefinitions.add(interceptorDefinition);
63     }
64
65     /**
66      * Specify target-object to use.
67      *
68      * @param targetClass
69      */

70     public void setTarget(Class JavaDoc targetClass) {
71         this.targetClass = targetClass;
72     }
73
74     MixinInstance createMixinInstance()
75             throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
76         if (targetClass != null) {
77             return newInstance(targetClass.newInstance());
78         } else {
79             return newInstance(null);
80         }
81     }
82
83     public Class JavaDoc getInterfaceClass() {
84         return interfaceClass;
85     }
86
87     MixinInstance newInstance(Object JavaDoc target) {
88         checkTarget(target);
89
90         MixinInstance mixinInstance = new MixinInstance();
91         mixinInstance.setInterfaceClass(getInterfaceClass());
92
93         for (Iterator iterator = interceptorDefinitions.iterator(); iterator.hasNext();) {
94             InterceptorDefinition interceptorDefinition = (InterceptorDefinition) iterator.next();
95             Method JavaDoc[] methods = mixinInstance.getAllMethods();
96             for (int j = 0; j < methods.length; j++) {
97                 Method JavaDoc method = methods[j];
98                 if (interceptorDefinition.interceptsMethod(method)) {
99                     mixinInstance.addInterceptor(method, (MethodInterceptor) interceptorDefinition.newInstance());
100                 }
101             }
102         }
103
104         mixinInstance.setTarget(target);
105         return mixinInstance;
106     }
107
108     private void checkTarget(Object JavaDoc target) {
109         if (target == null) {
110             return;
111         }
112
113         if (!interfaceClass.isInstance(target)) {
114             throw new IllegalArgumentException JavaDoc("target does not implement interface: " + target);
115         }
116         if (!targetClass.isInstance(target)) {
117             throw new IllegalArgumentException JavaDoc("target is not an instance of target-class: " + target);
118         }
119     }
120
121     public List getConstructionInterceptors() {
122         List interceptors = new ArrayList();
123         for (Iterator iterator = interceptorDefinitions.iterator(); iterator.hasNext();) {
124             InterceptorDefinition interceptorDefinition = (InterceptorDefinition) iterator.next();
125             if (interceptorDefinition.interceptsConstructor(getInterfaceClass())) {
126                 interceptors.add(interceptorDefinition.newInstance());
127             }
128         }
129         return interceptors;
130     }
131
132     public Collection getInterceptorDefinitions() {
133         return interceptorDefinitions;
134     }
135 }
136
Popular Tags