KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > advice > AbstractAdvice


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.advice;
23
24 import org.jboss.aop.instrument.Untransformable;
25 import org.jboss.aop.joinpoint.ConstructorCalledByConstructorInvocation;
26 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation;
27 import org.jboss.aop.joinpoint.ConstructorInvocation;
28 import org.jboss.aop.joinpoint.FieldInvocation;
29 import org.jboss.aop.joinpoint.FieldReadInvocation;
30 import org.jboss.aop.joinpoint.FieldWriteInvocation;
31 import org.jboss.aop.joinpoint.Invocation;
32 import org.jboss.aop.joinpoint.MethodCalledByConstructorInvocation;
33 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation;
34 import org.jboss.aop.joinpoint.MethodInvocation;
35
36 import java.lang.reflect.Method JavaDoc;
37
38 /**
39  * Comment
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 37406 $
43  */

44 public abstract class AbstractAdvice implements Interceptor, Untransformable
45 {
46    private static final Class JavaDoc[] INVOCATION_SIGNATURE = {Invocation.class};
47    private static final Class JavaDoc[] METHOD_SIGNATURE = {MethodInvocation.class};
48    private static final Class JavaDoc[] CONSTRUCTOR_SIGNATURE = {ConstructorInvocation.class};
49    private static final Class JavaDoc[] FIELD_SIGNATURE = {FieldInvocation.class};
50    private static final Class JavaDoc[] FIELD_READ_SIGNATURE = {FieldReadInvocation.class};
51    private static final Class JavaDoc[] FIELD_WRITE_SIGNATURE = {FieldWriteInvocation.class};
52    private static final Class JavaDoc[] METHOD_CALLED_BY_METHOD_SIGNATURE = {MethodCalledByMethodInvocation.class};
53    private static final Class JavaDoc[] METHOD_CALLED_BY_CONSTRUCTOR_SIGNATURE = {MethodCalledByConstructorInvocation.class};
54    private static final Class JavaDoc[] CON_CALLED_BY_METHOD_SIGNATURE = {ConstructorCalledByMethodInvocation.class};
55    private static final Class JavaDoc[] CON_CALLED_BY_CONSTRUCTOR_SIGNATURE = {ConstructorCalledByConstructorInvocation.class};
56    protected Method JavaDoc invocationAdvice;
57    protected Method JavaDoc methodAdvice;
58    protected Method JavaDoc constructorAdvice;
59    protected Method JavaDoc fieldAdvice;
60    protected Method JavaDoc fieldReadAdvice;
61    protected Method JavaDoc fieldWriteAdvice;
62    protected Method JavaDoc methodCalledByMethodAdvice;
63    protected Method JavaDoc methodCalledByConstructorAdvice;
64    protected Method JavaDoc conCalledByMethodAdvice;
65    protected Method JavaDoc conCalledByConstructorAdvice;
66    protected Class JavaDoc aspectClass;
67    protected String JavaDoc adviceName;
68
69    protected void init(String JavaDoc advice, Class JavaDoc aspectClass)
70    {
71       this.aspectClass = aspectClass;
72       this.adviceName = advice;
73       invocationAdvice = findByInvocation(adviceName, aspectClass);
74       if (invocationAdvice == null)
75       {
76          methodAdvice = findByMethodInvocation(adviceName, aspectClass);
77          constructorAdvice = findByConstructorInvocation(adviceName, aspectClass);
78          fieldAdvice = findByFieldInvocation(adviceName, aspectClass);
79          if (fieldAdvice == null)
80          {
81             fieldReadAdvice = findByFieldReadInvocation(adviceName, aspectClass);
82             fieldWriteAdvice = findByFieldWriteInvocation(adviceName, aspectClass);
83          }
84          methodCalledByMethodAdvice = findByMethodCalledByMethodInvocation(adviceName, aspectClass);
85          methodCalledByConstructorAdvice = findByMethodCalledByConstructorInvocation(adviceName, aspectClass);
86          conCalledByMethodAdvice = findByConstructorCalledByMethodInvocation(adviceName, aspectClass);
87          conCalledByConstructorAdvice = findByConstructorCalledByConstructorInvocation(adviceName, aspectClass);
88       }
89    }
90
91    protected static Method JavaDoc findByInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
92    {
93       try
94       {
95          return clazz.getMethod(adviceName, INVOCATION_SIGNATURE);
96       }
97       catch (NoSuchMethodException JavaDoc e)
98       {
99          // complete
100
}
101       return null;
102    }
103
104    protected static Method JavaDoc findByMethodInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
105    {
106       try
107       {
108          return clazz.getMethod(adviceName, METHOD_SIGNATURE);
109       }
110       catch (NoSuchMethodException JavaDoc e)
111       {
112          // complete
113
}
114       return null;
115    }
116
117    protected static Method JavaDoc findByFieldInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
118    {
119       try
120       {
121          return clazz.getMethod(adviceName, FIELD_SIGNATURE);
122       }
123       catch (NoSuchMethodException JavaDoc e)
124       {
125          // complete
126
}
127       return null;
128    }
129
130    protected static Method JavaDoc findByFieldReadInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
131    {
132       try
133       {
134          return clazz.getMethod(adviceName, FIELD_READ_SIGNATURE);
135       }
136       catch (NoSuchMethodException JavaDoc e)
137       {
138          // complete
139
}
140       return null;
141    }
142
143    protected static Method JavaDoc findByFieldWriteInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
144    {
145       try
146       {
147          return clazz.getMethod(adviceName, FIELD_WRITE_SIGNATURE);
148       }
149       catch (NoSuchMethodException JavaDoc e)
150       {
151          // complete
152
}
153       return null;
154    }
155
156    protected static Method JavaDoc findByConstructorInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
157    {
158       try
159       {
160          return clazz.getMethod(adviceName, CONSTRUCTOR_SIGNATURE);
161       }
162       catch (NoSuchMethodException JavaDoc e)
163       {
164          // complete
165
}
166       return null;
167    }
168
169    protected static Method JavaDoc findByMethodCalledByMethodInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
170    {
171       try
172       {
173          return clazz.getMethod(adviceName, METHOD_CALLED_BY_METHOD_SIGNATURE);
174       }
175       catch (NoSuchMethodException JavaDoc e)
176       {
177          // complete
178
}
179       return null;
180    }
181
182    protected static Method JavaDoc findByMethodCalledByConstructorInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
183    {
184       try
185       {
186          return clazz.getMethod(adviceName, METHOD_CALLED_BY_CONSTRUCTOR_SIGNATURE);
187       }
188       catch (NoSuchMethodException JavaDoc e)
189       {
190          // complete
191
}
192       return null;
193    }
194
195    protected static Method JavaDoc findByConstructorCalledByMethodInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
196    {
197       try
198       {
199          return clazz.getMethod(adviceName, CON_CALLED_BY_METHOD_SIGNATURE);
200       }
201       catch (NoSuchMethodException JavaDoc e)
202       {
203          // complete
204
}
205       return null;
206    }
207
208    protected static Method JavaDoc findByConstructorCalledByConstructorInvocation(String JavaDoc adviceName, Class JavaDoc clazz)
209    {
210       try
211       {
212          return clazz.getMethod(adviceName, CON_CALLED_BY_CONSTRUCTOR_SIGNATURE);
213       }
214       catch (NoSuchMethodException JavaDoc e)
215       {
216          // complete
217
}
218       return null;
219    }
220
221    protected Method JavaDoc resolveAdvice(Invocation invocation)
222    {
223       if (invocationAdvice != null) return invocationAdvice;
224       if (invocation instanceof MethodInvocation)
225       {
226          if (methodAdvice == null)
227          {
228             throw new IllegalStateException JavaDoc("Unable to resolve MethodInvocation advice " + getName());
229          }
230          return methodAdvice;
231       }
232       if (invocation instanceof FieldInvocation)
233       {
234          if (fieldAdvice != null) return fieldAdvice;
235          if (invocation instanceof FieldReadInvocation)
236          {
237             if (fieldReadAdvice == null)
238             {
239                throw new IllegalStateException JavaDoc("Unable to resolve FieldReadInvocation advice " + getName());
240             }
241             return fieldReadAdvice;
242          }
243          if (invocation instanceof FieldWriteInvocation)
244          {
245             if (fieldWriteAdvice == null)
246             {
247                throw new IllegalStateException JavaDoc("Unable to resolve FieldWriteInvocation advice " + getName());
248             }
249             return fieldWriteAdvice;
250          }
251       }
252       if (invocation instanceof ConstructorInvocation)
253       {
254          if (constructorAdvice == null)
255          {
256             throw new IllegalStateException JavaDoc("Unable to resolve ConstructorInvocation advice " + getName());
257          }
258          return constructorAdvice;
259       }
260       if (invocation instanceof MethodCalledByMethodInvocation)
261       {
262          if (methodCalledByMethodAdvice == null)
263          {
264             throw new IllegalStateException JavaDoc("Unable to resolve MethodCalledByMethodInvocation advice " + getName());
265          }
266          return methodCalledByMethodAdvice;
267       }
268       if (invocation instanceof MethodCalledByConstructorInvocation)
269       {
270          if (methodCalledByConstructorAdvice == null)
271          {
272             throw new IllegalStateException JavaDoc("Unable to resolve MethodCalledByConstructorInvocation advice " + getName());
273          }
274          return methodCalledByConstructorAdvice;
275       }
276       if (invocation instanceof ConstructorCalledByMethodInvocation)
277       {
278          if (conCalledByMethodAdvice == null)
279          {
280             throw new IllegalStateException JavaDoc("Unable to resolve ConstructorCalledByMethodInvocation advice " + getName());
281          }
282          return conCalledByMethodAdvice;
283       }
284       if (invocation instanceof ConstructorCalledByConstructorInvocation)
285       {
286          if (conCalledByMethodAdvice == null)
287          {
288             throw new IllegalStateException JavaDoc("Unable to resolve ConstructorCalledByConstructorInvocation advice " + getName());
289          }
290          return conCalledByConstructorAdvice;
291       }
292       throw new RuntimeException JavaDoc("Should Be Unreachable, but unable to discover Advice");
293
294    }
295 }
296
Popular Tags