1   /***************************************************************************************
2   
3    * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved.                 *
4   
5    * http://aspectwerkz.codehaus.org                                                    *
6   
7    * ---------------------------------------------------------------------------------- *
8   
9    * The software in this package is published under the terms of the LGPL license      *
10  
11   * a copy of which has been included with this distribution in the license.txt file.  *
12  
13   **************************************************************************************/
14  
15  package test.args;
16  
17  
18  
19  import org.codehaus.aspectwerkz.Pointcut;
20  
21  import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
22  
23  import test.Loggable;
24  
25  
26  
27  /***
28  
29   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
30  
31   */
32  
33  public class ArgsAspect {
34  
35  
36  
37      //-- Method execution pointcuts with args
38  
39  
40  
41      /*** @Expression within(test.args.ArgsAdviceTest) */
42  
43      Pointcut in_scope;
44  
45  
46  
47      /*** @Expression in_scope && execution(* matchAll(..)) && args(String, String, long) */
48  
49      Pointcut pc_matchAll;
50  
51  
52  
53      /*** @Expression in_scope && execution(* matchAllWithWildcard(..)) && args(..) */
54  
55      Pointcut pc_matchAllWithWildcard;
56  
57  
58  
59      /*** @Expression in_scope && execution(* getFirst(..)) && args(s, ..) */
60  
61      void pc_getFirst(String s) {;}// here we use "return void" style
62  
63  
64  
65      /*** @Expression in_scope && execution(* changeArg(..)) && args(String, s, long) */
66  
67      Pointcut pc_changeArg(StringBuffer s) {return null;}// here we use "return null" style
68  
69  
70  
71      /*** @Expression in_scope && execution(* orderChangedInPointcutSignature(..)) && args(s0, s1, long) */
72  
73      Pointcut pc_orderChangedInPointcutSignature(String s1, String s0) {return null;}
74  
75  
76  
77      /*** @Expression in_scope && execution(* orderChangedInAdviceSignature(..)) && args(s0, s1, long) */
78  
79      Pointcut pc_orderChangedInAdviceSignature(String s0, String s1) {return null;}
80  
81  
82  
83      /*** @Expression in_scope && execution(* orderChangedInPointcutAndAdviceSignature(..)) && args(s0, s1, long) */
84  
85      Pointcut pc_orderChangedInPointcutAndAdviceSignature(String s1, String s0) {return null;}
86  
87  
88  
89  
90  
91      /*** @Before pc_matchAll || pc_matchAllWithWildcard */
92  
93      public void matchAllBefore(JoinPoint joinPoint) {
94  
95          ((Loggable)joinPoint.getTarget()).log("before ");
96  
97      }
98  
99      /*** @After pc_matchAll || pc_matchAllWithWildcard */
100 
101     public void matchAllAfter(JoinPoint joinPoint) {
102 
103         ((Loggable)joinPoint.getTarget()).log("after ");
104 
105     }
106 
107     /*** @Around pc_matchAll || pc_matchAllWithWildcard */
108 
109     public Object matchAllAround(JoinPoint joinPoint) throws Throwable {
110 
111         ((Loggable)joinPoint.getTarget()).log("before1 ");
112 
113         Object res = joinPoint.proceed();
114 
115         ((Loggable)joinPoint.getTarget()).log("after1 ");
116 
117         return res;
118 
119     }
120 
121 
122 
123 
124 
125 
126 
127     /*** @Before pc_getFirst(as) */
128 
129     public void getFirstBefore(JoinPoint joinPoint, String as) {
130 
131         ((Loggable)joinPoint.getTarget()).log("before " + as + " ");
132 
133     }
134 
135     /*** @After pc_getFirst(as) */
136 
137     public void getFirstAfter(String as, JoinPoint joinPoint) {//here we use some fancy order in the signature
138 
139         ((Loggable)joinPoint.getTarget()).log("after " + as + " ");
140 
141     }
142 
143     /*** @Around pc_getFirst(as) */
144 
145     public Object getFirstAround(JoinPoint joinPoint, String as) throws Throwable {
146 
147         ((Loggable)joinPoint.getTarget()).log("before1 " + as + " ");
148 
149         Object res = joinPoint.proceed();
150 
151         ((Loggable)joinPoint.getTarget()).log("after1 " + as + " ");
152 
153         return res;
154 
155     }
156 
157 
158 
159     /*** @Before in_scope && execution(* getFirstAnonymous(..)) && args(as,String,..) */
160 
161     public void getFirstAnonymousBefore(JoinPoint joinPoint, String as) {
162 
163         ((Loggable)joinPoint.getTarget()).log("before " + as + " ");
164 
165     }
166 
167     /*** @After in_scope && execution(* getFirstAnonymous(..)) && args(as, String, long) */
168 
169     public void getFirstAnonymousAfter(String as, JoinPoint joinPoint) {
170 
171         ((Loggable)joinPoint.getTarget()).log("after " + as + " ");
172 
173     }
174 
175     /*** @Around in_scope && execution(* getFirstAnonymous(..)) && args(as,..) */
176 
177     public Object getFirstAnonymousAround(JoinPoint joinPoint, String as) throws Throwable {
178 
179         ((Loggable)joinPoint.getTarget()).log("before1 " + as + " ");
180 
181         Object res = joinPoint.proceed();
182 
183         ((Loggable)joinPoint.getTarget()).log("after1 " + as + " ");
184 
185         return res;
186 
187     }
188 
189 
190 
191     /*** @Before pc_changeArg(as) */
192 
193     public void changeArgBefore(JoinPoint joinPoint, StringBuffer as) {
194 
195         as.append("x");
196 
197         ((Loggable)joinPoint.getTarget()).log("before " + as.toString() + " ");
198 
199     }
200 
201     /*** @After pc_changeArg(as) */
202 
203     public void changeArgAfter(JoinPoint joinPoint, StringBuffer as) {
204 
205         as.append("x");
206 
207         ((Loggable)joinPoint.getTarget()).log("after " + as.toString() + " ");
208 
209     }
210 
211     /*** @Around pc_changeArg(as) */
212 
213     public Object changeArgAround(StringBuffer as, JoinPoint joinPoint) throws Throwable {//here we use some fancy order in the signature
214 
215         as.append("x");
216 
217         ((Loggable)joinPoint.getTarget()).log("before1 " + as.toString() + " ");
218 
219         Object res = joinPoint.proceed();
220 
221         as.append("x");
222 
223         ((Loggable)joinPoint.getTarget()).log("after1 " + as.toString() + " ");
224 
225         return res;
226 
227     }
228 
229 
230 
231 
232 
233 
234 
235 
236 
237     /*** @Before pc_orderChangedInPointcutSignature(as0, as1) */
238 
239     public void orderChangedInPointcutSignatureBefore(JoinPoint joinPoint, String as0, String as1) {
240 
241         ((Loggable)joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
242 
243     }
244 
245     /*** @After pc_orderChangedInPointcutSignature(as0, as1) */
246 
247     public void orderChangedInPointcutSignatureAfter(JoinPoint joinPoint, String as0, String as1) {
248 
249         ((Loggable)joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
250 
251     }
252 
253     /*** @Around pc_orderChangedInPointcutSignature(as0, as1) */
254 
255     public Object orderChangedInPointcutSignatureAround(JoinPoint joinPoint, String as0, String as1) throws Throwable {
256 
257         ((Loggable)joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
258 
259         Object res = joinPoint.proceed();
260 
261         ((Loggable)joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
262 
263         return res;
264 
265     }
266 
267 
268 
269 
270 
271 
272 
273     /*** @Before pc_orderChangedInAdviceSignature(as1, as0) */
274 
275     public void orderChangedInAdviceSignatureBefore(JoinPoint joinPoint, String as0, String as1) {
276 
277         ((Loggable)joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
278 
279     }
280 
281     /*** @After pc_orderChangedInAdviceSignature(as1, as0) */
282 
283     public void orderChangedInAdviceSignatureAfter(JoinPoint joinPoint, String as0, String as1) {
284 
285         ((Loggable)joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
286 
287     }
288 
289     /*** @Around pc_orderChangedInAdviceSignature(as1, as0) */
290 
291     public Object orderChangedInAdviceSignatureAround(JoinPoint joinPoint, String as0, String as1) throws Throwable {
292 
293         ((Loggable)joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
294 
295         Object res = joinPoint.proceed();
296 
297         ((Loggable)joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
298 
299         return res;
300 
301     }
302 
303 
304 
305 
306 
307 
308 
309     /*** @Before pc_orderChangedInPointcutAndAdviceSignature(as1, as0) */
310 
311     public void orderChangedInPointcutAndAdviceSignatureBefore(JoinPoint joinPoint, String as0, String as1) {
312 
313         ((Loggable)joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
314 
315     }
316 
317     /*** @After pc_orderChangedInPointcutAndAdviceSignature(as1, as0) */
318 
319     public void orderChangedInPointcutAndAdviceSignatureAfter(JoinPoint joinPoint, String as0, String as1) {
320 
321         ((Loggable)joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
322 
323     }
324 
325     /*** @Around pc_orderChangedInPointcutAndAdviceSignature(as1, as0) */
326 
327     public Object orderChangedInPointcutAndAdviceSignatureAround(JoinPoint joinPoint, String as0, String as1) throws Throwable {
328 
329         ((Loggable)joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
330 
331         Object res = joinPoint.proceed();
332 
333         ((Loggable)joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
334 
335         return res;
336 
337     }
338 
339 
340 
341     //-- Method call pointcuts with args
342 
343 
344 
345     /*** @Expression in_scope && call(* callGetFirstAndSecond(..)) && args(l, s) */
346 
347     void pc_callGetFirstAndSecond(long l, String[] s) {};
348 
349 
350 
351     /*** @Before pc_callGetFirstAndSecond(l, s) */
352 
353     public void callGetFirstAndSecondBefore(JoinPoint joinPoint, long l, String[] s) {
354 
355         ((Loggable)joinPoint.getTarget()).log("before " + l + " " + s[0] + "," + s[1] + " ");
356 
357     }
358 
359     /*** @After pc_callGetFirstAndSecond(l, s) */
360 
361     public void callGetFirstAndSecondAfter(JoinPoint joinPoint, long l, String[] s) {
362 
363         ((Loggable)joinPoint.getTarget()).log("after " + l + " " + s[0] + "," + s[1] + " ");
364 
365     }
366 
367     /*** @Around pc_callGetFirstAndSecond(l, s) */
368 
369     public Object callGetFirstAndSecondAround(JoinPoint joinPoint, long l, String[] s) throws Throwable {
370 
371         ((Loggable)joinPoint.getTarget()).log("before1 " + l + " " + s[0] + "," + s[1] + " ");
372 
373         Object res = joinPoint.proceed();
374 
375         ((Loggable)joinPoint.getTarget()).log("after1 " + l + " " + s[0] + "," + s[1] + " ");
376 
377         return res;
378 
379     }
380 
381 
382 
383     //-- Ctor execution pointcuts with args
384 
385     // we are using inner class, so args() is a bit tricky
386 
387 
388 
389     /*** @Expression execution(test.args.ArgsAdviceTest$CtorExecution.new(..)) && args(test.args.ArgsAdviceTest, s) */
390 
391     void pc_ctorExecutionGetFirst(String s) {};
392 
393 
394 
395     /*** @Before pc_ctorExecutionGetFirst(s) */
396 
397     public void ctorExecutionGetFirstBefore(JoinPoint joinPoint, String s) {
398 
399         ((Loggable)joinPoint.getTarget()).log("before " + s+ " ");
400 
401     }
402 
403     /*** @After pc_ctorExecutionGetFirst(s) */
404 
405     public void ctorExecutionGetFirstAfter(JoinPoint joinPoint, String s) {
406 
407         ((Loggable)joinPoint.getTarget()).log("after " + s + " ");
408 
409     }
410 
411     /*** @Around pc_ctorExecutionGetFirst(s) */
412 
413     public Object ctorExecutionGetFirstAround(JoinPoint joinPoint, String s) throws Throwable {
414 
415         ((Loggable)joinPoint.getTarget()).log("before1 " + s + " ");
416 
417         Object res = joinPoint.proceed();
418 
419         ((Loggable)joinPoint.getTarget()).log("after1 " + s + " ");
420 
421         return res;
422 
423     }
424 
425 
426 
427     //-- Ctor call pointcuts with args
428 
429     // we are using inner class, so args() is a bit tricky
430 
431 
432 
433     /*** @Expression in_scope && call(test.args.ArgsAdviceTest$CtorCall.new(..)) && args(test.args.ArgsAdviceTest, s) */
434 
435     void pc_ctorCallGetFirst(String s) {};
436 
437 
438 
439     /*** @Before pc_ctorCallGetFirst(s) */
440 
441     public void ctorCallGetFirstBefore(JoinPoint joinPoint, String s) {
442 
443         ArgsAdviceTest.logStatic("before " + s+ " ");
444 
445     }
446 
447     /*** @After pc_ctorCallGetFirst(s) */
448 
449     public void ctorCallGetFirstAfter(JoinPoint joinPoint, String s) {
450 
451         ArgsAdviceTest.logStatic("after " + s + " ");
452 
453     }
454 
455     /*** @Around pc_ctorCallGetFirst(s) */
456 
457     public Object ctorCallGetFirstAround(JoinPoint joinPoint, String s) throws Throwable {
458 
459         ArgsAdviceTest.logStatic("before1 " + s + " ");
460 
461         Object res = joinPoint.proceed();
462 
463         ArgsAdviceTest.logStatic("after1 " + s + " ");
464 
465         return res;
466 
467     }
468 
469 
470 
471     //-- field set with args()
472 
473     /*** @Expression in_scope && set(* m_field) && args(s) */
474 
475     void pc_mfield(String s) {};
476 
477 
478 
479     /*** @Before pc_mfield(s) */
480 
481     public void mfieldBefore(JoinPoint joinPoint, String s) {
482 
483         String fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
484 
485         ((Loggable)joinPoint.getTarget()).log("before " + fieldValue + "," + s + " ");
486 
487     }
488 
489     /*** @After pc_mfield(s) */
490 
491     public void mfieldAfter(JoinPoint joinPoint, String s) {
492 
493         String fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
494 
495         ((Loggable)joinPoint.getTarget()).log("after " + fieldValue + "," + s + " ");
496 
497     }
498 
499     /*** @Around pc_mfield(s) */
500 
501     public Object mfieldAround(JoinPoint joinPoint, String s) throws Throwable {
502 
503         String fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
504 
505         ((Loggable)joinPoint.getTarget()).log("before1 " + fieldValue + "," + s + " ");
506 
507         s = "changed"; // will be ignored due to delegation ! [AJ]
508 
509         Object res = joinPoint.proceed();
510 
511         fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
512 
513         ((Loggable)joinPoint.getTarget()).log("after1 " + fieldValue + "," + s + " ");
514 
515         return "ignored";
516 
517     }
518 
519 
520 
521     //-- static field set with args()
522 
523     /*** @Expression in_scope && set(* s_field) && args(s) */
524 
525     void pc_sfield(String s) {};
526 
527 
528 
529     /*** @Before pc_sfield(s) */
530 
531     public void sfieldBefore(JoinPoint joinPoint, String s) {
532 
533         String fieldValue = ArgsAdviceTest.getStaticField();
534 
535         ArgsAdviceTest.logStatic("before " + fieldValue + "," + s + " ");
536 
537     }
538 
539     /*** @After pc_sfield(s) */
540 
541     public void sfieldAfter(JoinPoint joinPoint, String s) {
542 
543         String fieldValue = ArgsAdviceTest.getStaticField();
544 
545         ArgsAdviceTest.logStatic("after " + fieldValue + "," + s + " ");
546 
547     }
548 
549     /*** @Around pc_sfield(s) */
550 
551     public Object sfieldAround(JoinPoint joinPoint, String s) throws Throwable {
552 
553         String fieldValue = ArgsAdviceTest.getStaticField();
554 
555         ArgsAdviceTest.logStatic("before1 " + fieldValue + "," + s + " ");
556 
557         s = "changed"; // will be ignored due to delegation ! [AJ]
558 
559         Object res = joinPoint.proceed();
560 
561         fieldValue = ArgsAdviceTest.getStaticField();
562 
563         ArgsAdviceTest.logStatic("after1 " + fieldValue + "," + s + " ");
564 
565         return "ignored";
566 
567     }
568 
569 
570 
571 }
572