KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > definition > AdviceDefinition


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.definition;
5
6
7 import com.tc.aspectwerkz.DeploymentModel;
8 import com.tc.aspectwerkz.reflect.MethodInfo;
9 import com.tc.aspectwerkz.expression.ExpressionInfo;
10 import com.tc.aspectwerkz.aspect.AdviceType;
11 import com.tc.aspectwerkz.cflow.CflowBinding;
12 import com.tc.aspectwerkz.util.Strings;
13
14 import java.util.List JavaDoc;
15
16 /**
17  * Holds the meta-data for the advices.
18  *
19  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Boner </a>
20  */

21 public class AdviceDefinition {
22
23   /**
24    * The name of the advice.
25    * It is the advice method name and optionnaly the call signature.
26    * e.g. advice or advice() or advice(JoinPoint jp) or myadvice(JoinPoint myJp , java.lang.String foo) ...
27    */

28   private String JavaDoc m_name;
29
30   /**
31    * The type of the advice.
32    */

33   private AdviceType m_type;
34
35   /**
36    * The aspect class name.
37    */

38   private final String JavaDoc m_aspectClassName;
39
40   /**
41    * The aspect name.
42    */

43   private final String JavaDoc m_aspectName;
44
45   /**
46    * The pointcut expression.
47    */

48   private ExpressionInfo m_expressionInfo;
49
50   /**
51    * The method for the advice.
52    */

53   private final MethodInfo m_method;
54
55   /**
56    * The attribute for the advice.
57    */

58   private String JavaDoc m_attribute = "";
59
60   /**
61    * The aspect definition holding this advice definition.
62    */

63   private AspectDefinition m_aspectDefinition;
64
65   /**
66    * The special arg type, such as returning(TYPE) or throwing(TYPE).
67    */

68   private String JavaDoc m_specialArgumentType;
69
70   /**
71    * Indicates if this advice will need a cflow or cflowbelow runtime check
72    */

73   private boolean m_hasCflowOrCflowBelow = false;
74
75   /**
76    * TODO only use this method and make ctor private?
77    * <p/>
78    * Creates a new advice definition.
79    *
80    * @param adviceName the advice name
81    * @param adviceType the advice type
82    * @param expression the advice expression
83    * @param specialArgumentType the arg
84    * @param aspectName the aspect name
85    * @param aspectClassName the aspect class name
86    * @param method the advice method
87    * @param aspectDef the aspect definition
88    * @return the new advice definition
89    */

90   public static AdviceDefinition newInstance(final String JavaDoc adviceName,
91                                              final AdviceType adviceType,
92                                              final String JavaDoc expression,
93                                              final String JavaDoc specialArgumentType,
94                                              final String JavaDoc aspectName,
95                                              final String JavaDoc aspectClassName,
96                                              final MethodInfo method,
97                                              final AspectDefinition aspectDef) {
98     ExpressionInfo expressionInfo = new ExpressionInfo(
99             expression,
100             aspectDef.getQualifiedName()
101     );
102
103     // support for pointcut signature
104
String JavaDoc adviceCallSignature = null;
105     String JavaDoc resolvedSpecialArgumentType = specialArgumentType;
106     if (adviceName.indexOf('(') > 0) {
107       adviceCallSignature = adviceName.substring(adviceName.indexOf('(') + 1, adviceName.lastIndexOf(')'));
108       String JavaDoc[] parameters = Strings.splitString(adviceCallSignature, ",");
109       for (int i = 0; i < parameters.length; i++) {
110         String JavaDoc[] parameterInfo = Strings.splitString(
111                 Strings.replaceSubString(parameters[i].trim(), " ", " "),
112                 " "
113         );
114         // skip the parameter if this ones is a after returning / throwing binding
115
if (parameterInfo[1].equals(specialArgumentType)) {
116           resolvedSpecialArgumentType = parameterInfo[0];
117           expressionInfo.setSpecialArgumentName(parameterInfo[1]);
118         } else {
119           expressionInfo.addArgument(
120                   parameterInfo[1],
121                   parameterInfo[0],
122                   aspectDef.getClassInfo().getClassLoader()
123           );
124         }
125       }
126     }
127
128     return new AdviceDefinition(
129             adviceName,
130             adviceType,
131             resolvedSpecialArgumentType,
132             aspectName,
133             aspectClassName,
134             expressionInfo,
135             method,
136             aspectDef
137     );
138   }
139
140   /**
141    * ==== CAUTION: do not use directly in the normal case - use newInstance() ===
142    * <p/>
143    * Creates a new advice meta-data instance.
144    *
145    * @param name the name of the expressionInfo
146    * @param type the type of the advice
147    * @param specialArgumentType the special arg type, such as returning(TYPE) or throwing(TYPE)
148    * @param aspectName the name of the aspect
149    * @param aspectClassName the class name of the aspect
150    * @param expressionInfo the expressionInfo
151    * @param methodInfo the methodInfo
152    */

153   public AdviceDefinition(final String JavaDoc name,
154                           final AdviceType type,
155                           final String JavaDoc specialArgumentType,
156                           final String JavaDoc aspectName,
157                           final String JavaDoc aspectClassName,
158                           final ExpressionInfo expressionInfo,
159                           final MethodInfo methodInfo,
160                           final AspectDefinition aspectDef) {
161     if (name == null) {
162       throw new IllegalArgumentException JavaDoc("name can not be null");
163     }
164     if (type == null) {
165       throw new IllegalArgumentException JavaDoc("illegal advice type");
166     }
167     if (aspectName == null) {
168       throw new IllegalArgumentException JavaDoc("aspect name can not be null");
169     }
170     if (aspectClassName == null) {
171       throw new IllegalArgumentException JavaDoc("class name can not be null");
172     }
173     if (methodInfo == null) {
174       throw new IllegalArgumentException JavaDoc("methodInfo can not be null");
175     }
176     if (aspectDef == null) {
177       throw new IllegalArgumentException JavaDoc("aspect definition can not be null");
178     }
179     m_name = name;
180     m_type = type;
181     m_specialArgumentType = specialArgumentType;
182     m_aspectName = aspectName;
183     m_aspectClassName = aspectClassName;
184     m_expressionInfo = expressionInfo;
185     m_method = methodInfo;
186     m_aspectDefinition = aspectDef;
187
188     // getDefault the cflow Advice bindings to know if this advice binding is using cflow or cflowbelow
189
List JavaDoc cflowBindings = CflowBinding.getCflowBindingsForCflowOf(m_expressionInfo);
190     m_hasCflowOrCflowBelow = (cflowBindings.size() > 0);
191   }
192
193   /**
194    * Returns the advice type.
195    *
196    * @return the advice type
197    */

198   public AdviceType getType() {
199     return m_type;
200   }
201
202   /**
203    * Returns the name of the advice.
204    *
205    * @return the name
206    */

207   public String JavaDoc getName() {
208     return m_name;
209   }
210
211   /**
212    * Returns the fully qualified name for the advice
213    *
214    * @return the fully qualified name
215    */

216   public String JavaDoc getQualifiedName() {
217     return m_aspectDefinition.getQualifiedName() + '.' + m_name;
218   }
219
220   /**
221    * Sets the name of the advice.
222    *
223    * @param name the name
224    */

225   public void setName(final String JavaDoc name) {
226     m_name = name.trim();
227   }
228
229   /**
230    * Returns the expression.
231    * <p/>
232    * TODO should return NULL object if null
233    *
234    * @return the expression
235    */

236   public ExpressionInfo getExpressionInfo() {
237     return m_expressionInfo;
238   }
239
240   /**
241    * Sets the expression info.
242    *
243    * @param newExpression the new expression info
244    */

245   public void setExpressionInfo(final ExpressionInfo newExpression) {
246     m_expressionInfo = newExpression;
247     // update the hasCflow caracteristic
248
List JavaDoc cflowBindings = CflowBinding.getCflowBindingsForCflowOf(m_expressionInfo);
249     m_hasCflowOrCflowBelow = (cflowBindings.size() > 0);
250   }
251
252   /**
253    * Returns the class name.
254    *
255    * @return the class name
256    */

257   public String JavaDoc getAspectClassName() {
258     return m_aspectClassName;
259   }
260
261   /**
262    * Returns the aspect name.
263    *
264    * @return the aspect name
265    */

266   public String JavaDoc getAspectName() {
267     return m_aspectName;
268   }
269
270   /**
271    * Returns the special arg type, such as returning(TYPE) or throwing(TYPE).
272    *
273    * @return
274    */

275   public String JavaDoc getSpecialArgumentType() {
276     return m_specialArgumentType;
277   }
278
279   /**
280    * Returns the method.
281    *
282    * @return the method
283    */

284   public MethodInfo getMethodInfo() {
285     return m_method;
286   }
287
288   /**
289    * Returns the the deployment model for the advice
290    *
291    * @return the deployment model
292    */

293   public DeploymentModel getDeploymentModel() {
294     return m_aspectDefinition.getDeploymentModel();
295   }
296
297   /**
298    * Returns the attribute.
299    *
300    * @return the attribute
301    */

302   public String JavaDoc getAttribute() {
303     return m_attribute;
304   }
305
306   /**
307    * Sets the attribute.
308    *
309    * @param attribute the attribute
310    */

311   public void setAttribute(final String JavaDoc attribute) {
312     m_attribute = attribute;
313   }
314
315   /**
316    * Returns the definition for the aspect that defines this advice.
317    *
318    * @return the aspect definition
319    */

320   public AspectDefinition getAspectDefinition() {
321     return m_aspectDefinition;
322   }
323
324   /**
325    * Check if the advice is bound to a pointcut with cflow or cflowbelow
326    *
327    * @return
328    */

329   public boolean hasCflowOrCflowBelow() {
330     return m_hasCflowOrCflowBelow;
331   }
332
333   /**
334    * Deep copy of the definition.
335    *
336    * @param expressionInfo
337    * @return
338    */

339   public AdviceDefinition copyAt(final ExpressionInfo expressionInfo) {
340     return new AdviceDefinition(
341             getName(),
342             getType(),
343             getSpecialArgumentType(),
344             getAspectName(),
345             getAspectClassName(),
346             expressionInfo,
347             getMethodInfo(),
348             m_aspectDefinition
349     );
350   }
351
352   /**
353    * Equals and hashcode means we have the same advice if the aspect qualified name (not classname) and
354    * advice name (may include signature) are the same. [AW-439 fix]
355    *
356    * @param o
357    * @return
358    */

359   public boolean equals(Object JavaDoc o) {
360     if (this == o) return true;
361     if (!(o instanceof AdviceDefinition)) return false;
362
363     final AdviceDefinition adviceDefinition = (AdviceDefinition) o;
364
365     if (!m_aspectName.equals(adviceDefinition.m_aspectName)) return false;
366     if (!m_name.equals(adviceDefinition.m_name)) return false;
367
368     return true;
369   }
370
371   public int hashCode() {
372     int result;
373     result = m_name.hashCode();
374     result = 29 * result + m_aspectName.hashCode();
375     return result;
376   }
377 }
Popular Tags