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.aspect;
008:
009: import test.Loggable;
010: import org.codehaus.aspectwerkz.definition.Pointcut;
011: import org.codehaus.aspectwerkz.definition.Pointcut;
012: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
013: import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
014: import org.codehaus.aspectwerkz.joinpoint.Rtti;
015:
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileOutputStream;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutput;
021: import java.io.ObjectOutputStream;
022:
023: /**
024: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
025: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
026: * @Aspect perJVM
027: */
028: public class MemberMethodTestAspect {
029: // ============ Pointcuts ============
030:
031: /**
032: * @Expression call(* test.MemberMethodAdviceTest.get*(..)) && within(test.MemberMethodAdviceTest)
033: */
034: Pointcut member_pc1;
035:
036: /**
037: * @Expression execution(* test.MemberMethodAdviceTest.*Param**(..))
038: */
039: Pointcut member_pc2;
040:
041: /**
042: * @Expression execution(* test.MemberMethodAdviceTest.exceptionThrower*(..))
043: */
044: Pointcut member_pc3;
045:
046: /**
047: * @Expression call(* test.MemberMethodAdviceTest.methodAdvicedMethod()) && within(test.MemberMethodAdviceTest)
048: */
049: Pointcut member_pc4;
050:
051: /**
052: * @Expression execution(* test.MemberMethodAdviceTest.meth*AdvicedMethod())
053: */
054: Pointcut member_pc5;
055:
056: /**
057: * @Expression call(* test.MemberMethodAdviceTest.method*icedMethodNewThread(..)) && within(test.MemberMethodAdviceTest)
058: */
059: Pointcut member_pc6;
060:
061: /**
062: * @Expression execution(* test.MemberMethodAdviceTest.method*dvicedMethodNewThread(..))
063: */
064: Pointcut member_pc7;
065:
066: /**
067: * @Expression call(* test.MemberMethodAdviceTest.multipleMethodAdvicedMethod(..)) && within(test.MemberMethodAdviceTest)
068: */
069: Pointcut member_pc8;
070:
071: /**
072: * @Expression execution(* test.MemberMethodAdviceTest.multipleChainedMethodAdvicedMethod(..))
073: */
074: Pointcut member_pc9;
075:
076: /**
077: * @Expression call(* test.MemberMethodAdviceTest.joinPointMetaData(..)) && within(test.MemberMethodAdviceTest)
078: */
079: Pointcut member_pc10;
080:
081: /**
082: * @Expression call(void test.MemberMethodAdviceTest.passingParameterToAdviceMethod(..)) && within(test.MemberMethodAdviceTest)
083: */
084: Pointcut member_pc11;
085:
086: /**
087: * @Expression execution(void test.MemberMethodAdviceTest.multiplePointcutsMethod(..))
088: */
089: Pointcut member_pc12;
090:
091: /**
092: * @Expression call(void test.MemberMethodAdviceTest.multiplePointcutsMethod(..)) && within(test.MemberMethodAdviceTest)
093: */
094: Pointcut member_pc13;
095:
096: /**
097: * @Expression execution(* test.MemberMethodAdviceTest.takesArrayAsArgument(String[]))
098: */
099: Pointcut member_pc14;
100:
101: /**
102: * @Expression call(long test.MemberMethodAdviceTest.getPrimitiveAndNullFromAdvice()) && within(test.MemberMethodAdviceTest)
103: */
104: Pointcut member_pc15;
105:
106: /**
107: * @Expression execution(void test.MemberMethodAdviceTest.beforeAdvicedMethod())
108: */
109: Pointcut member_pc16;
110:
111: /**
112: * @Expression call(void test.MemberMethodAdviceTest.afterAdvicedMethod()) && within(test.MemberMethodAdviceTest)
113: */
114: Pointcut member_pc17;
115:
116: /**
117: * @Expression execution(void test.MemberMethodAdviceTest.beforeAfterAdvicedMethod())
118: */
119: Pointcut member_pc18;
120:
121: /**
122: * @Expression call(void test.MemberMethodAdviceTest.beforeAroundAfterAdvicedMethod()) && within(test.MemberMethodAdviceTest)
123: */
124: Pointcut member_pc19;
125:
126: /**
127: * @Expression call(* test.MemberMethodAdviceTest.longNoAroundAdvice(..)) && within(test.MemberMethodAdviceTest)
128: */
129: Pointcut noAroundAdvice;
130:
131: // ============ Advices ============
132:
133: /**
134: * @Around member_pc1 || member_pc2 || member_pc3 || member_pc4 || member_pc14 || member_pc9
135: */
136: public Object advice1(final JoinPoint joinPoint) throws Throwable {
137: return joinPoint.proceed();
138: }
139:
140: /**
141: * @Around member_pc5 || member_pc8 || member_pc9 || member_pc12 || member_pc19
142: */
143: public Object advice2(final JoinPoint joinPoint) throws Throwable {
144: ((Loggable) joinPoint.getTarget()).log("before1 ");
145: final Object result = joinPoint.proceed();
146: ((Loggable) joinPoint.getTarget()).log("after1 ");
147: return result;
148: }
149:
150: /**
151: * @Around member_pc8 || member_pc9 || member_pc13 || member_pc19
152: */
153: public Object advice3(final JoinPoint joinPoint) throws Throwable {
154: ((Loggable) joinPoint.getTarget()).log("before2 ");
155: final Object result = joinPoint.proceed();
156: ((Loggable) joinPoint.getTarget()).log("after2 ");
157: return result;
158: }
159:
160: /**
161: * @Around member_pc10
162: */
163: public Object advice4(JoinPoint joinPoint) throws Throwable {
164: final Object result = joinPoint.proceed();
165: MethodRtti mrtti = (MethodRtti) joinPoint.getRtti();
166: String metadata = joinPoint.getCalleeClass().getName()
167: + mrtti.getMethod().getName()
168: + joinPoint.getTarget().hashCode()
169: + mrtti.getParameterValues()[0]
170: + mrtti.getParameterTypes()[0].getName()
171: + mrtti.getReturnType().getName()
172: + mrtti.getReturnValue();
173: return metadata;
174: }
175:
176: /**
177: * @Around member_pc6 || member_pc7
178: */
179: public Object advice5(final JoinPoint joinPoint) throws Throwable {
180: ((Loggable) joinPoint.getTarget()).log("before ");
181: final Object result = joinPoint.proceed();
182: ((Loggable) joinPoint.getTarget()).log("after ");
183: return result;
184: }
185:
186: /**
187: * @Around member_pc15
188: */
189: public Object advice6(JoinPoint joinPoint) throws Throwable {
190: // test to serialize the join point instance
191: try {
192: ObjectOutput out = new ObjectOutputStream(
193: new FileOutputStream("joinpoint.ser"));
194: out.writeObject(joinPoint);
195: out.close();
196: File file = new File("joinpoint.ser");
197: ObjectInputStream in = new ObjectInputStream(
198: new FileInputStream(file));
199: joinPoint = (JoinPoint) in.readObject();
200: in.close();
201: } catch (Exception e) {
202: System.err
203: .println("FIXME: serialization for JIT compiled join points");
204: }
205: return null;
206: }
207:
208: /**
209: * @Before member_pc16 || member_pc18 || member_pc19 || noAroundAdvice
210: */
211: public void before(final JoinPoint joinPoint) throws Throwable {
212: ((Loggable) joinPoint.getTarget()).log("pre ");
213: }
214:
215: /**
216: * @After member_pc17 || member_pc18 || member_pc19
217: */
218: public void after(final JoinPoint joinPoint) throws Throwable {
219: ((Loggable) joinPoint.getTarget()).log("post ");
220: }
221:
222: /**
223: * @After call(* test.MemberMethodAdviceTest.callWithincodeCtor(..))
224: * && withincode(test.MemberMethodAdviceTest.new(int))
225: */
226: public void afterWithinCtor(final JoinPoint joinPoint) {
227: ((Loggable) joinPoint.getTarget()).log("post ");
228: }
229:
230: }
|