1 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 25 public class SpringAspectModel extends AopAllianceAspectModel { 26 27 protected static final String ASPECT_MODEL_TYPE = "spring"; 28 29 private static final String METHOD_INTERCEPTOR_CLASS = "org.aopalliance.intercept.MethodInterceptor"; 30 private static final String AFTER_RETURNING_ADVICE_CLASS = "org.springframework.aop.AfterReturningAdvice"; 31 private static final String METHOD_BEFORE_ADVICE_CLASS = "org.springframework.aop.MethodBeforeAdvice"; 32 private static final String THROWS_ADVICE_CLASS = "org.springframework.aop.ThrowsAdvice"; 33 34 40 public String getAspectModelType() { 41 return ASPECT_MODEL_TYPE; 42 } 43 44 47 public void defineAspect(final ClassInfo classInfo, final AspectDefinition aspectDef, final ClassLoader loader) { 48 ClassInfo[] interfaces = classInfo.getInterfaces(); 49 for (int i = 0; i < interfaces.length; i++) { 50 String 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 67 public AroundClosureClassInfo getAroundClosureClassInfo() { 68 return new AspectModel.AroundClosureClassInfo(null, 69 new String [] { 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 createAfterAdviceArgumentHandling(methodVisitor, adviceMethodInfo, compilerInput.joinPointInstanceIndex); 82 } 83 } 84 85 88 public void createBeforeAdviceArgumentHandling(final MethodVisitor cv, final AdviceMethodInfo adviceMethodInfo, 89 final int joinPointInstanceIndex) { 90 final String 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 113 public void createAfterAdviceArgumentHandling(final MethodVisitor cv, final AdviceMethodInfo adviceMethodInfo, 114 final int joinPointInstanceIndex) { 115 final String joinPointClassName = adviceMethodInfo.getJoinPointClassName(); 116 final int joinPointIndex = joinPointInstanceIndex; 117 final String 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 |