KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > args > ArgsAspect


1 /**************************************************************************************
2  * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package test.args;
9
10 import org.codehaus.aspectwerkz.definition.Pointcut;
11 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
12 import test.Loggable;
13
14 /**
15  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur</a>
16  */

17 public class ArgsAspect {
18
19     //-- Method execution pointcuts with args
20

21     /**
22      * @Expression within(test.args.ArgsAdviceTest)
23      */

24     Pointcut in_scope;
25
26     /**
27      * @Expression in_scope && execution(* matchAll(..)) && args(String, String, long)
28      */

29     Pointcut pc_matchAll;
30
31     /**
32      * @Expression in_scope && execution(* matchAllWithWildcard(..)) && args(..)
33      */

34     Pointcut pc_matchAllWithWildcard;
35
36     /**
37      * @Expression in_scope && execution(* getFirst(..)) && args(s, ..)
38      */

39     void pc_getFirst(String JavaDoc s) {
40         ;
41     }// here we use "return void" style
42

43     /**
44      * @Expression in_scope && execution(* changeArg(..)) && args(String, s, long)
45      */

46     Pointcut pc_changeArg(StringBuffer JavaDoc s) {
47         return null;
48     }// here we use "return null" style
49

50     /**
51      * @Expression in_scope && execution(* orderChangedInPointcutSignature(..)) && args(s0, s1, long)
52      */

53     Pointcut pc_orderChangedInPointcutSignature(String JavaDoc s1, String JavaDoc s0) {
54         return null;
55     }
56
57     /**
58      * @Expression in_scope && execution(* orderChangedInAdviceSignature(..)) && args(s0, s1, long)
59      */

60     Pointcut pc_orderChangedInAdviceSignature(String JavaDoc s0, String JavaDoc s1) {
61         return null;
62     }
63
64     /**
65      * @Expression in_scope && execution(* orderChangedInPointcutAndAdviceSignature(..)) && args(s0, s1, long)
66      */

67     Pointcut pc_orderChangedInPointcutAndAdviceSignature(String JavaDoc s1, String JavaDoc s0) {
68         return null;
69     }
70
71     /**
72      * @Before in_scope && execution(* singleAndDotDot(..)) && args(i)
73      */

74     public void singleAndDotDot(JoinPoint joinPoint, int i) {
75         ((Loggable) joinPoint.getTarget()).log("before " + i + " ");
76     }
77
78     /**
79      * @Before in_scope && execution(* withArray(..)) && args(l, s, matrix)
80      */

81     public void withArray(JoinPoint joinPoint, long l, String JavaDoc s, int[][] matrix) {
82         String JavaDoc iis = "";
83         for (int i = 0; i < matrix.length; i++) {
84             for (int j = 0; j < matrix[i].length; j++) {
85                 iis += ""+matrix[i][j]+"-";
86             }
87         }
88         ((Loggable) joinPoint.getTarget()).log("before " + l + " " + s + " " + iis + " ");
89     }
90
91     /**
92      * @Before pc_matchAll || pc_matchAllWithWildcard
93      */

94     public void matchAllBefore(JoinPoint joinPoint) {
95         ((Loggable) joinPoint.getTarget()).log("before ");
96     }
97
98     /**
99      * @After pc_matchAll || pc_matchAllWithWildcard
100      */

101     public void matchAllAfter(JoinPoint joinPoint) {
102         ((Loggable) joinPoint.getTarget()).log("after ");
103     }
104
105     /**
106      * @Around pc_matchAll || pc_matchAllWithWildcard
107      */

108     public Object JavaDoc matchAllAround(JoinPoint joinPoint) throws Throwable JavaDoc {
109         ((Loggable) joinPoint.getTarget()).log("before1 ");
110         Object JavaDoc res = joinPoint.proceed();
111         ((Loggable) joinPoint.getTarget()).log("after1 ");
112         return res;
113     }
114
115
116     /**
117      * @Before pc_getFirst(as)
118      */

119     public void getFirstBefore(JoinPoint joinPoint, String JavaDoc as) {
120         ((Loggable) joinPoint.getTarget()).log("before " + as + " ");
121     }
122
123     /**
124      * @After pc_getFirst(as)
125      */

126     public void getFirstAfter(String JavaDoc as, JoinPoint joinPoint) {//here we use some fancy order in the signature
127
((Loggable) joinPoint.getTarget()).log("after " + as + " ");
128     }
129
130     /**
131      * @Around pc_getFirst(as)
132      */

133     public Object JavaDoc getFirstAround(JoinPoint joinPoint, String JavaDoc as) throws Throwable JavaDoc {
134         ((Loggable) joinPoint.getTarget()).log("before1 " + as + " ");
135         Object JavaDoc res = joinPoint.proceed();
136         ((Loggable) joinPoint.getTarget()).log("after1 " + as + " ");
137         return res;
138     }
139
140     /**
141      * @Before in_scope && execution(* getFirstAnonymous(..)) && args(as,String,..)
142      */

143     public void getFirstAnonymousBefore(JoinPoint joinPoint, String JavaDoc as) {
144         ((Loggable) joinPoint.getTarget()).log("before " + as + " ");
145     }
146
147     /**
148      * @After in_scope && execution(* getFirstAnonymous(..)) && args(as, String, long)
149      */

150     public void getFirstAnonymousAfter(String JavaDoc as, JoinPoint joinPoint) {
151         ((Loggable) joinPoint.getTarget()).log("after " + as + " ");
152     }
153
154     /**
155      * @Around in_scope && execution(* getFirstAnonymous(..)) && args(as,..)
156      */

157     public Object JavaDoc getFirstAnonymousAround(JoinPoint joinPoint, String JavaDoc as) throws Throwable JavaDoc {
158         ((Loggable) joinPoint.getTarget()).log("before1 " + as + " ");
159         Object JavaDoc res = joinPoint.proceed();
160         ((Loggable) joinPoint.getTarget()).log("after1 " + as + " ");
161         return res;
162     }
163
164     /**
165      * @Before pc_changeArg(as)
166      */

167     public void changeArgBefore(JoinPoint joinPoint, StringBuffer JavaDoc as) {
168         as.append("x");
169         ((Loggable) joinPoint.getTarget()).log("before " + as.toString() + " ");
170     }
171
172     /**
173      * @After pc_changeArg(as)
174      */

175     public void changeArgAfter(JoinPoint joinPoint, StringBuffer JavaDoc as) {
176         as.append("x");
177         ((Loggable) joinPoint.getTarget()).log("after " + as.toString() + " ");
178     }
179
180     /**
181      * @Around pc_changeArg(as)
182      */

183     public Object JavaDoc changeArgAround(StringBuffer JavaDoc as, JoinPoint joinPoint) throws Throwable JavaDoc {//here we use some fancy order in the signature
184
as.append("x");
185         ((Loggable) joinPoint.getTarget()).log("before1 " + as.toString() + " ");
186         Object JavaDoc res = joinPoint.proceed();
187         as.append("x");
188         ((Loggable) joinPoint.getTarget()).log("after1 " + as.toString() + " ");
189         return res;
190     }
191
192
193     /**
194      * @Before pc_orderChangedInPointcutSignature(as0, as1)
195      */

196     public void orderChangedInPointcutSignatureBefore(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) {
197         ((Loggable) joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
198     }
199
200     /**
201      * @After pc_orderChangedInPointcutSignature(as0, as1)
202      */

203     public void orderChangedInPointcutSignatureAfter(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) {
204         ((Loggable) joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
205     }
206
207     /**
208      * @Around pc_orderChangedInPointcutSignature(as0, as1)
209      */

210     public Object JavaDoc orderChangedInPointcutSignatureAround(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) throws Throwable JavaDoc {
211         ((Loggable) joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
212         Object JavaDoc res = joinPoint.proceed();
213         ((Loggable) joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
214         return res;
215     }
216
217
218     /**
219      * @Before pc_orderChangedInAdviceSignature(as1, as0)
220      */

221     public void orderChangedInAdviceSignatureBefore(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) {
222         ((Loggable) joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
223     }
224
225     /**
226      * @After pc_orderChangedInAdviceSignature(as1, as0)
227      */

228     public void orderChangedInAdviceSignatureAfter(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) {
229         ((Loggable) joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
230     }
231
232     /**
233      * @Around pc_orderChangedInAdviceSignature(as1, as0)
234      */

235     public Object JavaDoc orderChangedInAdviceSignatureAround(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) throws Throwable JavaDoc {
236         ((Loggable) joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
237         Object JavaDoc res = joinPoint.proceed();
238         ((Loggable) joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
239         return res;
240     }
241
242
243     /**
244      * @Before pc_orderChangedInPointcutAndAdviceSignature(as1, as0)
245      */

246     public void orderChangedInPointcutAndAdviceSignatureBefore(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) {
247         ((Loggable) joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
248     }
249
250     /**
251      * @After pc_orderChangedInPointcutAndAdviceSignature(as1, as0)
252      */

253     public void orderChangedInPointcutAndAdviceSignatureAfter(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1) {
254         ((Loggable) joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
255     }
256
257     /**
258      * @Around pc_orderChangedInPointcutAndAdviceSignature(as1, as0)
259      */

260     public Object JavaDoc orderChangedInPointcutAndAdviceSignatureAround(JoinPoint joinPoint, String JavaDoc as0, String JavaDoc as1)
261             throws Throwable JavaDoc {
262         ((Loggable) joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
263         Object JavaDoc res = joinPoint.proceed();
264         ((Loggable) joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
265         return res;
266     }
267
268     //-- Method call pointcuts with args
269

270     /**
271      * @Expression in_scope && call(* callGetFirstAndSecond(..)) && args(l, s)
272      */

273     void pc_callGetFirstAndSecond(long l, String JavaDoc[] s) {
274     };
275
276     /**
277      * @Before pc_callGetFirstAndSecond(l, s)
278      */

279     public void callGetFirstAndSecondBefore(JoinPoint joinPoint, long l, String JavaDoc[] s) {
280         ((Loggable) joinPoint.getTarget()).log("before " + l + " " + s[0] + "," + s[1] + " ");
281     }
282
283     /**
284      * @After pc_callGetFirstAndSecond(l, s)
285      */

286     public void callGetFirstAndSecondAfter(JoinPoint joinPoint, long l, String JavaDoc[] s) {
287         ((Loggable) joinPoint.getTarget()).log("after " + l + " " + s[0] + "," + s[1] + " ");
288     }
289
290     /**
291      * @Around pc_callGetFirstAndSecond(l, s)
292      */

293     public Object JavaDoc callGetFirstAndSecondAround(JoinPoint joinPoint, long l, String JavaDoc[] s) throws Throwable JavaDoc {
294         ((Loggable) joinPoint.getTarget()).log("before1 " + l + " " + s[0] + "," + s[1] + " ");
295         Object JavaDoc res = joinPoint.proceed();
296         ((Loggable) joinPoint.getTarget()).log("after1 " + l + " " + s[0] + "," + s[1] + " ");
297         return res;
298     }
299
300     //-- Ctor execution pointcuts with args
301
// we are using inner class, so args() is a bit tricky
302

303     /**
304      * @Expression execution(test.args.ArgsAdviceTest$CtorExecution.new(..)) && args(test.args.ArgsAdviceTest, s)
305      */

306     void pc_ctorExecutionGetFirst(String JavaDoc s) {
307     };
308
309     /**
310      * @Before pc_ctorExecutionGetFirst(s)
311      */

312     public void ctorExecutionGetFirstBefore(JoinPoint joinPoint, String JavaDoc s) {
313         ((Loggable) joinPoint.getTarget()).log("before " + s + " ");
314     }
315
316     /**
317      * @After pc_ctorExecutionGetFirst(s)
318      */

319     public void ctorExecutionGetFirstAfter(JoinPoint joinPoint, String JavaDoc s) {
320         ((Loggable) joinPoint.getTarget()).log("after " + s + " ");
321     }
322
323     /**
324      * @Around pc_ctorExecutionGetFirst(s)
325      */

326     public Object JavaDoc ctorExecutionGetFirstAround(JoinPoint joinPoint, String JavaDoc s) throws Throwable JavaDoc {
327         ((Loggable) joinPoint.getTarget()).log("before1 " + s + " ");
328         Object JavaDoc res = joinPoint.proceed();
329         ((Loggable) joinPoint.getTarget()).log("after1 " + s + " ");
330         return res;
331     }
332
333     //-- Ctor call pointcuts with args
334
// we are using inner class, so args() is a bit tricky
335

336     /**
337      * @Expression in_scope && call(test.args.ArgsAdviceTest$CtorCall.new(..)) && args(test.args.ArgsAdviceTest, s)
338      */

339     void pc_ctorCallGetFirst(String JavaDoc s) {
340     };
341
342     /**
343      * @Before pc_ctorCallGetFirst(s)
344      */

345     public void ctorCallGetFirstBefore(JoinPoint joinPoint, String JavaDoc s) {
346         ArgsAdviceTest.logStatic("before " + s + " ");
347     }
348
349     /**
350      * @After pc_ctorCallGetFirst(s)
351      */

352     public void ctorCallGetFirstAfter(JoinPoint joinPoint, String JavaDoc s) {
353         ArgsAdviceTest.logStatic("after " + s + " ");
354     }
355
356     /**
357      * @Around pc_ctorCallGetFirst(s)
358      */

359     public Object JavaDoc ctorCallGetFirstAround(JoinPoint joinPoint, String JavaDoc s) throws Throwable JavaDoc {
360         ArgsAdviceTest.logStatic("before1 " + s + " ");
361         Object JavaDoc res = joinPoint.proceed();
362         ArgsAdviceTest.logStatic("after1 " + s + " ");
363         return res;
364     }
365
366     //-- field set with args()
367
/**
368      * @Expression in_scope && set(* m_field) && args(s)
369      */

370     void pc_mfield(String JavaDoc s) {
371     };
372
373     /**
374      * @Before pc_mfield(s)
375      */

376     public void mfieldBefore(JoinPoint joinPoint, String JavaDoc s) {
377         String JavaDoc fieldValue = ((ArgsAdviceTest) joinPoint.getTarget()).getField();
378         ((Loggable) joinPoint.getTarget()).log("before " + fieldValue + "," + s + " ");
379     }
380
381     /**
382      * @After pc_mfield(s)
383      */

384     public void mfieldAfter(JoinPoint joinPoint, String JavaDoc s) {
385         String JavaDoc fieldValue = ((ArgsAdviceTest) joinPoint.getTarget()).getField();
386         ((Loggable) joinPoint.getTarget()).log("after " + fieldValue + "," + s + " ");
387     }
388
389     /**
390      * @Around pc_mfield(s)
391      */

392     public Object JavaDoc mfieldAround(JoinPoint joinPoint, String JavaDoc s) throws Throwable JavaDoc {
393         String JavaDoc fieldValue = ((ArgsAdviceTest) joinPoint.getTarget()).getField();
394         ((Loggable) joinPoint.getTarget()).log("before1 " + fieldValue + "," + s + " ");
395         s = "changed"; // will be ignored due to delegation ! [AJ]
396
Object JavaDoc res = joinPoint.proceed();
397         fieldValue = ((ArgsAdviceTest) joinPoint.getTarget()).getField();
398         ((Loggable) joinPoint.getTarget()).log("after1 " + fieldValue + "," + s + " ");
399         return "ignored";
400     }
401
402     //-- static field set with args()
403
/**
404      * @Expression in_scope && set(* s_field) && args(s)
405      */

406     void pc_sfield(String JavaDoc s) {
407     };
408
409     /**
410      * @Before pc_sfield(s)
411      */

412     public void sfieldBefore(JoinPoint joinPoint, String JavaDoc s) {
413         String JavaDoc fieldValue = ArgsAdviceTest.getStaticField();
414         ArgsAdviceTest.logStatic("before " + fieldValue + "," + s + " ");
415     }
416
417     /**
418      * @After pc_sfield(s)
419      */

420     public void sfieldAfter(JoinPoint joinPoint, String JavaDoc s) {
421         String JavaDoc fieldValue = ArgsAdviceTest.getStaticField();
422         ArgsAdviceTest.logStatic("after " + fieldValue + "," + s + " ");
423     }
424
425     /**
426      * @Around pc_sfield(s)
427      */

428     public Object JavaDoc sfieldAround(JoinPoint joinPoint, String JavaDoc s) throws Throwable JavaDoc {
429         String JavaDoc fieldValue = ArgsAdviceTest.getStaticField();
430         ArgsAdviceTest.logStatic("before1 " + fieldValue + "," + s + " ");
431         s = "changed"; // will be ignored due to delegation ! [AJ]
432
Object JavaDoc res = joinPoint.proceed();
433         fieldValue = ArgsAdviceTest.getStaticField();
434         ArgsAdviceTest.logStatic("after1 " + fieldValue + "," + s + " ");
435         return "ignored";
436     }
437
438 }
439
Popular Tags