001: /**************************************************************************************
002: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.args;
008:
009: import org.codehaus.aspectwerkz.definition.Pointcut;
010: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
011: import test.Loggable;
012:
013: /**
014: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
015: */
016: public class ArgsAspect {
017:
018: //-- Method execution pointcuts with args
019:
020: /**
021: * @Expression within(test.args.ArgsAdviceTest)
022: */
023: Pointcut in_scope;
024:
025: /**
026: * @Expression in_scope && execution(* matchAll(..)) && args(String, String, long)
027: */
028: Pointcut pc_matchAll;
029:
030: /**
031: * @Expression in_scope && execution(* matchAllWithWildcard(..)) && args(..)
032: */
033: Pointcut pc_matchAllWithWildcard;
034:
035: /**
036: * @Expression in_scope && execution(* getFirst(..)) && args(s, ..)
037: */
038: void pc_getFirst(String s) {
039: ;
040: }// here we use "return void" style
041:
042: /**
043: * @Expression in_scope && execution(* changeArg(..)) && args(String, s, long)
044: */
045: Pointcut pc_changeArg(StringBuffer s) {
046: return null;
047: }// here we use "return null" style
048:
049: /**
050: * @Expression in_scope && execution(* orderChangedInPointcutSignature(..)) && args(s0, s1, long)
051: */
052: Pointcut pc_orderChangedInPointcutSignature(String s1, String s0) {
053: return null;
054: }
055:
056: /**
057: * @Expression in_scope && execution(* orderChangedInAdviceSignature(..)) && args(s0, s1, long)
058: */
059: Pointcut pc_orderChangedInAdviceSignature(String s0, String s1) {
060: return null;
061: }
062:
063: /**
064: * @Expression in_scope && execution(* orderChangedInPointcutAndAdviceSignature(..)) && args(s0, s1, long)
065: */
066: Pointcut pc_orderChangedInPointcutAndAdviceSignature(String s1,
067: String s0) {
068: return null;
069: }
070:
071: /**
072: * @Before in_scope && execution(* singleAndDotDot(..)) && args(i)
073: */
074: public void singleAndDotDot(JoinPoint joinPoint, int i) {
075: ((Loggable) joinPoint.getTarget()).log("before " + i + " ");
076: }
077:
078: /**
079: * @Before in_scope && execution(* withArray(..)) && args(l, s, matrix)
080: */
081: public void withArray(JoinPoint joinPoint, long l, String s,
082: int[][] matrix) {
083: String iis = "";
084: for (int i = 0; i < matrix.length; i++) {
085: for (int j = 0; j < matrix[i].length; j++) {
086: iis += "" + matrix[i][j] + "-";
087: }
088: }
089: ((Loggable) joinPoint.getTarget()).log("before " + l + " " + s
090: + " " + iis + " ");
091: }
092:
093: /**
094: * @Before pc_matchAll || pc_matchAllWithWildcard
095: */
096: public void matchAllBefore(JoinPoint joinPoint) {
097: ((Loggable) joinPoint.getTarget()).log("before ");
098: }
099:
100: /**
101: * @After pc_matchAll || pc_matchAllWithWildcard
102: */
103: public void matchAllAfter(JoinPoint joinPoint) {
104: ((Loggable) joinPoint.getTarget()).log("after ");
105: }
106:
107: /**
108: * @Around pc_matchAll || pc_matchAllWithWildcard
109: */
110: public Object matchAllAround(JoinPoint joinPoint) throws Throwable {
111: ((Loggable) joinPoint.getTarget()).log("before1 ");
112: Object res = joinPoint.proceed();
113: ((Loggable) joinPoint.getTarget()).log("after1 ");
114: return res;
115: }
116:
117: /**
118: * @Before pc_getFirst(as)
119: */
120: public void getFirstBefore(JoinPoint joinPoint, String as) {
121: ((Loggable) joinPoint.getTarget()).log("before " + as + " ");
122: }
123:
124: /**
125: * @After pc_getFirst(as)
126: */
127: public void getFirstAfter(String as, JoinPoint joinPoint) {//here we use some fancy order in the signature
128: ((Loggable) joinPoint.getTarget()).log("after " + as + " ");
129: }
130:
131: /**
132: * @Around pc_getFirst(as)
133: */
134: public Object getFirstAround(JoinPoint joinPoint, String as)
135: throws Throwable {
136: ((Loggable) joinPoint.getTarget()).log("before1 " + as + " ");
137: Object res = joinPoint.proceed();
138: ((Loggable) joinPoint.getTarget()).log("after1 " + as + " ");
139: return res;
140: }
141:
142: /**
143: * @Before in_scope && execution(* getFirstAnonymous(..)) && args(as,String,..)
144: */
145: public void getFirstAnonymousBefore(JoinPoint joinPoint, String as) {
146: ((Loggable) joinPoint.getTarget()).log("before " + as + " ");
147: }
148:
149: /**
150: * @After in_scope && execution(* getFirstAnonymous(..)) && args(as, String, long)
151: */
152: public void getFirstAnonymousAfter(String as, JoinPoint joinPoint) {
153: ((Loggable) joinPoint.getTarget()).log("after " + as + " ");
154: }
155:
156: /**
157: * @Around in_scope && execution(* getFirstAnonymous(..)) && args(as,..)
158: */
159: public Object getFirstAnonymousAround(JoinPoint joinPoint, String as)
160: throws Throwable {
161: ((Loggable) joinPoint.getTarget()).log("before1 " + as + " ");
162: Object res = joinPoint.proceed();
163: ((Loggable) joinPoint.getTarget()).log("after1 " + as + " ");
164: return res;
165: }
166:
167: /**
168: * @Before pc_changeArg(as)
169: */
170: public void changeArgBefore(JoinPoint joinPoint, StringBuffer as) {
171: as.append("x");
172: ((Loggable) joinPoint.getTarget()).log("before "
173: + as.toString() + " ");
174: }
175:
176: /**
177: * @After pc_changeArg(as)
178: */
179: public void changeArgAfter(JoinPoint joinPoint, StringBuffer as) {
180: as.append("x");
181: ((Loggable) joinPoint.getTarget()).log("after " + as.toString()
182: + " ");
183: }
184:
185: /**
186: * @Around pc_changeArg(as)
187: */
188: public Object changeArgAround(StringBuffer as, JoinPoint joinPoint)
189: throws Throwable {//here we use some fancy order in the signature
190: as.append("x");
191: ((Loggable) joinPoint.getTarget()).log("before1 "
192: + as.toString() + " ");
193: Object res = joinPoint.proceed();
194: as.append("x");
195: ((Loggable) joinPoint.getTarget()).log("after1 "
196: + as.toString() + " ");
197: return res;
198: }
199:
200: /**
201: * @Before pc_orderChangedInPointcutSignature(as0, as1)
202: */
203: public void orderChangedInPointcutSignatureBefore(
204: JoinPoint joinPoint, String as0, String as1) {
205: ((Loggable) joinPoint.getTarget()).log("before " + as0 + " "
206: + as1 + " ");
207: }
208:
209: /**
210: * @After pc_orderChangedInPointcutSignature(as0, as1)
211: */
212: public void orderChangedInPointcutSignatureAfter(
213: JoinPoint joinPoint, String as0, String as1) {
214: ((Loggable) joinPoint.getTarget()).log("after " + as0 + " "
215: + as1 + " ");
216: }
217:
218: /**
219: * @Around pc_orderChangedInPointcutSignature(as0, as1)
220: */
221: public Object orderChangedInPointcutSignatureAround(
222: JoinPoint joinPoint, String as0, String as1)
223: throws Throwable {
224: ((Loggable) joinPoint.getTarget()).log("before1 " + as0 + " "
225: + as1 + " ");
226: Object res = joinPoint.proceed();
227: ((Loggable) joinPoint.getTarget()).log("after1 " + as0 + " "
228: + as1 + " ");
229: return res;
230: }
231:
232: /**
233: * @Before pc_orderChangedInAdviceSignature(as1, as0)
234: */
235: public void orderChangedInAdviceSignatureBefore(
236: JoinPoint joinPoint, String as0, String as1) {
237: ((Loggable) joinPoint.getTarget()).log("before " + as0 + " "
238: + as1 + " ");
239: }
240:
241: /**
242: * @After pc_orderChangedInAdviceSignature(as1, as0)
243: */
244: public void orderChangedInAdviceSignatureAfter(JoinPoint joinPoint,
245: String as0, String as1) {
246: ((Loggable) joinPoint.getTarget()).log("after " + as0 + " "
247: + as1 + " ");
248: }
249:
250: /**
251: * @Around pc_orderChangedInAdviceSignature(as1, as0)
252: */
253: public Object orderChangedInAdviceSignatureAround(
254: JoinPoint joinPoint, String as0, String as1)
255: throws Throwable {
256: ((Loggable) joinPoint.getTarget()).log("before1 " + as0 + " "
257: + as1 + " ");
258: Object res = joinPoint.proceed();
259: ((Loggable) joinPoint.getTarget()).log("after1 " + as0 + " "
260: + as1 + " ");
261: return res;
262: }
263:
264: /**
265: * @Before pc_orderChangedInPointcutAndAdviceSignature(as1, as0)
266: */
267: public void orderChangedInPointcutAndAdviceSignatureBefore(
268: JoinPoint joinPoint, String as0, String as1) {
269: ((Loggable) joinPoint.getTarget()).log("before " + as0 + " "
270: + as1 + " ");
271: }
272:
273: /**
274: * @After pc_orderChangedInPointcutAndAdviceSignature(as1, as0)
275: */
276: public void orderChangedInPointcutAndAdviceSignatureAfter(
277: JoinPoint joinPoint, String as0, String as1) {
278: ((Loggable) joinPoint.getTarget()).log("after " + as0 + " "
279: + as1 + " ");
280: }
281:
282: /**
283: * @Around pc_orderChangedInPointcutAndAdviceSignature(as1, as0)
284: */
285: public Object orderChangedInPointcutAndAdviceSignatureAround(
286: JoinPoint joinPoint, String as0, String as1)
287: throws Throwable {
288: ((Loggable) joinPoint.getTarget()).log("before1 " + as0 + " "
289: + as1 + " ");
290: Object res = joinPoint.proceed();
291: ((Loggable) joinPoint.getTarget()).log("after1 " + as0 + " "
292: + as1 + " ");
293: return res;
294: }
295:
296: //-- Method call pointcuts with args
297:
298: /**
299: * @Expression in_scope && call(* callGetFirstAndSecond(..)) && args(l, s)
300: */
301: void pc_callGetFirstAndSecond(long l, String[] s) {
302: };
303:
304: /**
305: * @Before pc_callGetFirstAndSecond(l, s)
306: */
307: public void callGetFirstAndSecondBefore(JoinPoint joinPoint,
308: long l, String[] s) {
309: ((Loggable) joinPoint.getTarget()).log("before " + l + " "
310: + s[0] + "," + s[1] + " ");
311: }
312:
313: /**
314: * @After pc_callGetFirstAndSecond(l, s)
315: */
316: public void callGetFirstAndSecondAfter(JoinPoint joinPoint, long l,
317: String[] s) {
318: ((Loggable) joinPoint.getTarget()).log("after " + l + " "
319: + s[0] + "," + s[1] + " ");
320: }
321:
322: /**
323: * @Around pc_callGetFirstAndSecond(l, s)
324: */
325: public Object callGetFirstAndSecondAround(JoinPoint joinPoint,
326: long l, String[] s) throws Throwable {
327: ((Loggable) joinPoint.getTarget()).log("before1 " + l + " "
328: + s[0] + "," + s[1] + " ");
329: Object res = joinPoint.proceed();
330: ((Loggable) joinPoint.getTarget()).log("after1 " + l + " "
331: + s[0] + "," + s[1] + " ");
332: return res;
333: }
334:
335: //-- Ctor execution pointcuts with args
336: // we are using inner class, so args() is a bit tricky
337:
338: /**
339: * @Expression execution(test.args.ArgsAdviceTest$CtorExecution.new(..)) && args(test.args.ArgsAdviceTest, s)
340: */
341: void pc_ctorExecutionGetFirst(String s) {
342: };
343:
344: /**
345: * @Before pc_ctorExecutionGetFirst(s)
346: */
347: public void ctorExecutionGetFirstBefore(JoinPoint joinPoint,
348: String s) {
349: ((Loggable) joinPoint.getTarget()).log("before " + s + " ");
350: }
351:
352: /**
353: * @After pc_ctorExecutionGetFirst(s)
354: */
355: public void ctorExecutionGetFirstAfter(JoinPoint joinPoint, String s) {
356: ((Loggable) joinPoint.getTarget()).log("after " + s + " ");
357: }
358:
359: /**
360: * @Around pc_ctorExecutionGetFirst(s)
361: */
362: public Object ctorExecutionGetFirstAround(JoinPoint joinPoint,
363: String s) throws Throwable {
364: ((Loggable) joinPoint.getTarget()).log("before1 " + s + " ");
365: Object res = joinPoint.proceed();
366: ((Loggable) joinPoint.getTarget()).log("after1 " + s + " ");
367: return res;
368: }
369:
370: //-- Ctor call pointcuts with args
371: // we are using inner class, so args() is a bit tricky
372:
373: /**
374: * @Expression in_scope && call(test.args.ArgsAdviceTest$CtorCall.new(..)) && args(test.args.ArgsAdviceTest, s)
375: */
376: void pc_ctorCallGetFirst(String s) {
377: };
378:
379: /**
380: * @Before pc_ctorCallGetFirst(s)
381: */
382: public void ctorCallGetFirstBefore(JoinPoint joinPoint, String s) {
383: ArgsAdviceTest.logStatic("before " + s + " ");
384: }
385:
386: /**
387: * @After pc_ctorCallGetFirst(s)
388: */
389: public void ctorCallGetFirstAfter(JoinPoint joinPoint, String s) {
390: ArgsAdviceTest.logStatic("after " + s + " ");
391: }
392:
393: /**
394: * @Around pc_ctorCallGetFirst(s)
395: */
396: public Object ctorCallGetFirstAround(JoinPoint joinPoint, String s)
397: throws Throwable {
398: ArgsAdviceTest.logStatic("before1 " + s + " ");
399: Object res = joinPoint.proceed();
400: ArgsAdviceTest.logStatic("after1 " + s + " ");
401: return res;
402: }
403:
404: //-- field set with args()
405: /**
406: * @Expression in_scope && set(* m_field) && args(s)
407: */
408: void pc_mfield(String s) {
409: };
410:
411: /**
412: * @Before pc_mfield(s)
413: */
414: public void mfieldBefore(JoinPoint joinPoint, String s) {
415: String fieldValue = ((ArgsAdviceTest) joinPoint.getTarget())
416: .getField();
417: ((Loggable) joinPoint.getTarget()).log("before " + fieldValue
418: + "," + s + " ");
419: }
420:
421: /**
422: * @After pc_mfield(s)
423: */
424: public void mfieldAfter(JoinPoint joinPoint, String s) {
425: String fieldValue = ((ArgsAdviceTest) joinPoint.getTarget())
426: .getField();
427: ((Loggable) joinPoint.getTarget()).log("after " + fieldValue
428: + "," + s + " ");
429: }
430:
431: /**
432: * @Around pc_mfield(s)
433: */
434: public Object mfieldAround(JoinPoint joinPoint, String s)
435: throws Throwable {
436: String fieldValue = ((ArgsAdviceTest) joinPoint.getTarget())
437: .getField();
438: ((Loggable) joinPoint.getTarget()).log("before1 " + fieldValue
439: + "," + s + " ");
440: s = "changed"; // will be ignored due to delegation ! [AJ]
441: Object res = joinPoint.proceed();
442: fieldValue = ((ArgsAdviceTest) joinPoint.getTarget())
443: .getField();
444: ((Loggable) joinPoint.getTarget()).log("after1 " + fieldValue
445: + "," + s + " ");
446: return "ignored";
447: }
448:
449: //-- static field set with args()
450: /**
451: * @Expression in_scope && set(* s_field) && args(s)
452: */
453: void pc_sfield(String s) {
454: };
455:
456: /**
457: * @Before pc_sfield(s)
458: */
459: public void sfieldBefore(JoinPoint joinPoint, String s) {
460: String fieldValue = ArgsAdviceTest.getStaticField();
461: ArgsAdviceTest
462: .logStatic("before " + fieldValue + "," + s + " ");
463: }
464:
465: /**
466: * @After pc_sfield(s)
467: */
468: public void sfieldAfter(JoinPoint joinPoint, String s) {
469: String fieldValue = ArgsAdviceTest.getStaticField();
470: ArgsAdviceTest.logStatic("after " + fieldValue + "," + s + " ");
471: }
472:
473: /**
474: * @Around pc_sfield(s)
475: */
476: public Object sfieldAround(JoinPoint joinPoint, String s)
477: throws Throwable {
478: String fieldValue = ArgsAdviceTest.getStaticField();
479: ArgsAdviceTest.logStatic("before1 " + fieldValue + "," + s
480: + " ");
481: s = "changed"; // will be ignored due to delegation ! [AJ]
482: Object res = joinPoint.proceed();
483: fieldValue = ArgsAdviceTest.getStaticField();
484: ArgsAdviceTest
485: .logStatic("after1 " + fieldValue + "," + s + " ");
486: return "ignored";
487: }
488:
489: }
|