1 22 package org.jboss.aop.instrument; 23 24 25 import org.jboss.aop.classpool.AOPClassPool; 26 27 import javassist.ClassPool; 28 import javassist.CtClass; 29 import javassist.CtConstructor; 30 import javassist.CtField; 31 import javassist.CtMethod; 32 import javassist.CtNewConstructor; 33 import javassist.CtNewMethod; 34 import javassist.Modifier; 35 36 42 public class OptimizedConstructionInvocations extends 43 OptimizedBehaviourInvocations 44 { 45 46 protected static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params) throws Exception 47 { 48 CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.ConstructionInvocation"); 49 CtMethod template = methodInvocation.getDeclaredMethod("copy"); 50 51 52 String code = 53 "{ " + 54 " " + invocation.getName() + " wrapper = new " + invocation.getName() + "(this.interceptors, this.constructor); " + 55 " wrapper.metadata = this.metadata; " + 56 " wrapper.currentInterceptor = this.currentInterceptor; "; 57 for (int i = 0; i < params.length; i++) 58 { 59 code += " wrapper.arg" + i + " = this.arg" + i + "; "; 60 } 61 code += " return wrapper; }"; 62 63 CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code, invocation); 64 copy.setModifiers(template.getModifiers()); 65 invocation.addMethod(copy); 66 67 } 68 69 75 protected static String getOptimizedInvocationClassName(CtClass declaringClazz, int constructorIndex) 76 { 77 return declaringClazz.getName() + constructorIndex + "OptimizedConstructionInvocation"; 78 } 79 80 protected static String createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index) throws Exception 81 { 82 AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool(); 83 CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructionInvocation"); 84 CtClass untransformable = pool.get("org.jboss.aop.instrument.Untransformable"); 85 86 String className = getOptimizedInvocationClassName(clazz, index); 87 boolean makeInnerClass = !Modifier.isPublic(con.getModifiers()); 88 89 CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz, className, conInvocation); 90 91 CtConstructor template = null; 92 CtConstructor[] tcons = conInvocation.getDeclaredConstructors(); 93 for (int i = 0; i < tcons.length; i++) 94 { 95 if (tcons[i].getParameterTypes().length == 2) 96 { 97 template = tcons[i]; 98 break; 99 } 100 } 101 CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(), template.getExceptionTypes(), invocation); 102 invocation.addConstructor(icon); 103 104 CtClass[] params = con.getParameterTypes(); 105 for (int i = 0; i < params.length; i++) 106 { 107 CtField field = new CtField(params[i], "arg" + i, invocation); 108 field.setModifiers(Modifier.PUBLIC); 109 invocation.addField(field); 110 } 111 112 addGetArguments(pool, invocation, con.getParameterTypes()); 113 addCopy(pool, invocation, con.getParameterTypes()); 114 TransformerCommon.compileOrLoadClass(con.getDeclaringClass(), invocation); 116 117 return invocation.getName(); 119 } 120 121 } 122 | Popular Tags |