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