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.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      /*** @Expression within(test.args.ArgsAdviceTest) */
22      Pointcut in_scope;
23  
24      /*** @Expression in_scope && execution(* matchAll(..)) && args(String, String, long) */
25      Pointcut pc_matchAll;
26  
27      /*** @Expression in_scope && execution(* matchAllWithWildcard(..)) && args(..) */
28      Pointcut pc_matchAllWithWildcard;
29  
30      /*** @Expression in_scope && execution(* getFirst(..)) && args(s, ..) */
31      void pc_getFirst(String s) {;}// here we use "return void" style
32  
33      /*** @Expression in_scope && execution(* changeArg(..)) && args(String, s, long) */
34      Pointcut pc_changeArg(StringBuffer s) {return null;}// here we use "return null" style
35  
36      /*** @Expression in_scope && execution(* orderChangedInPointcutSignature(..)) && args(s0, s1, long) */
37      Pointcut pc_orderChangedInPointcutSignature(String s1, String s0) {return null;}
38  
39      /*** @Expression in_scope && execution(* orderChangedInAdviceSignature(..)) && args(s0, s1, long) */
40      Pointcut pc_orderChangedInAdviceSignature(String s0, String s1) {return null;}
41  
42      /*** @Expression in_scope && execution(* orderChangedInPointcutAndAdviceSignature(..)) && args(s0, s1, long) */
43      Pointcut pc_orderChangedInPointcutAndAdviceSignature(String s1, String s0) {return null;}
44  
45  
46      /*** @Before pc_matchAll || pc_matchAllWithWildcard */
47      public void matchAllBefore(JoinPoint joinPoint) {
48          ((Loggable)joinPoint.getTarget()).log("before ");
49      }
50      /*** @After pc_matchAll || pc_matchAllWithWildcard */
51      public void matchAllAfter(JoinPoint joinPoint) {
52          ((Loggable)joinPoint.getTarget()).log("after ");
53      }
54      /*** @Around pc_matchAll || pc_matchAllWithWildcard */
55      public Object matchAllAround(JoinPoint joinPoint) throws Throwable {
56          ((Loggable)joinPoint.getTarget()).log("before1 ");
57          Object res = joinPoint.proceed();
58          ((Loggable)joinPoint.getTarget()).log("after1 ");
59          return res;
60      }
61  
62  
63  
64      /*** @Before pc_getFirst(as) */
65      public void getFirstBefore(JoinPoint joinPoint, String as) {
66          ((Loggable)joinPoint.getTarget()).log("before " + as + " ");
67      }
68      /*** @After pc_getFirst(as) */
69      public void getFirstAfter(String as, JoinPoint joinPoint) {//here we use some fancy order in the signature
70          ((Loggable)joinPoint.getTarget()).log("after " + as + " ");
71      }
72      /*** @Around pc_getFirst(as) */
73      public Object getFirstAround(JoinPoint joinPoint, String as) throws Throwable {
74          ((Loggable)joinPoint.getTarget()).log("before1 " + as + " ");
75          Object res = joinPoint.proceed();
76          ((Loggable)joinPoint.getTarget()).log("after1 " + as + " ");
77          return res;
78      }
79  
80      /*** @Before in_scope && execution(* getFirstAnonymous(..)) && args(as,String,..) */
81      public void getFirstAnonymousBefore(JoinPoint joinPoint, String as) {
82          ((Loggable)joinPoint.getTarget()).log("before " + as + " ");
83      }
84      /*** @After in_scope && execution(* getFirstAnonymous(..)) && args(as, String, long) */
85      public void getFirstAnonymousAfter(String as, JoinPoint joinPoint) {
86          ((Loggable)joinPoint.getTarget()).log("after " + as + " ");
87      }
88      /*** @Around in_scope && execution(* getFirstAnonymous(..)) && args(as,..) */
89      public Object getFirstAnonymousAround(JoinPoint joinPoint, String as) throws Throwable {
90          ((Loggable)joinPoint.getTarget()).log("before1 " + as + " ");
91          Object res = joinPoint.proceed();
92          ((Loggable)joinPoint.getTarget()).log("after1 " + as + " ");
93          return res;
94      }
95  
96      /*** @Before pc_changeArg(as) */
97      public void changeArgBefore(JoinPoint joinPoint, StringBuffer as) {
98          as.append("x");
99          ((Loggable)joinPoint.getTarget()).log("before " + as.toString() + " ");
100     }
101     /*** @After pc_changeArg(as) */
102     public void changeArgAfter(JoinPoint joinPoint, StringBuffer as) {
103         as.append("x");
104         ((Loggable)joinPoint.getTarget()).log("after " + as.toString() + " ");
105     }
106     /*** @Around pc_changeArg(as) */
107     public Object changeArgAround(StringBuffer as, JoinPoint joinPoint) throws Throwable {//here we use some fancy order in the signature
108         as.append("x");
109         ((Loggable)joinPoint.getTarget()).log("before1 " + as.toString() + " ");
110         Object res = joinPoint.proceed();
111         as.append("x");
112         ((Loggable)joinPoint.getTarget()).log("after1 " + as.toString() + " ");
113         return res;
114     }
115 
116 
117 
118 
119     /*** @Before pc_orderChangedInPointcutSignature(as0, as1) */
120     public void orderChangedInPointcutSignatureBefore(JoinPoint joinPoint, String as0, String as1) {
121         ((Loggable)joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
122     }
123     /*** @After pc_orderChangedInPointcutSignature(as0, as1) */
124     public void orderChangedInPointcutSignatureAfter(JoinPoint joinPoint, String as0, String as1) {
125         ((Loggable)joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
126     }
127     /*** @Around pc_orderChangedInPointcutSignature(as0, as1) */
128     public Object orderChangedInPointcutSignatureAround(JoinPoint joinPoint, String as0, String as1) throws Throwable {
129         ((Loggable)joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
130         Object res = joinPoint.proceed();
131         ((Loggable)joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
132         return res;
133     }
134 
135 
136 
137     /*** @Before pc_orderChangedInAdviceSignature(as1, as0) */
138     public void orderChangedInAdviceSignatureBefore(JoinPoint joinPoint, String as0, String as1) {
139         ((Loggable)joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
140     }
141     /*** @After pc_orderChangedInAdviceSignature(as1, as0) */
142     public void orderChangedInAdviceSignatureAfter(JoinPoint joinPoint, String as0, String as1) {
143         ((Loggable)joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
144     }
145     /*** @Around pc_orderChangedInAdviceSignature(as1, as0) */
146     public Object orderChangedInAdviceSignatureAround(JoinPoint joinPoint, String as0, String as1) throws Throwable {
147         ((Loggable)joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
148         Object res = joinPoint.proceed();
149         ((Loggable)joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
150         return res;
151     }
152 
153 
154 
155     /*** @Before pc_orderChangedInPointcutAndAdviceSignature(as1, as0) */
156     public void orderChangedInPointcutAndAdviceSignatureBefore(JoinPoint joinPoint, String as0, String as1) {
157         ((Loggable)joinPoint.getTarget()).log("before " + as0 + " " + as1 + " ");
158     }
159     /*** @After pc_orderChangedInPointcutAndAdviceSignature(as1, as0) */
160     public void orderChangedInPointcutAndAdviceSignatureAfter(JoinPoint joinPoint, String as0, String as1) {
161         ((Loggable)joinPoint.getTarget()).log("after " + as0 + " " + as1 + " ");
162     }
163     /*** @Around pc_orderChangedInPointcutAndAdviceSignature(as1, as0) */
164     public Object orderChangedInPointcutAndAdviceSignatureAround(JoinPoint joinPoint, String as0, String as1) throws Throwable {
165         ((Loggable)joinPoint.getTarget()).log("before1 " + as0 + " " + as1 + " ");
166         Object res = joinPoint.proceed();
167         ((Loggable)joinPoint.getTarget()).log("after1 " + as0 + " " + as1 + " ");
168         return res;
169     }
170 
171     //-- Method call pointcuts with args
172 
173     /*** @Expression in_scope && call(* callGetFirstAndSecond(..)) && args(l, s) */
174     void pc_callGetFirstAndSecond(long l, String[] s) {};
175 
176     /*** @Before pc_callGetFirstAndSecond(l, s) */
177     public void callGetFirstAndSecondBefore(JoinPoint joinPoint, long l, String[] s) {
178         ((Loggable)joinPoint.getTarget()).log("before " + l + " " + s[0] + "," + s[1] + " ");
179     }
180     /*** @After pc_callGetFirstAndSecond(l, s) */
181     public void callGetFirstAndSecondAfter(JoinPoint joinPoint, long l, String[] s) {
182         ((Loggable)joinPoint.getTarget()).log("after " + l + " " + s[0] + "," + s[1] + " ");
183     }
184     /*** @Around pc_callGetFirstAndSecond(l, s) */
185     public Object callGetFirstAndSecondAround(JoinPoint joinPoint, long l, String[] s) throws Throwable {
186         ((Loggable)joinPoint.getTarget()).log("before1 " + l + " " + s[0] + "," + s[1] + " ");
187         Object res = joinPoint.proceed();
188         ((Loggable)joinPoint.getTarget()).log("after1 " + l + " " + s[0] + "," + s[1] + " ");
189         return res;
190     }
191 
192     //-- Ctor execution pointcuts with args
193     // we are using inner class, so args() is a bit tricky
194 
195     /*** @Expression execution(test.args.ArgsAdviceTest$CtorExecution.new(..)) && args(test.args.ArgsAdviceTest, s) */
196     void pc_ctorExecutionGetFirst(String s) {};
197 
198     /*** @Before pc_ctorExecutionGetFirst(s) */
199     public void ctorExecutionGetFirstBefore(JoinPoint joinPoint, String s) {
200         ((Loggable)joinPoint.getTarget()).log("before " + s+ " ");
201     }
202     /*** @After pc_ctorExecutionGetFirst(s) */
203     public void ctorExecutionGetFirstAfter(JoinPoint joinPoint, String s) {
204         ((Loggable)joinPoint.getTarget()).log("after " + s + " ");
205     }
206     /*** @Around pc_ctorExecutionGetFirst(s) */
207     public Object ctorExecutionGetFirstAround(JoinPoint joinPoint, String s) throws Throwable {
208         ((Loggable)joinPoint.getTarget()).log("before1 " + s + " ");
209         Object res = joinPoint.proceed();
210         ((Loggable)joinPoint.getTarget()).log("after1 " + s + " ");
211         return res;
212     }
213 
214     //-- Ctor call pointcuts with args
215     // we are using inner class, so args() is a bit tricky
216 
217     /*** @Expression in_scope && call(test.args.ArgsAdviceTest$CtorCall.new(..)) && args(test.args.ArgsAdviceTest, s) */
218     void pc_ctorCallGetFirst(String s) {};
219 
220     /*** @Before pc_ctorCallGetFirst(s) */
221     public void ctorCallGetFirstBefore(JoinPoint joinPoint, String s) {
222         ArgsAdviceTest.logStatic("before " + s+ " ");
223     }
224     /*** @After pc_ctorCallGetFirst(s) */
225     public void ctorCallGetFirstAfter(JoinPoint joinPoint, String s) {
226         ArgsAdviceTest.logStatic("after " + s + " ");
227     }
228     /*** @Around pc_ctorCallGetFirst(s) */
229     public Object ctorCallGetFirstAround(JoinPoint joinPoint, String s) throws Throwable {
230         ArgsAdviceTest.logStatic("before1 " + s + " ");
231         Object res = joinPoint.proceed();
232         ArgsAdviceTest.logStatic("after1 " + s + " ");
233         return res;
234     }
235 
236     //-- field set with args()
237     /*** @Expression in_scope && set(* m_field) && args(s) */
238     void pc_mfield(String s) {};
239 
240     /*** @Before pc_mfield(s) */
241     public void mfieldBefore(JoinPoint joinPoint, String s) {
242         String fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
243         ((Loggable)joinPoint.getTarget()).log("before " + fieldValue + "," + s + " ");
244     }
245     /*** @After pc_mfield(s) */
246     public void mfieldAfter(JoinPoint joinPoint, String s) {
247         String fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
248         ((Loggable)joinPoint.getTarget()).log("after " + fieldValue + "," + s + " ");
249     }
250     /*** @Around pc_mfield(s) */
251     public Object mfieldAround(JoinPoint joinPoint, String s) throws Throwable {
252         String fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
253         ((Loggable)joinPoint.getTarget()).log("before1 " + fieldValue + "," + s + " ");
254         s = "changed"; // will be ignored due to delegation ! [AJ]
255         Object res = joinPoint.proceed();
256         fieldValue = ((ArgsAdviceTest)joinPoint.getTarget()).getField();
257         ((Loggable)joinPoint.getTarget()).log("after1 " + fieldValue + "," + s + " ");
258         return "ignored";
259     }
260 
261     //-- static field set with args()
262     /*** @Expression in_scope && set(* s_field) && args(s) */
263     void pc_sfield(String s) {};
264 
265     /*** @Before pc_sfield(s) */
266     public void sfieldBefore(JoinPoint joinPoint, String s) {
267         String fieldValue = ArgsAdviceTest.getStaticField();
268         ArgsAdviceTest.logStatic("before " + fieldValue + "," + s + " ");
269     }
270     /*** @After pc_sfield(s) */
271     public void sfieldAfter(JoinPoint joinPoint, String s) {
272         String fieldValue = ArgsAdviceTest.getStaticField();
273         ArgsAdviceTest.logStatic("after " + fieldValue + "," + s + " ");
274     }
275     /*** @Around pc_sfield(s) */
276     public Object sfieldAround(JoinPoint joinPoint, String s) throws Throwable {
277         String fieldValue = ArgsAdviceTest.getStaticField();
278         ArgsAdviceTest.logStatic("before1 " + fieldValue + "," + s + " ");
279         s = "changed"; // will be ignored due to delegation ! [AJ]
280         Object res = joinPoint.proceed();
281         fieldValue = ArgsAdviceTest.getStaticField();
282         ArgsAdviceTest.logStatic("after1 " + fieldValue + "," + s + " ");
283         return "ignored";
284     }
285 
286 }