1 22 package org.jboss.aop.instrument; 23 24 import javassist.CannotCompileException; 25 import javassist.CtMethod; 26 import javassist.CtNewMethod; 27 import javassist.Modifier; 28 import javassist.NotFoundException; 29 30 import org.jboss.aop.ClassAdvisor; 31 32 33 39 public class OptimizedMethodExecutionTransformer extends MethodExecutionTransformer 40 { 41 42 public OptimizedMethodExecutionTransformer(Instrumentor instrumentor) 43 { 44 super(instrumentor); 45 } 46 47 protected void transformMethod(MethodTransformation trans, boolean wrap)throws CannotCompileException, NotFoundException 48 { 49 String methodInfoField = addMethodInfoField(Modifier.PRIVATE | Modifier.STATIC, trans.getClazz(), trans); 50 String wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(), 51 trans.getOriginalName()); 52 CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null); 53 54 String originalName = trans.getOriginalName(); 56 wmethod.setName(wrappedName); 57 trans.getClazz().addMethod(wmethod); 58 copyAnnotations(trans.getMethod(), wmethod); 59 String optimizedInvocation = OptimizedMethodInvocations.createOptimizedInvocationClass(trans.getInstrumentor(), trans.getClazz(), trans.getMethod()); 60 trans.getMethod().setName(wrappedName); 61 wmethod.setName(originalName); 62 63 trans.setWMethod(wmethod, wrappedName); 64 getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); 66 67 if (wrap) 68 { 69 getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); 71 setWrapperBody(trans, methodInfoField, optimizedInvocation); 73 } 74 } 75 76 protected void doWrap(MethodTransformation trans, String methodInfoFieldName)throws NotFoundException, Exception 77 { 78 String invocationClassName = OptimizedMethodInvocations.getOptimizedInvocationClassName(trans.getClazz(), trans.getWMethod()); 79 invocationClassName = invocationClassName.substring(invocationClassName.lastIndexOf('.') + 1); 80 invocationClassName = trans.getMethod().getDeclaringClass().getName() + "$" + invocationClassName; 81 setWrapperBody(trans, methodInfoFieldName, invocationClassName); 82 } 83 84 protected void setWrapperBody(MethodTransformation trans, String methodInfoField, String optimizedInvocation) throws NotFoundException 85 { 86 boolean isStatic = Modifier.isStatic(trans.getMethod().getModifiers()); 87 String code = null; 88 if (!isStatic) 89 { 90 code = 91 "{ " + 92 " " + methodInfoFromWeakReference("info", methodInfoField) + 93 " org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)_getInstanceAdvisor();" + 94 " org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors();" + 96 " if (interceptors != (Object[])null || (instAdv != null && instAdv.hasInstanceAspects)) " + 97 " { " + 98 " if (instAdv != null) " + 99 " { " + 100 " interceptors = instAdv.getInterceptors(interceptors); " + 101 " } " + 102 " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(info, interceptors); " + 103 " " + OptimizedBehaviourInvocations.setArguments(trans.getMethod().getParameterTypes().length) + 104 " invocation.setTargetObject(this); " + 105 " invocation.typedTargetObject = this; " + 106 " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " + 107 " " + getAopReturnStr(trans.getMethod()) + "invocation.invokeNext(); " + 108 " } " + 109 " else " + 110 " {" + 111 " " + getReturnStr(trans.getMethod()) + " " + trans.getWrappedName() + "($$); " + 112 " }" + 113 "}"; 114 } 115 else 116 { 117 code = 118 "{ " + 119 " " + methodInfoFromWeakReference("info", methodInfoField) + 120 " org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors();" + 121 " if (interceptors != (Object[])null) " + 122 " { " + 123 " " + optimizedInvocation + " invocation = new " + optimizedInvocation + "(info, interceptors); " + 124 " " + OptimizedBehaviourInvocations.setArguments(trans.getMethod().getParameterTypes().length) + 125 " invocation.setAdvisor(" + Instrumentor.HELPER_FIELD_NAME + "); " + 126 " " + getAopReturnStr(trans.getMethod()) + "invocation.invokeNext(); " + 127 " } " + 128 " else " + 129 " {" + 130 " " + getReturnStr(trans.getMethod()) + " " + trans.getWrappedName() + "($$); " + 131 " }" + 132 "}"; 133 } 134 try 135 { 136 trans.setWMethodBody(code); 137 } 138 catch (CannotCompileException e) 139 { 140 e.printStackTrace(); 141 throw new RuntimeException ("code was: " + code + " for method " + trans.getOriginalName()); } 143 } 144 145 } 146 | Popular Tags |