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.aspect;
9   
10  import test.FieldAdviceTest;
11  import org.codehaus.aspectwerkz.Pointcut;
12  import org.codehaus.aspectwerkz.joinpoint.FieldRtti;
13  import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
14  
15  /***
16   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
17   * @Aspect perJVM
18   */
19  public class FieldTestAspect {
20      // ============ Pointcuts ============
21  
22      /***
23       * @Expression set(* test.FieldAdviceTest.m_setFieldPreAdvice*)
24       */
25      Pointcut pc1;
26  
27      /***
28       * @Expression set(int test.FieldAdviceTest.m_setFieldPreAdvi*)
29       */
30      Pointcut pc2;
31  
32      /***
33       * @Expression set(* test.FieldAdviceTest.m_setFie*dPostAdviced)
34       */
35      Pointcut pc3;
36  
37      /***
38       * @Expression set(* test.FieldAdviceTest.m_se*FieldPostAdviced)
39       */
40      Pointcut pc4;
41  
42      /***
43       * @Expression set(* test.FieldAdviceTest.m_setFieldPrePostAdviced)
44       */
45      Pointcut pc5;
46  
47      /***
48       * @Expression get(* test.FieldAdviceTest.m_getFieldPreAdvic*)
49       */
50      Pointcut pc6;
51  
52      /***
53       * @Expression get(* test.FieldAdviceTest.m_getFieldPreAdvice*)
54       */
55      Pointcut pc7;
56  
57      /***
58       * @Expression get(* test.FieldAdviceTest.m_getFieldPostAdviced)
59       */
60      Pointcut pc8;
61  
62      /***
63       * @Expression get(* test.FieldAdviceTest.m_getFieldPrePostAdviced)
64       */
65      Pointcut pc9;
66  
67      /***
68       * @Expression set(* test.FieldAdviceTest.s_setStaticFieldPreAdvic*)
69       */
70      Pointcut pc10;
71  
72      /***
73       * @Expression set(* test.FieldAdviceTest.s_setStaticFieldPreAdvice*)
74       */
75      Pointcut pc11;
76  
77      /***
78       * @Expression set(* test.FieldAdviceTest.s_setStaticFieldPostAdviced)
79       */
80      Pointcut pc12;
81  
82      /***
83       * @Expression set(* test.FieldAdviceTest.s_setStaticFieldPrePostAdviced)
84       */
85      Pointcut pc13;
86  
87      /***
88       * @Expression get(* test.FieldAdviceTest.s_getStaticFieldPreAdvice*)
89       */
90      Pointcut pc14;
91  
92      /***
93       * @Expression get(* test.FieldAdviceTest.s_getStaticFieldPreAdvic*)
94       */
95      Pointcut pc15;
96  
97      /***
98       * @Expression get(* test.FieldAdviceTest.s_getStaticFieldPostAdviced)
99       */
100     Pointcut pc16;
101 
102     /***
103      * @Expression get(* test.FieldAdviceTest.s_getStaticFieldPrePostAdviced)
104      */
105     Pointcut pc17;
106 
107     /***
108      * @Expression set(* test.FieldAdviceTest.m_setFieldAroundAdviced)
109      */
110     Pointcut pc18;
111 
112     /***
113      * @Expression set(* test.FieldAdviceTest.s_setStaticFieldAroundAdviced)
114      */
115     Pointcut pc19;
116 
117     /***
118      * @Expression get(* test.FieldAdviceTest.m_getFieldAroundAdviced)
119      */
120     Pointcut pc20;
121 
122     /***
123      * @Expression get(* test.FieldAdviceTest.s_getStaticFieldAroundAdviced)
124      */
125     Pointcut pc21;
126 
127     /***
128      * @Expression set(* test.FieldAdviceTest.m_setFieldAroundAdviced*WithNullAdvice)
129      */
130     Pointcut pc22;
131 
132     /***
133      * @Expression get(* test.FieldAdviceTest.m_getFieldAroundAdvicedWithNullAdvice)
134      */
135     Pointcut pc23;
136 
137     /***
138      * @Expression set(* test.FieldAdviceTest.m_setFieldAroundAdvicedObjectWithAPI)
139      */
140     Pointcut pc24;
141 
142     /***
143      * @Expression set(* test.FieldAdviceTest.m_setFieldAroundAdvicedWithAPI)
144      */
145     Pointcut pc25;
146 
147     // ============ Advices ============
148 
149     /***
150      * @Before pc2 || pc5 || pc10 || pc13 || pc6 || pc9 || pc14 || pc17
151      */
152     public void preAdvice1(final JoinPoint joinPoint) throws Throwable {
153         FieldAdviceTest.log("pre1 ");
154     }
155 
156     /***
157      * @Before pc1 || pc5 || pc11 || pc13 || pc7 || pc9 || pc15 || pc17
158      */
159     public void preAdvice2(final JoinPoint joinPoint) throws Throwable {
160         FieldAdviceTest.log("pre2 ");
161     }
162 
163     /***
164      * @After pc4 || pc5 || pc12 || pc13 || pc8 || pc9 || pc16 || pc17
165      */
166     public void postAdvice1(final JoinPoint joinPoint) throws Throwable {
167         FieldAdviceTest.log("post1 ");
168     }
169 
170     /***
171      * @After pc3 || pc5 || pc12 || pc13 || pc8 || pc9 || pc16 || pc17
172      */
173     public void postAdvice2(final JoinPoint joinPoint) throws Throwable {
174         FieldAdviceTest.log("post2 ");
175     }
176 
177     /***
178      * @Around pc18 || pc19 || pc20 || pc21
179      */
180     public Object around(final JoinPoint joinPoint) throws Throwable {
181         FieldAdviceTest.log("before ");
182         final Object result = joinPoint.proceed();
183         FieldAdviceTest.log("after ");
184         return result;
185     }
186 
187     /***
188      * @Around pc22 || pc23
189      */
190     public Object aroundNullAdvice(final JoinPoint joinPoint) throws Throwable {
191         FieldAdviceTest.log("before ");
192         final Object result = joinPoint.proceed();
193         FieldAdviceTest.log("after ");
194         return null;
195     }
196 
197     /***
198      * @Around pc24
199      */
200     public Object aroundAdviceAltering(final JoinPoint joinPoint) throws Throwable {
201         FieldAdviceTest.log("before ");
202         FieldRtti rtti = (FieldRtti) joinPoint.getRtti();
203         rtti.setFieldValue(new String("byAdvice"));
204         joinPoint.proceed();
205         FieldAdviceTest.log("after ");
206         return null;
207     }
208 
209     /***
210      * @Around pc25
211      */
212     public Object aroundAdviceAlteringPrimitive(final JoinPoint joinPoint) throws Throwable {
213         FieldAdviceTest.log("before ");
214         FieldRtti rtti = (FieldRtti) joinPoint.getRtti();
215         rtti.setFieldValue(new Integer(3));
216         joinPoint.proceed();
217         FieldAdviceTest.log("after ");
218         return null;
219     }
220 }