KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > transform > spring > SpringAspectModel


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 org.codehaus.aspectwerkz.transform.spring;
9
10 import org.codehaus.aspectwerkz.transform.aopalliance.AopAllianceAspectModel;
11 import org.codehaus.aspectwerkz.transform.inlining.spi.AspectModel;
12 import org.codehaus.aspectwerkz.transform.inlining.AdviceMethodInfo;
13 import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
14 import org.codehaus.aspectwerkz.reflect.ClassInfo;
15 import org.codehaus.aspectwerkz.definition.AspectDefinition;
16
17 import org.aopalliance.intercept.MethodInterceptor;
18
19 import org.springframework.aop.AfterReturningAdvice;
20 import org.springframework.aop.MethodBeforeAdvice;
21 import org.springframework.aop.ThrowsAdvice;
22
23 //import org.objectweb.asm.CodeVisitor;
24
//import org.objectweb.asm.ClassWriter;
25
//import org.objectweb.asm.Type;
26
import org.codehaus.aspectwerkz.org.objectweb.asm.CodeVisitor;
27 import org.codehaus.aspectwerkz.org.objectweb.asm.ClassWriter;
28 import org.codehaus.aspectwerkz.org.objectweb.asm.Type;
29
30 /**
31  * Implementation of the AspectModel interface for Spring framework.
32  * <p/>
33  * Provides methods for definition of aspects and framework specific bytecode generation
34  * used by the join point compiler.
35  *
36  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér </a>
37  */

38 public class SpringAspectModel extends AopAllianceAspectModel {
39
40     protected static final String JavaDoc ASPECT_MODEL_TYPE = "spring";
41
42     /**
43      * Returns the aspect model type, which is an id for the the special aspect model, can be anything as long
44      * as it is unique.
45      *
46      * @return the aspect model type id
47      */

48     public String JavaDoc getAspectModelType() {
49         return ASPECT_MODEL_TYPE;
50     }
51
52     /**
53      * Defines the aspect.
54      *
55      * @param classInfo
56      * @param aspectDef
57      * @param loader
58      */

59     public void defineAspect(final ClassInfo classInfo,
60                              final AspectDefinition aspectDef,
61                              final ClassLoader JavaDoc loader) {
62         ClassInfo[] interfaces = classInfo.getInterfaces();
63         for (int i = 0; i < interfaces.length; i++) {
64             ClassInfo anInterface = interfaces[i];
65             if (anInterface.getName().equals(MethodInterceptor.class.getName()) ||
66                 anInterface.getName().equals(MethodBeforeAdvice.class.getName()) ||
67                 anInterface.getName().equals(AfterReturningAdvice.class.getName()) ||
68                 anInterface.getName().equals(ThrowsAdvice.class.getName())) {
69                 aspectDef.setAspectModel(ASPECT_MODEL_TYPE);
70                 aspectDef.setContainerClassName(ASPECT_CONTAINER_CLASS_NAME);
71                 return;
72             }
73         }
74     }
75
76     /**
77      * Returns info about the closure class, name and type (interface or class).
78      *
79      * @return the closure class info
80      */

81     public AroundClosureClassInfo getAroundClosureClassInfo() {
82         return new AspectModel.AroundClosureClassInfo(
83                 null,
84                 new String JavaDoc[]{
85                     AOP_ALLIANCE_CLOSURE_CLASS_NAME,
86                     MethodBeforeAdvice.class.getName().replace('.', '/'),
87                     AfterReturningAdvice.class.getName().replace('.', '/')
88                 }
89         );
90     }
91
92
93     /**
94      * Creates the methods required to implement or extend to implement the closure for the specific
95      * aspect model type.
96      *
97      * @param cw
98      * @param className
99      */

100     public void createMandatoryMethods(final ClassWriter cw, final String JavaDoc className) {
101         super.createMandatoryMethods(cw, className);
102     }
103
104     /**
105      * Handles the arguments to the before advice.
106      *
107      * @param cv
108      * @param adviceMethodInfo
109      */

110     public void createBeforeAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) {
111         final String JavaDoc joinPointClassName = adviceMethodInfo.getJoinPointClassName();
112         final int joinPointIndex = adviceMethodInfo.getJoinPointIndex();
113         cv.visitFieldInsn(
114                 GETSTATIC,
115                 joinPointClassName,
116                 SIGNATURE_FIELD_NAME,
117                 METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE
118         );
119         cv.visitMethodInsn(
120                 INVOKEVIRTUAL,
121                 METHOD_SIGNATURE_IMPL_CLASS_NAME,
122                 GET_METHOD_METHOD_NAME,
123                 GET_METHOD_METHOD_SIGNATURE
124         );
125
126         if (Type.getArgumentTypes(adviceMethodInfo.getCalleeMemberDesc()).length == 0) {
127             cv.visitInsn(ACONST_NULL);
128         } else {
129             cv.visitVarInsn(ALOAD, joinPointIndex);
130             cv.visitMethodInsn(
131                     INVOKEVIRTUAL,
132                     joinPointClassName,
133                     GET_RTTI_METHOD_NAME,
134                     GET_RTTI_METHOD_SIGNATURE
135             );
136             cv.visitTypeInsn(CHECKCAST, METHOD_RTTI_IMPL_CLASS_NAME);
137             cv.visitMethodInsn(
138                     INVOKEVIRTUAL,
139                     METHOD_RTTI_IMPL_CLASS_NAME,
140                     GET_PARAMETER_VALUES_METHOD_NAME,
141                     GET_ARGUMENTS_METHOD_SIGNATURE
142             );
143         }
144         cv.visitVarInsn(ALOAD, joinPointIndex);
145         cv.visitFieldInsn(
146                 GETFIELD,
147                 joinPointClassName,
148                 CALLEE_INSTANCE_FIELD_NAME,
149                 adviceMethodInfo.getCalleeClassSignature()
150         );
151     }
152
153     /**
154      * Handles the arguments to the after advice.
155      *
156      * @param cv
157      * @param adviceMethodInfo
158      */

159     public void createAfterAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) {
160         final String JavaDoc joinPointClassName = adviceMethodInfo.getJoinPointClassName();
161         final int joinPointIndex = adviceMethodInfo.getJoinPointIndex();
162         final String JavaDoc specArgDesc = adviceMethodInfo.getSpecialArgumentTypeDesc();
163         if (specArgDesc == null) {
164             cv.visitInsn(ACONST_NULL);
165         } else {
166             cv.visitVarInsn(ALOAD, adviceMethodInfo.getSpecialArgumentIndex());
167             AsmHelper.wrapPrimitiveType(cv, Type.getType(specArgDesc));
168         }
169         cv.visitFieldInsn(
170                 GETSTATIC,
171                 joinPointClassName,
172                 SIGNATURE_FIELD_NAME,
173                 METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE
174         );
175         cv.visitMethodInsn(
176                 INVOKEVIRTUAL,
177                 METHOD_SIGNATURE_IMPL_CLASS_NAME,
178                 GET_METHOD_METHOD_NAME,
179                 GET_METHOD_METHOD_SIGNATURE
180         );
181
182         if (Type.getArgumentTypes(adviceMethodInfo.getCalleeMemberDesc()).length == 0) {
183             cv.visitInsn(ACONST_NULL);
184         } else {
185             cv.visitVarInsn(ALOAD, joinPointIndex);
186             cv.visitMethodInsn(
187                     INVOKEVIRTUAL,
188                     joinPointClassName,
189                     GET_RTTI_METHOD_NAME,
190                     GET_RTTI_METHOD_SIGNATURE
191             );
192             cv.visitTypeInsn(CHECKCAST, METHOD_RTTI_IMPL_CLASS_NAME);
193             cv.visitMethodInsn(
194                     INVOKEVIRTUAL,
195                     METHOD_RTTI_IMPL_CLASS_NAME,
196                     GET_PARAMETER_VALUES_METHOD_NAME,
197                     GET_ARGUMENTS_METHOD_SIGNATURE
198             );
199         }
200
201         cv.visitVarInsn(ALOAD, joinPointIndex);
202         cv.visitFieldInsn(
203                 GETFIELD,
204                 joinPointClassName,
205                 CALLEE_INSTANCE_FIELD_NAME,
206                 adviceMethodInfo.getCalleeClassSignature()
207         );
208     }
209 }
210
Popular Tags