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.Loggable;
11  import org.codehaus.aspectwerkz.Pointcut;
12  import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
13  import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
14  
15  import java.io.File;
16  import java.io.FileInputStream;
17  import java.io.FileOutputStream;
18  import java.io.ObjectInputStream;
19  import java.io.ObjectOutput;
20  import java.io.ObjectOutputStream;
21  
22  /***
23   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
24   * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
25   * @Aspect perJVM
26   */
27  public class MemberMethodTestAspect {
28      // ============ Pointcuts ============
29  
30      /***
31       * @Expression execution(* test.MemberMethodAdviceTest.get*(..))
32       */
33      Pointcut member_pc1;
34  
35      /***
36       * @Expression execution(* test.MemberMethodAdviceTest.*Param**(..))
37       */
38      Pointcut member_pc2;
39  
40      /***
41       * @Expression execution(* test.MemberMethodAdviceTest.exceptionThrower*(..))
42       */
43      Pointcut member_pc3;
44  
45      /***
46       * @Expression execution(* test.MemberMethodAdviceTest.methodAdvicedMethod())
47       */
48      Pointcut member_pc4;
49  
50      /***
51       * @Expression execution(* test.MemberMethodAdviceTest.meth*AdvicedMethod())
52       */
53      Pointcut member_pc5;
54  
55      /***
56       * @Expression call(* test.MemberMethodAdviceTest.method*icedMethodNewThread(..))
57       */
58      Pointcut member_pc6;
59  
60      /***
61       * @Expression execution(* test.MemberMethodAdviceTest.method*dvicedMethodNewThread(..))
62       */
63      Pointcut member_pc7;
64  
65      /***
66       * @Expression execution(* test.MemberMethodAdviceTest.multipleMethodAdvicedMethod(..))
67       */
68      Pointcut member_pc8;
69  
70      /***
71       * @Expression execution(* test.MemberMethodAdviceTest.multipleChainedMethodAdvicedMethod(..))
72       */
73      Pointcut member_pc9;
74  
75      /***
76       * @Expression execution(* test.MemberMethodAdviceTest.joinPointMetaData(..))
77       */
78      Pointcut member_pc10;
79  
80      /***
81       * @Expression execution(void test.MemberMethodAdviceTest.passingParameterToAdviceMethod(..))
82       */
83      Pointcut member_pc11;
84  
85      /***
86       * @Expression execution(void test.MemberMethodAdviceTest.multiplePointcutsMethod(..))
87       */
88      Pointcut member_pc12;
89  
90      /***
91       * @Expression call(void test.MemberMethodAdviceTest.multiplePointcutsMethod(..))
92       */
93      Pointcut member_pc13;
94  
95      /***
96       * @Expression execution(* test.MemberMethodAdviceTest.takesArrayAsArgument(String[]))
97       */
98      Pointcut member_pc14;
99  
100     /***
101      * @Expression execution(long test.MemberMethodAdviceTest.getPrimitiveAndNullFromAdvice())
102      */
103     Pointcut member_pc15;
104 
105     /***
106      * @Expression execution(void test.MemberMethodAdviceTest.beforeAdvicedMethod())
107      */
108     Pointcut member_pc16;
109 
110     /***
111      * @Expression execution(void test.MemberMethodAdviceTest.afterAdvicedMethod())
112      */
113     Pointcut member_pc17;
114 
115     /***
116      * @Expression execution(void test.MemberMethodAdviceTest.beforeAfterAdvicedMethod())
117      */
118     Pointcut member_pc18;
119 
120     /***
121      * @Expression execution(void test.MemberMethodAdviceTest.beforeAroundAfterAdvicedMethod())
122      */
123     Pointcut member_pc19;
124 
125     /***
126      * @Expression execution(* test.MemberMethodAdviceTest.longNoAroundAdvice(..))
127      */
128     Pointcut noAroundAdvice;
129 
130     // ============ Advices ============
131 
132     /***
133      * @Around member_pc1 || member_pc2 || member_pc3 || member_pc4 || member_pc14 || member_pc9
134      */
135     public Object advice1(final JoinPoint joinPoint) throws Throwable {
136         return joinPoint.proceed();
137     }
138 
139     /***
140      * @Around member_pc5 || member_pc8 || member_pc9 || member_pc12 || member_pc19
141      */
142     public Object advice2(final JoinPoint joinPoint) throws Throwable {
143         ((Loggable) joinPoint.getTarget()).log("before1 ");
144         final Object result = joinPoint.proceed();
145         ((Loggable) joinPoint.getTarget()).log("after1 ");
146         return result;
147     }
148 
149     /***
150      * @Around member_pc8 || member_pc9 || member_pc13 || member_pc19
151      */
152     public Object advice3(final JoinPoint joinPoint) throws Throwable {
153         ((Loggable) joinPoint.getTarget()).log("before2 ");
154         final Object result = joinPoint.proceed();
155         ((Loggable) joinPoint.getTarget()).log("after2 ");
156         return result;
157     }
158 
159     /***
160      * @Around member_pc10
161      */
162     public Object advice4(JoinPoint joinPoint) throws Throwable {
163         final Object result = joinPoint.proceed();
164         MethodRtti rtti = (MethodRtti) joinPoint.getRtti();
165         String metadata = joinPoint.getTargetClass().getName()
166             + rtti.getMethod().getName()
167             + joinPoint.getTarget().hashCode()
168             + rtti.getParameterValues()[0]
169             + rtti.getParameterTypes()[0].getName()
170             + rtti.getReturnType().getName()
171             + rtti.getReturnValue();
172         return metadata;
173     }
174 
175     /***
176      * @Around member_pc6 || member_pc7
177      */
178     public Object advice5(final JoinPoint joinPoint) throws Throwable {
179         ((Loggable) joinPoint.getTarget()).log("before ");
180         final Object result = joinPoint.proceed();
181         ((Loggable) joinPoint.getTarget()).log("after ");
182         return result;
183     }
184 
185     /***
186      * @Around member_pc15
187      */
188     public Object advice6(JoinPoint joinPoint) throws Throwable {
189         // test to serialize the join point instance
190         try {
191             ObjectOutput out = new ObjectOutputStream(new FileOutputStream("joinpoint.ser"));
192             out.writeObject(joinPoint);
193             out.close();
194             File file = new File("joinpoint.ser");
195             ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
196             joinPoint = (JoinPoint) in.readObject();
197             in.close();
198         } catch (Exception e) {
199             System.err.println("FIXME: serialization for JIT compiled join points");
200         }
201         return null;
202     }
203 
204     /***
205      * @Before member_pc16 || member_pc18 || member_pc19 || noAroundAdvice
206      */
207     public void before(final JoinPoint joinPoint) throws Throwable {
208         ((Loggable) joinPoint.getTarget()).log("pre ");
209     }
210 
211     /***
212      * @After member_pc17 || member_pc18 || member_pc19
213      */
214     public void after(final JoinPoint joinPoint) throws Throwable {
215         ((Loggable) joinPoint.getTarget()).log("post ");
216     }
217 
218     /***
219      * @After call(* test.MemberMethodAdviceTest.callWithincodeCtor(..))
220      *        && withincode(test.MemberMethodAdviceTest.new(int))
221      */
222     public void afterWithinCtor(final JoinPoint joinPoint) {
223         ((Loggable) joinPoint.getTarget()).log("post ");
224     }
225 
226 }