1 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.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 38 public class SpringAspectModel extends AopAllianceAspectModel { 39 40 protected static final String ASPECT_MODEL_TYPE = "spring"; 41 42 48 public String getAspectModelType() { 49 return ASPECT_MODEL_TYPE; 50 } 51 52 59 public void defineAspect(final ClassInfo classInfo, 60 final AspectDefinition aspectDef, 61 final ClassLoader 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 81 public AroundClosureClassInfo getAroundClosureClassInfo() { 82 return new AspectModel.AroundClosureClassInfo( 83 null, 84 new String []{ 85 AOP_ALLIANCE_CLOSURE_CLASS_NAME, 86 MethodBeforeAdvice.class.getName().replace('.', '/'), 87 AfterReturningAdvice.class.getName().replace('.', '/') 88 } 89 ); 90 } 91 92 93 100 public void createMandatoryMethods(final ClassWriter cw, final String className) { 101 super.createMandatoryMethods(cw, className); 102 } 103 104 110 public void createBeforeAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) { 111 final String 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 159 public void createAfterAdviceArgumentHandling(final CodeVisitor cv, final AdviceMethodInfo adviceMethodInfo) { 160 final String joinPointClassName = adviceMethodInfo.getJoinPointClassName(); 161 final int joinPointIndex = adviceMethodInfo.getJoinPointIndex(); 162 final String 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 |