1 22 package org.jboss.aop.instrument; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.net.URI ; 27 import java.net.URL ; 28 29 import org.jboss.aop.classpool.AOPClassPool; 30 import org.jboss.aop.standalone.Compiler; 31 32 import javassist.CannotCompileException; 33 import javassist.ClassPool; 34 import javassist.CtClass; 35 import javassist.CtConstructor; 36 import javassist.CtField; 37 import javassist.CtMethod; 38 import javassist.CtNewConstructor; 39 import javassist.CtNewMethod; 40 import javassist.Modifier; 41 import javassist.NotFoundException; 42 43 49 public class OptimizedConstructorInvocations extends 50 OptimizedBehaviourInvocations 51 { 52 58 protected static String getOptimizedInvocationClassName(CtClass declaringClazz, int constructorIndex) 59 { 60 return declaringClazz.getName() + "_" + constructorIndex + "OptimizedConstructorInvocation"; 61 } 62 63 protected static String createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index) throws NotFoundException, CannotCompileException 64 { 65 AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool(); 66 CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorInvocation"); 67 CtClass untransformable = pool.get("org.jboss.aop.instrument.Untransformable"); 68 69 String className = getOptimizedInvocationClassName(clazz, index); 70 boolean makeInnerClass = !Modifier.isPublic(con.getModifiers()); 71 72 CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz, className, conInvocation); 73 74 CtConstructor template = conInvocation.getDeclaredConstructors()[0]; 75 CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(), template.getExceptionTypes(), invocation); 76 invocation.addConstructor(icon); 77 78 CtClass[] params = con.getParameterTypes(); 79 for (int i = 0; i < params.length; i++) 80 { 81 CtField field = new CtField(params[i], "arg" + i, invocation); 82 field.setModifiers(Modifier.PUBLIC); 83 invocation.addField(field); 84 } 85 86 CtMethod in = conInvocation.getDeclaredMethod("invokeTarget"); 87 88 StringBuffer code = new StringBuffer ("{") ; 89 90 code.append("setTargetObject( new ").append(con.getDeclaringClass().getName()).append("("); 91 for (int i = 0; i < params.length; i++) 92 { 93 if (i > 0) 94 code.append(", "); 95 code.append("arg").append(i); 96 } 97 code.append("));"); 98 code.append("return getTargetObject();"); 99 code.append("}"); 100 101 CtMethod invokeTarget = null; 102 try 103 { 104 invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget", in.getParameterTypes(), in.getExceptionTypes(), code.toString(), invocation); 105 } 106 catch (CannotCompileException e) 107 { 108 System.out.println(code.toString()); 109 throw e; 110 } 111 invocation.addMethod(invokeTarget); 112 invokeTarget.setModifiers(in.getModifiers()); 113 addGetArguments(pool, invocation, con.getParameterTypes()); 114 addCopy(pool, invocation, con.getParameterTypes()); 115 116 TransformerCommon.compileOrLoadClass(clazz, invocation); 117 118 return invocation.getName(); 120 } 121 122 private static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params) throws CannotCompileException, NotFoundException 123 { 124 CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorInvocation"); 125 CtMethod template = methodInvocation.getDeclaredMethod("copy"); 126 127 String code = 128 "{ " + 129 " " + invocation.getName() + " wrapper = new " + invocation.getName() + "(this.interceptors); " + 130 " wrapper.constructor = this.constructor; " + 131 " wrapper.arguments = this.arguments; " + 132 " wrapper.metadata = this.metadata; " + 133 " wrapper.currentInterceptor = this.currentInterceptor; "; 134 for (int i = 0; i < params.length; i++) 135 { 136 code += " wrapper.arg" + i + " = this.arg" + i + "; "; 137 } 138 code += " return wrapper; }"; 139 140 CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code, invocation); 141 copy.setModifiers(template.getModifiers()); 142 invocation.addMethod(copy); 143 144 } 145 146 } 147 | Popular Tags |