KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > transform > inlining > model > SpringAspectModel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.transform.inlining.model;
5
6 import com.tc.asm.MethodVisitor;
7 import com.tc.asm.Type;
8
9 import com.tc.aspectwerkz.aspect.AdviceType;
10 import com.tc.aspectwerkz.definition.AspectDefinition;
11 import com.tc.aspectwerkz.transform.inlining.AdviceMethodInfo;
12 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
13 import com.tc.aspectwerkz.transform.inlining.compiler.CompilerInput;
14 import com.tc.aspectwerkz.transform.inlining.spi.AspectModel;
15 import com.tc.aspectwerkz.reflect.ClassInfo;
16
17 /**
18  * Implementation of the AspectModel interface for Spring framework.
19  * <p/>
20  * Provides methods for definition of aspects and framework specific bytecode generation
21  * used by the join point compiler.
22  *
23  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bon&#233;r </a>
24  */

25 public class SpringAspectModel extends AopAllianceAspectModel {
26
27   protected static final String JavaDoc ASPECT_MODEL_TYPE = "spring";
28
29   private static final String JavaDoc METHOD_INTERCEPTOR_CLASS = "org.aopalliance.intercept.MethodInterceptor";
30   private static final String JavaDoc AFTER_RETURNING_ADVICE_CLASS = "org.springframework.aop.AfterReturningAdvice";
31   private static final String JavaDoc METHOD_BEFORE_ADVICE_CLASS = "org.springframework.aop.MethodBeforeAdvice";
32   private static final String JavaDoc THROWS_ADVICE_CLASS = "org.springframework.aop.ThrowsAdvice";
33
34   /**
35    * Returns the aspect model type, which is an id for the the special aspect model, can be anything as long
36    * as it is unique.
37    *
38    * @return the aspect model type id
39    */

40   public String JavaDoc getAspectModelType() {
41     return ASPECT_MODEL_TYPE;
42   }
43
44   /**
45    * Defines the aspect.
46    */

47   public void defineAspect(final ClassInfo classInfo, final AspectDefinition aspectDef, final ClassLoader JavaDoc loader) {
48     ClassInfo[] interfaces = classInfo.getInterfaces();
49     for (int i = 0; i < interfaces.length; i++) {
50       String JavaDoc name = interfaces[i].getName();
51       if (METHOD_INTERCEPTOR_CLASS.equals(name)
52           || METHOD_BEFORE_ADVICE_CLASS.equals(name)
53           || AFTER_RETURNING_ADVICE_CLASS.equals(name)
54           || THROWS_ADVICE_CLASS.equals(name)) {
55         aspectDef.setAspectModel(ASPECT_MODEL_TYPE);
56         aspectDef.setContainerClassName(null);
57         return;
58       }
59     }
60   }
61
62   /**
63    * Returns info about the closure class, name and type (interface or class).
64    *
65    * @return the closure class info
66    */

67   public AroundClosureClassInfo getAroundClosureClassInfo() {
68     return new AspectModel.AroundClosureClassInfo(null,
69         new String JavaDoc[] {
70           AOP_ALLIANCE_CLOSURE_CLASS_NAME,
71           METHOD_BEFORE_ADVICE_CLASS.replace('.', '/'),
72           AFTER_RETURNING_ADVICE_CLASS.replace('.', '/') });
73   }
74
75   public void createBeforeOrAfterAdviceArgumentHandling(MethodVisitor methodVisitor, CompilerInput compilerInput,
76       Type[] types, AdviceMethodInfo adviceMethodInfo, int i) {
77     if (AdviceType.BEFORE.equals(adviceMethodInfo.getAdviceInfo().getType())) {
78       createBeforeAdviceArgumentHandling(methodVisitor, adviceMethodInfo, compilerInput.joinPointInstanceIndex);
79     } else {
80       // after advice no matter what
81
createAfterAdviceArgumentHandling(methodVisitor, adviceMethodInfo, compilerInput.joinPointInstanceIndex);
82     }
83   }
84
85   /**
86    * Handles the arguments to the before advice.
87    */

88   public void createBeforeAdviceArgumentHandling(final MethodVisitor cv, final AdviceMethodInfo adviceMethodInfo,
89       final int joinPointInstanceIndex) {
90     final String JavaDoc joinPointClassName = adviceMethodInfo.getJoinPointClassName();
91     final int joinPointIndex = joinPointInstanceIndex;
92     cv.visitFieldInsn(GETSTATIC, joinPointClassName, SIGNATURE_FIELD_NAME, METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE);
93     cv.visitMethodInsn(INVOKEVIRTUAL, METHOD_SIGNATURE_IMPL_CLASS_NAME, GET_METHOD_METHOD_NAME,
94         GET_METHOD_METHOD_SIGNATURE);
95
96     if (Type.getArgumentTypes(adviceMethodInfo.getCalleeMemberDesc()).length == 0) {
97       cv.visitInsn(ACONST_NULL);
98     } else {
99       cv.visitVarInsn(ALOAD, joinPointIndex);
100       cv.visitMethodInsn(INVOKEVIRTUAL, joinPointClassName, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE);
101       cv.visitTypeInsn(CHECKCAST, METHOD_RTTI_IMPL_CLASS_NAME);
102       cv.visitMethodInsn(INVOKEVIRTUAL, METHOD_RTTI_IMPL_CLASS_NAME, GET_PARAMETER_VALUES_METHOD_NAME,
103           GET_ARGUMENTS_METHOD_SIGNATURE);
104     }
105     cv.visitVarInsn(ALOAD, joinPointIndex);
106     cv.visitFieldInsn(GETFIELD, joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, adviceMethodInfo
107         .getCalleeClassSignature());
108   }
109
110   /**
111    * Handles the arguments to the after advice.
112    */

113   public void createAfterAdviceArgumentHandling(final MethodVisitor cv, final AdviceMethodInfo adviceMethodInfo,
114       final int joinPointInstanceIndex) {
115     final String JavaDoc joinPointClassName = adviceMethodInfo.getJoinPointClassName();
116     final int joinPointIndex = joinPointInstanceIndex;
117     final String JavaDoc specArgDesc = adviceMethodInfo.getSpecialArgumentTypeDesc();
118     if (specArgDesc == null) {
119       cv.visitInsn(ACONST_NULL);
120     } else {
121       cv.visitVarInsn(ALOAD, adviceMethodInfo.getSpecialArgumentIndex());
122       AsmHelper.wrapPrimitiveType(cv, Type.getType(specArgDesc));
123     }
124     cv.visitFieldInsn(GETSTATIC, joinPointClassName, SIGNATURE_FIELD_NAME, METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE);
125     cv.visitMethodInsn(INVOKEVIRTUAL, METHOD_SIGNATURE_IMPL_CLASS_NAME, GET_METHOD_METHOD_NAME,
126         GET_METHOD_METHOD_SIGNATURE);
127
128     if (Type.getArgumentTypes(adviceMethodInfo.getCalleeMemberDesc()).length == 0) {
129       cv.visitInsn(ACONST_NULL);
130     } else {
131       cv.visitVarInsn(ALOAD, joinPointIndex);
132       cv.visitMethodInsn(INVOKEVIRTUAL, joinPointClassName, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE);
133       cv.visitTypeInsn(CHECKCAST, METHOD_RTTI_IMPL_CLASS_NAME);
134       cv.visitMethodInsn(INVOKEVIRTUAL, METHOD_RTTI_IMPL_CLASS_NAME, GET_PARAMETER_VALUES_METHOD_NAME,
135           GET_ARGUMENTS_METHOD_SIGNATURE);
136     }
137
138     cv.visitVarInsn(ALOAD, joinPointIndex);
139     cv.visitFieldInsn(GETFIELD, joinPointClassName, CALLEE_INSTANCE_FIELD_NAME, adviceMethodInfo
140         .getCalleeClassSignature());
141   }
142 }
143
Popular Tags