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.thistarget;
008:
009: import org.codehaus.aspectwerkz.definition.Pointcut;
010: import org.codehaus.aspectwerkz.definition.Pointcut;
011: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
012: import junit.framework.TestCase;
013:
014: /**
015: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
016: */
017: public class ThisTargetAspect {
018:
019: //------------------------- Method execution
020:
021: /**
022: * @Expression execution(* test.thistarget.*.target())
023: */
024: Pointcut exe_target;
025:
026: /**
027: * @Expression execution(* test.thistarget.*.targetAbstract())
028: */
029: Pointcut exe_targetAbstract;
030:
031: // interface
032:
033: /**
034: * @Before exe_target && target(t)
035: */
036: public void beforeITarget(ITarget t) {
037: validate(t, ITarget.class);
038: TargetTest.log("before_ITarget");
039: }
040:
041: /**
042: * @Around exe_target && target(t)
043: */
044: public Object aroundITarget(JoinPoint jp, ITarget t)
045: throws Throwable {
046: validate(t, ITarget.class);
047: TargetTest.log("pre_ITarget");
048: Object o = jp.proceed();
049: TargetTest.log("post_ITarget");
050: return o;
051: }
052:
053: /**
054: * @After exe_target && target(t)
055: */
056: public void afterITarget(ITarget t) {
057: validate(t, ITarget.class);
058: TargetTest.log("after_ITarget");
059: }
060:
061: // interface implementation
062:
063: /**
064: * @Before exe_target && target(t) && this(callee)
065: */
066: public void beforeTargetIWithThis(TargetI t, Object callee) {
067: validate(t, TargetI.class);
068: validate(callee, TargetI.class);
069: TargetTest.log("before_TargetI");
070: }
071:
072: /**
073: * @Around exe_target && target(t)
074: */
075: public Object aroundTargetI(JoinPoint jp, TargetI t)
076: throws Throwable {
077: validate(t, TargetI.class);
078: TargetTest.log("pre_TargetI");
079: Object o = jp.proceed();
080: TargetTest.log("post_TargetI");
081: return o;
082: }
083:
084: /**
085: * @After exe_target && target(t)
086: */
087: public void afterTargetI(TargetI t) {
088: validate(t, TargetI.class);
089: TargetTest.log("after_TargetI");
090: }
091:
092: // super class
093:
094: /**
095: * @Before exe_target && target(t)
096: */
097: public void beforeSuperTarget(SuperTarget t) {
098: validate(t, SuperTarget.class);
099: TargetTest.log("before_SuperTarget");
100: }
101:
102: /**
103: * @Around exe_target && target(t)
104: */
105: public Object aroundSuperTarget(JoinPoint jp, SuperTarget t)
106: throws Throwable {
107: validate(t, SuperTarget.class);
108: TargetTest.log("pre_SuperTarget");
109: Object o = jp.proceed();
110: TargetTest.log("post_SuperTarget");
111: return o;
112: }
113:
114: /**
115: * @After exe_target && target(t)
116: */
117: public void afterSuperTarget(SuperTarget t) {
118: validate(t, SuperTarget.class);
119: TargetTest.log("after_SuperTarget");
120: }
121:
122: // super class abstract method
123:
124: /**
125: * @Before exe_targetAbstract && target(t)
126: */
127: public void beforeSuperTargetA(SuperTarget t) {
128: validate(t, SuperTarget.class);
129: TargetTest.log("before_SuperTargetA");
130: }
131:
132: /**
133: * @Around exe_targetAbstract && target(t)
134: */
135: public Object aroundSuperTargetA(JoinPoint jp, SuperTarget t)
136: throws Throwable {
137: validate(t, SuperTarget.class);
138: TargetTest.log("pre_SuperTargetA");
139: Object o = jp.proceed();
140: TargetTest.log("post_SuperTargetA");
141: return o;
142: }
143:
144: /**
145: * @After exe_targetAbstract && target(t)
146: */
147: public void afterSuperTargetA(SuperTarget t) {
148: validate(t, SuperTarget.class);
149: TargetTest.log("after_SuperTargetA");
150: }
151:
152: //------------------------- Ctor call
153:
154: /**
155: * @Expression this(caller) && call(test.thistarget.*.new()) && withincode(* test.*.*.testConstructorCallTargetThis(..))
156: */
157: Pointcut cctor_this (TargetTest caller) {
158: return null;
159: }
160:
161: // interface
162:
163: /**
164: * @Before cctor_this(caller) && target(t)
165: */
166: public void beforeITarget(ITarget t, Object caller) {
167: validate(t, null);
168: validate(caller, TargetTest.class);
169: TargetTest.log("before_ITarget");
170: }
171:
172: /**
173: * @Around cctor_this(caller) && target(t)
174: */
175: public Object aroundITarget(JoinPoint jp, ITarget t, Object caller)
176: throws Throwable {
177: validate(t, null);
178: validate(caller, TargetTest.class);
179: TargetTest.log("pre_ITarget");
180: Object o = jp.proceed();
181: validate(o, ITarget.class);
182: validate(t, null);// in an around advice, target is a local variable so even if jp has set callee, the local
183: // instance is not.
184: TargetTest.log("post_ITarget");
185: return o;
186: }
187:
188: /**
189: * @After cctor_this(caller) && target(t)
190: */
191: public void afterITarget(ITarget t, Object caller) {
192: validate(t, ITarget.class);
193: validate(caller, TargetTest.class);
194: TargetTest.log("after_ITarget");
195: }
196:
197: // interface implementation
198:
199: /**
200: * @Before cctor_this(caller) && target(t)
201: */
202: public void beforeTargetI(TargetI t, Object caller) {
203: validate(t, null);
204: validate(caller, TargetTest.class);
205: TargetTest.log("before_TargetI");
206: }
207:
208: /**
209: * @Around cctor_this(caller) && target(t)
210: */
211: public Object aroundTargetI(JoinPoint jp, TargetI t, Object caller)
212: throws Throwable {
213: validate(t, null);
214: validate(caller, TargetTest.class);
215: TargetTest.log("pre_TargetI");
216: Object o = jp.proceed();
217: validate(o, TargetI.class);
218: validate(t, null);// still null
219: TargetTest.log("post_TargetI");
220: return o;
221: }
222:
223: /**
224: * @After cctor_this(caller) && target(t)
225: */
226: public void afterTargetI(TargetI t, Object caller) {
227: validate(t, TargetI.class);
228: validate(caller, TargetTest.class);
229: TargetTest.log("after_TargetI");
230: }
231:
232: // super class
233:
234: /**
235: * @Before cctor_this(caller) && target(t)
236: */
237: public void beforeSuperTarget(SuperTarget t, Object caller) {
238: validate(t, null);
239: validate(caller, TargetTest.class);
240: TargetTest.log("before_SuperTarget");
241: }
242:
243: /**
244: * @Around cctor_this(caller) && target(t)
245: */
246: public Object aroundSuperTarget(JoinPoint jp, SuperTarget t,
247: Object caller) throws Throwable {
248: validate(t, null);
249: validate(caller, TargetTest.class);
250: TargetTest.log("pre_SuperTarget");
251: Object o = jp.proceed();
252: validate(o, SuperTarget.class);
253: validate(t, null);//still null - local variable
254: TargetTest.log("post_SuperTarget");
255: return o;
256: }
257:
258: /**
259: * @After cctor_this(caller) && target(t)
260: */
261: public void afterSuperTarget(SuperTarget t, Object caller) {
262: validate(t, SuperTarget.class);
263: validate(caller, TargetTest.class);
264: TargetTest.log("after_SuperTarget");
265: }
266:
267: //------------------------- Method call
268:
269: /**
270: * @Expression this(caller) && call(* test.thistarget.*.call()) && withincode(* test.*.*.testMethodCallTargetThis(..))
271: */
272: Pointcut call_this (TargetTest caller) {
273: return null;
274: }
275:
276: /**
277: * @Expression this(caller) && call(* test.thistarget.*.callAbstract()) && withincode(* test.*.*.testMethodCallTargetThis(..))
278: */
279: Pointcut callAbstract_this (TargetTest caller) {
280: return null;
281: }
282:
283: // interface
284:
285: /**
286: * @Before call_this(caller) && target(t)
287: */
288: public void beforeICall(ITarget t, Object caller) {
289: validate(t, ITarget.class);
290: validate(caller, TargetTest.class);
291: TargetTest.log("before_ITarget");
292: }
293:
294: /**
295: * @Around call_this(caller) && target(t)
296: */
297: public Object aroundICall(JoinPoint jp, ITarget t, Object caller)
298: throws Throwable {
299: validate(t, ITarget.class);
300: validate(caller, TargetTest.class);
301: TargetTest.log("pre_ITarget");
302: Object o = jp.proceed();
303: validate(t, ITarget.class);
304: // instance is not.
305: TargetTest.log("post_ITarget");
306: return o;
307: }
308:
309: /**
310: * @After call_this(caller) && target(t)
311: */
312: public void afterICall(ITarget t, Object caller) {
313: validate(t, ITarget.class);
314: validate(caller, TargetTest.class);
315: TargetTest.log("after_ITarget");
316: }
317:
318: // interface implementation
319:
320: /**
321: * @Before call_this(caller) && target(t)
322: */
323: public void beforeCallI(TargetI t, Object caller) {
324: validate(t, TargetI.class);
325: validate(caller, TargetTest.class);
326: TargetTest.log("before_TargetI");
327: }
328:
329: /**
330: * @Around call_this(caller) && target(t)
331: */
332: public Object aroundCallI(JoinPoint jp, TargetI t, Object caller)
333: throws Throwable {
334: validate(t, TargetI.class);
335: validate(caller, TargetTest.class);
336: TargetTest.log("pre_TargetI");
337: Object o = jp.proceed();
338: validate(t, TargetI.class);
339: TargetTest.log("post_TargetI");
340: return o;
341: }
342:
343: /**
344: * @After call_this(caller) && target(t)
345: */
346: public void afterCallI(TargetI t, Object caller) {
347: validate(t, TargetI.class);
348: validate(caller, TargetTest.class);
349: TargetTest.log("after_TargetI");
350: }
351:
352: // super class
353:
354: /**
355: * @Before call_this(caller) && target(t)
356: */
357: public void beforeSuperCall(SuperTarget t, Object caller) {
358: validate(t, SuperTarget.class);
359: validate(caller, TargetTest.class);
360: TargetTest.log("before_SuperTarget");
361: }
362:
363: /**
364: * @Around call_this(caller) && target(t)
365: */
366: public Object aroundSuperCall(JoinPoint jp, SuperTarget t,
367: Object caller) throws Throwable {
368: validate(t, SuperTarget.class);
369: validate(caller, TargetTest.class);
370: TargetTest.log("pre_SuperTarget");
371: Object o = jp.proceed();
372: validate(t, SuperTarget.class);
373: TargetTest.log("post_SuperTarget");
374: return o;
375: }
376:
377: /**
378: * @After call_this(caller) && target(t)
379: */
380: public void afterSuperCall(SuperTarget t, Object caller) {
381: validate(t, SuperTarget.class);
382: validate(caller, TargetTest.class);
383: TargetTest.log("after_SuperTarget");
384: }
385:
386: // super class - abstract method
387:
388: /**
389: * @Before callAbstract_this(caller) && target(t)
390: */
391: public void beforeSuperCallA(SuperTarget t, Object caller) {
392: validate(t, SuperTarget.class);
393: validate(caller, TargetTest.class);
394: TargetTest.log("before_SuperTargetA");
395: }
396:
397: /**
398: * @Around callAbstract_this(caller) && target(t)
399: */
400: public Object aroundSuperCallA(JoinPoint jp, SuperTarget t,
401: Object caller) throws Throwable {
402: validate(t, SuperTarget.class);
403: validate(caller, TargetTest.class);
404: TargetTest.log("pre_SuperTargetA");
405: Object o = jp.proceed();
406: validate(t, SuperTarget.class);
407: TargetTest.log("post_SuperTargetA");
408: return o;
409: }
410:
411: /**
412: * @After callAbstract_this(caller) && target(t)
413: */
414: public void afterSuperCallA(SuperTarget t, Object caller) {
415: validate(t, SuperTarget.class);
416: validate(caller, TargetTest.class);
417: TargetTest.log("after_SuperTargetA");
418: }
419:
420: //------------------------- Ctor exe
421:
422: /**
423: * @Expression this(self) && execution(test.thistarget.*.new())
424: */
425: Pointcut ector_this (Object self) {
426: return null;
427: }
428:
429: // interface
430:
431: /**
432: * @Before ector_this(caller) && target(t)
433: */
434: public void ector_beforeITarget(ITarget t, Object caller) {
435: validate(t, ITarget.class);
436: validate(caller, ITarget.class);
437: TargetTest.logCtorExe("before_ITarget");
438: }
439:
440: /**
441: * @Around ector_this(caller) && target(t)
442: */
443: public Object ector_aroundITarget(JoinPoint jp, ITarget t,
444: Object caller) throws Throwable {
445: validate(t, ITarget.class);
446: validate(caller, ITarget.class);
447: TargetTest.logCtorExe("pre_ITarget");
448: Object o = jp.proceed();
449: //validate(o, ITarget.class);
450: validate(t, ITarget.class);
451: // instance is not.
452: TargetTest.logCtorExe("post_ITarget");
453: return o;
454: }
455:
456: /**
457: * @After ector_this(caller) && target(t)
458: */
459: public void ector_afterITarget(ITarget t, Object caller) {
460: validate(t, ITarget.class);
461: validate(caller, ITarget.class);
462: TargetTest.logCtorExe("after_ITarget");
463: }
464:
465: // interface implementation
466:
467: /**
468: * @Before ector_this(caller) && target(t)
469: */
470: public void ector_beforeTargetI(TargetI t, Object caller) {
471: validate(t, TargetI.class);
472: validate(caller, TargetI.class);
473: TargetTest.logCtorExe("before_TargetI");
474: }
475:
476: /**
477: * @Around ector_this(caller) && target(t)
478: */
479: public Object ector_aroundTargetI(JoinPoint jp, TargetI t,
480: Object caller) throws Throwable {
481: validate(t, TargetI.class);
482: validate(caller, TargetI.class);
483: TargetTest.logCtorExe("pre_TargetI");
484: Object o = jp.proceed();
485: //validate(o, TargetI.class);
486: validate(t, TargetI.class);
487: TargetTest.logCtorExe("post_TargetI");
488: return o;
489: }
490:
491: /**
492: * @After ector_this(caller) && target(t)
493: */
494: public void ector_afterTargetI(TargetI t, Object caller) {
495: validate(t, TargetI.class);
496: validate(caller, TargetI.class);
497: TargetTest.logCtorExe("after_TargetI");
498: }
499:
500: // super class
501:
502: /**
503: * @Before ector_this(caller) && target(t)
504: */
505: public void ector_beforeSuperTarget(SuperTarget t, Object caller) {
506: validate(t, SuperTarget.class);
507: validate(caller, SuperTarget.class);
508: TargetTest.logCtorExe("before_SuperTarget");
509: }
510:
511: /**
512: * @Around ector_this(caller) && target(t)
513: */
514: public Object ector_aroundSuperTarget(JoinPoint jp, SuperTarget t,
515: Object caller) throws Throwable {
516: validate(t, SuperTarget.class);
517: validate(caller, SuperTarget.class);
518: TargetTest.logCtorExe("pre_SuperTarget");
519: Object o = jp.proceed();
520: //validate(o, SuperTarget.class);
521: validate(t, SuperTarget.class);
522: TargetTest.logCtorExe("post_SuperTarget");
523: return o;
524: }
525:
526: /**
527: * @After ector_this(caller) && target(t)
528: */
529: public void ector_afterSuperTarget(SuperTarget t, Object caller) {
530: validate(t, SuperTarget.class);
531: validate(caller, SuperTarget.class);
532: TargetTest.logCtorExe("after_SuperTarget");
533: }
534:
535: //------------------------- Method call while "this" is subclassed
536:
537: /**
538: * @Expression this(caller) && call(* test.thistarget.*.call()) && withincode(* test.*.*.callFrom(..))
539: */
540: Pointcut call_this Subinterface(IThis caller) {
541: return null;
542: }
543:
544: // interface, while this implements the interface we match
545:
546: /**
547: * @Before call_thisSubinterface(caller) && target(t)
548: */
549: public void beforeICallSubinterface(ITarget t, Object caller) {
550: validate(t, ITarget.class);
551: validate(caller, IThis.class);
552: TargetTest.log("before_ITarget");
553: }
554:
555: /**
556: * @Around call_thisSubinterface(caller) && target(t)
557: */
558: public Object aroundICallSubinterface(JoinPoint jp, ITarget t,
559: Object caller) throws Throwable {
560: validate(t, ITarget.class);
561: validate(caller, IThis.class);
562: TargetTest.log("pre_ITarget");
563: Object o = jp.proceed();
564: TargetTest.log("post_ITarget");
565: return o;
566: }
567:
568: /**
569: * @After call_thisSubinterface(caller) && target(t)
570: */
571: public void afterICallSubinterface(ITarget t, Object caller) {
572: validate(t, ITarget.class);
573: validate(caller, IThis.class);
574: TargetTest.log("after_ITarget");
575: }
576:
577: /**
578: * @Expression this(caller) && call(* test.thistarget.*.call()) && withincode(* test.*.*.callFrom(..))
579: */
580: Pointcut call_this Subclass(SuperThis caller) {
581: return null;
582: }
583:
584: // interface, while this subclass the class we match
585:
586: /**
587: * @Before call_thisSubclass(caller) && target(t)
588: */
589: public void beforeICallSubclass(ITarget t, Object caller) {
590: validate(t, ITarget.class);
591: validate(caller, SuperThis.class);
592: TargetTest.log("before_ITarget");
593: }
594:
595: /**
596: * @Around call_thisSubclass(caller) && target(t)
597: */
598: public Object aroundICallSubclass(JoinPoint jp, ITarget t,
599: Object caller) throws Throwable {
600: validate(t, ITarget.class);
601: validate(caller, SuperThis.class);
602: TargetTest.log("pre_ITarget");
603: Object o = jp.proceed();
604: TargetTest.log("post_ITarget");
605: return o;
606: }
607:
608: /**
609: * @After call_thisSubclass(caller) && target(t)
610: */
611: public void afterICallSubclass(ITarget t, Object caller) {
612: validate(t, ITarget.class);
613: validate(caller, SuperThis.class);
614: TargetTest.log("after_ITarget");
615: }
616:
617: /**
618: * We need to validate the bounded this/target since if the indexing is broken, we may have
619: * the joinpoint instance instead etc, and if not used, the VM will not complain.
620: *
621: * @param t
622: * @param checkCast
623: */
624: static void validate(Object t, Class checkCast) {
625: if (checkCast == null && t != null) {
626: TestCase.fail("should ne null: " + t.getClass().getName());
627: } else if (checkCast != null) {
628: if (!checkCast.isAssignableFrom(t.getClass())) {
629: TestCase.fail("t " + t.getClass().getName()
630: + " is not instance of " + checkCast.getName());
631: }
632: }
633: }
634:
635: }
|