KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > transform > aopalliance > AopAllianceAspectModel


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.aopalliance;
9
10 import org.codehaus.aspectwerkz.definition.AspectDefinition;
11 import org.codehaus.aspectwerkz.reflect.ClassInfo;
12 import org.codehaus.aspectwerkz.transform.inlining.spi.AspectModel;
13 import org.codehaus.aspectwerkz.transform.inlining.AdviceMethodInfo;
14 import org.codehaus.aspectwerkz.transform.inlining.AspectInfo;
15 import org.codehaus.aspectwerkz.transform.inlining.compiler.AbstractJoinPointCompiler;
16 import org.codehaus.aspectwerkz.transform.TransformationConstants;
17
18 import org.aopalliance.intercept.MethodInterceptor;
19 import org.aopalliance.intercept.ConstructorInterceptor;
20
21 //import org.objectweb.asm.CodeVisitor;
22
//import org.objectweb.asm.ClassWriter;
23
import org.codehaus.aspectwerkz.org.objectweb.asm.CodeVisitor;
24 import org.codehaus.aspectwerkz.org.objectweb.asm.ClassWriter;
25
26 /**
27  * TODO support ConstructorInvocation (1h work) (plus tests)
28  * <p/>
29  * Implementation of the AspectModel interface for AOP Alliance based frameworks (e.g. Spring, dynaop etc.).
30  * <p/>
31  * Provides methods for definition of aspects and framework specific bytecode generation
32  * used by the join point compiler.
33  *
34  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
35  */

36 public class AopAllianceAspectModel implements AspectModel, TransformationConstants {
37
38     protected static final String JavaDoc ASPECT_MODEL_TYPE = "aop-alliance";
39     protected static final String JavaDoc AOP_ALLIANCE_CLOSURE_CLASS_NAME = "org/aopalliance/intercept/MethodInvocation";
40     protected static final String JavaDoc AOP_ALLIANCE_CLOSURE_PROCEED_METHOD_NAME = "invoke";
41     protected static final String JavaDoc AOP_ALLIANCE_CLOSURE_PROCEED_METHOD_SIGNATURE = "(Lorg/aopalliance/intercept/MethodInvocation;)Ljava/lang/Object;";
42     protected static final String JavaDoc ASPECT_CONTAINER_CLASS_NAME = AopAllianceAspectContainer.class.getName();
43     protected static final String JavaDoc GET_METHOD_METHOD_NAME = "getMethod";
44     protected static final String JavaDoc GET_METHOD_METHOD_SIGNATURE = "()Ljava/lang/reflect/Method;";
45     protected static final String JavaDoc GET_STATIC_PART_METHOD_NAME = "getStaticPart";
46     protected static final String JavaDoc GET_STATIC_PART_METHOD_SIGNATURE = "()Ljava/lang/reflect/AccessibleObject;";
47     protected static final String JavaDoc GET_PARAMETER_VALUES_METHOD_NAME = "getParameterValues";
48     protected static final String JavaDoc GET_ARGUMENTS_METHOD_SIGNATURE = "()[Ljava/lang/Object;";
49     protected static final String JavaDoc GET_ARGUMENTS_METHOD_NAME = "getArguments";
50
51
52     /**
53      * Returns the aspect model type, which is an id for the the special aspect model, can be anything as long
54      * as it is unique.
55      *
56      * @return the aspect model type id
57      */

58     public String JavaDoc getAspectModelType() {
59         return ASPECT_MODEL_TYPE;
60     }
61
62     /**
63      * Defines the aspect.
64      *
65      * @param classInfo
66      * @param aspectDef
67      * @param loader
68      */

69     public void defineAspect(final ClassInfo classInfo,
70                              final AspectDefinition aspectDef,
71                              final ClassLoader JavaDoc loader) {
72         ClassInfo[] interfaces = classInfo.getInterfaces();
73         for (int i = 0; i < interfaces.length; i++) {
74             ClassInfo anInterface = interfaces[i];
75             if (anInterface.getName().equals(MethodInterceptor.class.getName()) ||
76                 anInterface.getName().equals(ConstructorInterceptor.class.getName())) {
77                 aspectDef.setAspectModel(ASPECT_MODEL_TYPE);
78                 aspectDef.setContainerClassName(ASPECT_CONTAINER_CLASS_NAME);
79                 return;
80             }
81         }
82     }
83
84     /**
85      * AOP Alliance is a reflection based model and therefore in need of RTTI info: returns true.
86      *
87      * @return true
88      */

89     public boolean requiresReflectiveInfo() {
90         return true;
91     }
92
93     /**
94      * Returns info about the closure class, name and type (interface or class).
95      *
96      * @return the closure class info
97      */

98     public AroundClosureClassInfo getAroundClosureClassInfo() {
99         return new AspectModel.AroundClosureClassInfo(null, new String JavaDoc[] {AOP_ALLIANCE_CLOSURE_CLASS_NAME});
100     }
101
102
103     /**
104      * Creates the methods required to implement or extend to implement the closure for the specific
105      * aspect model type.
106      *
107      * @param cw
108      * @param className
109      */

110     public void createMandatoryMethods(final ClassWriter cw, final String JavaDoc className) {
111         CodeVisitor cv;
112
113         // invoke
114
{
115             cv = cw.visitMethod(
116                     ACC_PUBLIC,
117                     AOP_ALLIANCE_CLOSURE_PROCEED_METHOD_NAME,
118                     AOP_ALLIANCE_CLOSURE_PROCEED_METHOD_SIGNATURE,
119                     new String JavaDoc[]{THROWABLE_CLASS_NAME},
120                     null
121             );
122             cv.visitVarInsn(ALOAD, 0);
123             cv.visitMethodInsn(INVOKEVIRTUAL, className, PROCEED_METHOD_NAME, PROCEED_METHOD_SIGNATURE);
124             cv.visitInsn(ARETURN);
125             cv.visitMaxs(0, 0);
126         }
127
128         // getStaticPart
129
{
130             cv = cw.visitMethod(
131                     ACC_PUBLIC,
132                     GET_STATIC_PART_METHOD_NAME,
133                     GET_STATIC_PART_METHOD_SIGNATURE,
134                     null, null
135             );
136             cv.visitFieldInsn(GETSTATIC, className, SIGNATURE_FIELD_NAME, METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE);
137             cv.visitTypeInsn(CHECKCAST, METHOD_SIGNATURE_IMPL_CLASS_NAME);
138             cv.visitMethodInsn(
139                     INVOKEVIRTUAL, METHOD_SIGNATURE_IMPL_CLASS_NAME, GET_METHOD_METHOD_NAME,
140                     GET_METHOD_METHOD_SIGNATURE
141             );
142             cv.visitInsn(ARETURN);
143             cv.visitMaxs(1, 1);
144         }
145
146         // getMethod
147
{
148             cv =
149             cw.visitMethod(
150                     ACC_PUBLIC,
151                     GET_METHOD_METHOD_NAME,
152                     GET_METHOD_METHOD_SIGNATURE,
153                     null, null
154             );
155             cv.visitFieldInsn(GETSTATIC, className, SIGNATURE_FIELD_NAME, METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE);
156             cv.visitTypeInsn(CHECKCAST, METHOD_SIGNATURE_IMPL_CLASS_NAME);
157             cv.visitMethodInsn(
158                     INVOKEVIRTUAL, METHOD_SIGNATURE_IMPL_CLASS_NAME, GET_METHOD_METHOD_NAME,
159                     GET_METHOD_METHOD_SIGNATURE
160             );
161             cv.visitInsn(ARETURN);
162             cv.visitMaxs(1, 1);
163         }
164
165         // getArguments
166
{
167             cv = cw.visitMethod(
168                     ACC_PUBLIC,
169                     GET_ARGUMENTS_METHOD_NAME,
170                     GET_ARGUMENTS_METHOD_SIGNATURE,
171                     null, null
172             );
173             cv.visitVarInsn(ALOAD, 0);
174             cv.visitMethodInsn(INVOKESPECIAL, className, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE);
175             cv.visitTypeInsn(CHECKCAST, METHOD_RTTI_IMPL_CLASS_NAME);
176             cv.visitMethodInsn(
177                     INVOKEVIRTUAL,
178                     METHOD_RTTI_IMPL_CLASS_NAME,
179                     GET_PARAMETER_VALUES_METHOD_NAME,
180                     GET_ARGUMENTS_METHOD_SIGNATURE
181             );
182             cv.visitInsn(ARETURN);
183             cv.visitMaxs(1, 1);
184         }
185
186     }
187
188     /**
189      * Creates an invocation of the around closure class' constructor.
190      *
191      * @param cv
192      */

193     public void createInvocationOfAroundClosureSuperClass(final CodeVisitor cv) {
194     }
195
196     /**
197      * Creates host of the aop alliance aspect instance by invoking aspectOf().
198      *
199      * @param cw
200      * @param aspectInfo
201      * @param joinPointClassName
202      */

203     public void createAspectReferenceField(final ClassWriter cw,
204                                            final AspectInfo aspectInfo,
205                                            final String JavaDoc joinPointClassName) {
206         AbstractJoinPointCompiler.createAspectReferenceField(cw, aspectInfo);
207     }
208
209     /**
210      * Creates instantiation of the aop alliance aspect instance by invoking aspectOf().
211      *
212      * @param cv
213      * @param aspectInfo
214      * @param joinPointClassName
215      */

216     public void createAspectInstantiation(final CodeVisitor cv,
217                                           final AspectInfo aspectInfo,
218                                           final String JavaDoc joinPointClassName) {
219         AbstractJoinPointCompiler.createAspectInstantiation(cv, aspectInfo, joinPointClassName);
220     }
221
222     /**
223      * Handles the arguments to the before around.
224      *
225      * @param cv
226      * @param adviceMethodInfo
227      */

228     public void createAroundAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) {
229     }
230
231     /**
232      * Handles the arguments to the before advice.
233      *
234      * @param cv
235      * @param adviceMethodInfo
236      */

237     public void createBeforeAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) {
238     }
239
240     /**
241      * Handles the arguments to the after advice.
242      *
243      * @param cv
244      * @param adviceMethodInfo
245      */

246     public void createAfterAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) {
247     }
248 }
249
Popular Tags