KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > MixinInstance


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

7 package com.tirsen.nanning;
8
9 import java.io.Serializable JavaDoc;
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.*;
13
14
15 /**
16  * TODO document AspectDefinition
17  *
18  * <!-- $Id: MixinInstance.java,v 1.15 2003/06/11 15:13:32 lecando Exp $ -->
19  *
20  * @author $Author: lecando $
21  * @version $Revision: 1.15 $
22  */

23 public final class MixinInstance implements Serializable JavaDoc {
24     static final long serialVersionUID = 7386027290257587762L;
25
26     private Class JavaDoc interfaceClass;
27     private Object JavaDoc target;
28
29     private transient Map methodInterceptors = new HashMap();
30
31     public MixinInstance() {
32     }
33
34     public MixinInstance(Class JavaDoc interfaceClass, Object JavaDoc target) {
35         setInterfaceClass(interfaceClass);
36         setTarget(target);
37     }
38
39     public void setInterfaceClass(Class JavaDoc interfaceClass) {
40         this.interfaceClass = interfaceClass;
41     }
42
43     public void setTarget(Object JavaDoc target) {
44         this.target = target;
45     }
46
47     public Class JavaDoc getInterfaceClass() {
48         return interfaceClass;
49     }
50
51     public Set getAllInterceptors() {
52         Set allInterceptors = new HashSet();
53         if (methodInterceptors != null) {
54             for (Iterator methodIterator = methodInterceptors.values().iterator(); methodIterator.hasNext();) {
55                 List interceptors = (List) methodIterator.next();
56                 for (Iterator interceptorIterator = interceptors.iterator(); interceptorIterator.hasNext();) {
57                     Interceptor interceptor = (Interceptor) interceptorIterator.next();
58                     allInterceptors.add(interceptor);
59                 }
60             }
61         }
62         return allInterceptors;
63     }
64
65     public Object JavaDoc getTarget() {
66         return target;
67     }
68
69     public List getInterceptorsForMethod(Method JavaDoc method) {
70         if (methodInterceptors == null) {
71             methodInterceptors = new HashMap();
72         }
73         List interceptors = (List) methodInterceptors.get(method);
74         if (interceptors == null) {
75             interceptors = new ArrayList();
76             methodInterceptors.put(method, interceptors);
77         }
78         return interceptors;
79     }
80
81     class InvocationImpl implements Invocation {
82         private Object JavaDoc proxy;
83         private final Method JavaDoc method;
84         private final Object JavaDoc[] args;
85         private ListIterator interceptors;
86
87         public InvocationImpl(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) {
88             this.proxy = proxy;
89             this.method = method;
90             this.args = args;
91             interceptors = getInterceptorsForMethod(method).listIterator();
92         }
93
94         public Object JavaDoc invokeNext() throws Throwable JavaDoc {
95             if (interceptors.hasNext()) {
96                 return ((MethodInterceptor) interceptors.next()).invoke(this);
97             } else {
98                 try {
99                     return method.invoke(getTarget(), args);
100                 } catch (InvocationTargetException JavaDoc e) {
101                     throwRealException(e);
102                     throw e;
103                 }
104             }
105         }
106
107         private void throwRealException(InvocationTargetException JavaDoc e) throws Exception JavaDoc {
108             Throwable JavaDoc realException = e.getTargetException();
109             if (realException instanceof Error JavaDoc) {
110                 throw (Error JavaDoc) realException;
111             } else if (realException instanceof RuntimeException JavaDoc) {
112                 throw (RuntimeException JavaDoc) realException;
113             } else {
114                 throw (Exception JavaDoc) realException;
115             }
116         }
117
118         public Interceptor getInterceptor(int index) {
119             return (Interceptor) getInterceptorsForMethod(method).get(index);
120         }
121
122         public Class JavaDoc getTargetInterface() {
123             return getInterfaceClass();
124         }
125
126         public AspectInstance getAspectInstance() {
127             return Aspects.getAspectInstance(getProxy());
128         }
129
130         public Object JavaDoc getArg(int arg) {
131             return args[arg];
132         }
133
134         public Object JavaDoc getTarget() {
135             return MixinInstance.this.getTarget();
136         }
137
138         public void setTarget(Object JavaDoc target) {
139             MixinInstance.this.setTarget(target);
140         }
141
142         public Object JavaDoc getProxy() {
143             return proxy;
144         }
145
146         public int getCurrentIndex() {
147             return interceptors.previousIndex();
148         }
149
150         public int getNumberOfInterceptors() {
151             return getInterceptorsForMethod(method).size();
152         }
153
154         public Method JavaDoc getMethod() {
155             return method;
156         }
157
158         public Object JavaDoc[] getArgs() {
159             return args;
160         }
161     }
162
163     public Object JavaDoc invokeMethod(Object JavaDoc proxy,
164                                Method JavaDoc method,
165                                Object JavaDoc[] args)
166             throws Throwable JavaDoc {
167         Invocation invocation = new InvocationImpl(proxy, method, args);
168         return invocation.invokeNext();
169     }
170
171     /**
172      * Adds this interceptor to all methods.
173      * @param interceptor
174      */

175     public void addInterceptor(Interceptor interceptor) {
176         assert !(interceptor instanceof ConstructionInterceptor) : "Construction interceptors are added on the aspect instance";
177         Method JavaDoc[] methods = getAllMethods();
178         for (int i = 0; i < methods.length; i++) {
179             Method JavaDoc method = methods[i];
180             if (interceptor instanceof FilterMethodsInterceptor) {
181                 FilterMethodsInterceptor filterMethodsInterceptor = (FilterMethodsInterceptor) interceptor;
182                 if (filterMethodsInterceptor.interceptsMethod(method)) {
183                     addInterceptor(method, (MethodInterceptor) interceptor);
184                 }
185             } else {
186                 addInterceptor(method, (MethodInterceptor) interceptor);
187             }
188         }
189     }
190
191     public boolean equals(Object JavaDoc o) {
192         if (this == o) return true;
193         if (!(o instanceof MixinInstance)) return false;
194
195         final MixinInstance mixinInstance = (MixinInstance) o;
196
197         if (interfaceClass != null ? !interfaceClass.equals(mixinInstance.interfaceClass) : mixinInstance.interfaceClass != null) return false;
198         if (target != null ? !target.equals(mixinInstance.target) : mixinInstance.target != null) return false;
199
200         return true;
201     }
202
203     public int hashCode() {
204         int result;
205         result = (interfaceClass != null ? interfaceClass.hashCode() : 0);
206         result = 29 * result + (target != null ? target.hashCode() : 0);
207         return result;
208     }
209
210     /**
211      * Adds interceptor to specified method.
212      * @param method
213      * @param interceptor
214      */

215     public void addInterceptor(Method JavaDoc method, MethodInterceptor interceptor) {
216         getInterceptorsForMethod(method).add(interceptor);
217     }
218
219     public Method JavaDoc[] getAllMethods() {
220         return interfaceClass.getMethods();
221     }
222
223     public String JavaDoc toString() {
224         return "mixin{" + getTarget() + "}";
225     }
226 }
227
Popular Tags