1 22 package org.jboss.aop.instrument; 23 24 import javassist.CannotCompileException; 25 import javassist.ClassPool; 26 import javassist.CtClass; 27 import javassist.CtField; 28 import javassist.CtMethod; 29 import javassist.CtNewMethod; 30 import javassist.CtPrimitiveType; 31 import javassist.Modifier; 32 import javassist.NotFoundException; 33 34 40 public abstract class OptimizedBehaviourInvocations extends OptimizedInvocations 41 { 42 protected static void addSetArguments(ClassPool pool, CtClass invocation, CtClass[] params)throws NotFoundException, CannotCompileException 43 { 44 if (params == null || params.length == 0) return; 45 CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodInvocation"); 46 CtMethod template = methodInvocation.getDeclaredMethod("setArguments"); 47 48 String code = 49 "public void setArguments(java.lang.Object[] args)" + 50 "{ "; 51 code += " arguments = args; "; 52 for (int i = 0; i < params.length; i++) 53 { 54 if (params[i].isPrimitive()) 55 { 56 CtPrimitiveType primitive = (CtPrimitiveType) params[i]; 57 code += " arg" + i + " = ((" + primitive.getWrapperName() + ")args[" + i + "])." + primitive.getGetMethodName() + "(); "; 58 } 59 else 60 { 61 code += " Object warg" + i + " = args[" + i + "]; "; 62 code += " arg" + i + " = (" + params[i].getName() + ")warg" + i + "; "; 63 } 64 } 65 code += "}"; 66 CtMethod setArguments = CtNewMethod.make(code, invocation); 67 setArguments.setModifiers(template.getModifiers()); 68 invocation.addMethod(setArguments); 69 } 70 71 public static void addGetArguments(ClassPool pool, CtClass invocation, CtClass[] params) throws CannotCompileException 72 { 73 addGetArguments(pool, invocation, params, false); 74 } 75 76 public static void addGetArguments(ClassPool pool, CtClass invocation, CtClass[] params, boolean hasMarshalledArguments) throws CannotCompileException 77 { 78 if (params == null || params.length == 0) return; 79 try { 80 CtClass superInvocation = invocation.getSuperclass(); 81 CtMethod template = superInvocation.getDeclaredMethod("getArguments"); 82 83 StringBuffer code = new StringBuffer (); 84 code.append("public Object[] getArguments()"); 85 code.append("{ "); 86 87 if (hasMarshalledArguments) 88 { 89 code.append(" if (super.marshalledArguments != null)"); 90 code.append(" {"); 91 code.append(" Object[] args = super.getArguments();"); 92 code.append(" setArguments(args);"); 93 code.append(" return args;"); 94 code.append(" }"); 95 } 96 97 code.append(" if (arguments != (Object[])null) { return (Object[])arguments; } "); 98 code.append(" arguments = new Object[" + params.length + "]; "); 99 for (int i = 0; i < params.length; i++) 100 { 101 code.append(" arguments[" + i + "] = ($w)arg" + i + "; "); 102 } 103 104 code.append(" return arguments; }"); 105 CtMethod getArguments = CtNewMethod.make(code.toString(), invocation); 106 getArguments.setModifiers(template.getModifiers()); 107 invocation.addMethod(getArguments); 108 } catch (NotFoundException e) { 109 } 111 } 112 113 protected static String setArguments(int length) 114 { 115 return setArguments("invocation", length, 0); 116 } 117 118 protected static String setArguments(String inv, int length, int offset) 119 { 120 StringBuffer sb = new StringBuffer (""); 121 for (int i = 0 ; i < length ; i++) 122 { 123 sb.append(inv + ".arg" + (i) + " = $" + (i + 1 + offset) + "; "); 124 } 125 return sb.toString(); 126 } 127 128 134 public static void addArgumentFieldsToInvocation(CtClass invocation, CtClass[] params)throws CannotCompileException 135 { 136 for (int i = 0 ; i < params.length ; i++) 137 { 138 CtField field = new CtField(params[i], "arg" + i, invocation); 139 field.setModifiers(Modifier.PUBLIC); 140 invocation.addField(field); 141 } 142 } 143 } 144 | Popular Tags |