1 29 30 package com.caucho.aop; 31 32 import java.io.IOException; 33 34 import com.caucho.bytecode.JClass; 35 import com.caucho.bytecode.JMethod; 36 37 import com.caucho.util.L10N; 38 39 import com.caucho.java.JavaWriter; 40 41 import com.caucho.java.gen.BaseMethod; 42 43 46 public class AopMethod extends BaseMethod { 47 private static final L10N L = new L10N(AopMethod.class); 48 49 private String _fieldName; 50 51 54 public AopMethod(JMethod apiMethod, JMethod implMethod) 55 { 56 super(apiMethod, implMethod); 57 } 58 59 64 public void generate(JavaWriter out) 65 throws IOException 66 { 67 _fieldName = "__caucho_aop_" + out.generateId(); 68 69 generateInterceptors(out); 70 71 super.generate(out); 72 } 73 74 private void generateInterceptors(JavaWriter out) 75 throws IOException 76 { 77 out.println(); 78 out.println("private static class " + _fieldName + " extends com.caucho.aop.MethodInvocationImpl {"); 79 out.pushDepth(); 80 out.print("private static java.lang.reflect.Method _method = getMethod("); 81 out.print(getMethod().getDeclaringClass().getName() + ".class"); 82 83 out.print(", \"" + getMethodName() + "\", new Class[] {"); 84 JClass []types = getParameterTypes(); 85 for (int i = 0; i < types.length; i++) { 86 if (i != 0) 87 out.print(", "); 88 out.print(types[i].getPrintName()); 89 out.print(".class"); 90 } 91 out.println("});"); 92 out.print("private static java.lang.reflect.Method _superMethod = getMethod("); 93 out.print(getMethod().getDeclaringClass().getName() + ".class"); 94 95 out.print(", \"" + getMethodName() + "__super\", new Class[] {"); 96 for (int i = 0; i < types.length; i++) { 97 if (i != 0) 98 out.print(", "); 99 out.print(types[i].getPrintName()); 100 out.print(".class"); 101 } 102 out.println("});"); 103 out.println(); 104 out.println("static final org.aopalliance.intercept.MethodInterceptor _interceptor = com.caucho.aop.MethodInterceptorBuilder.create(_method);"); 105 106 out.println(); 107 out.print(_fieldName + "("); 108 109 out.print("Object t"); 110 111 JClass []parameterTypes = getParameterTypes(); 112 for (int i = 0; i < parameterTypes.length; i++) { 113 out.print(", "); 114 115 out.print(parameterTypes[i].getPrintName()); 116 117 out.print(" a" + i); 118 } 119 120 out.println(")"); 121 out.println("{"); 122 out.pushDepth(); 123 124 out.print("super(t, new Object[] { "); 125 126 for (int i = 0; i < parameterTypes.length; i++) { 127 if (i != 0) 128 out.print(", "); 129 130 JClass type = parameterTypes[i]; 131 132 if ("boolean".equals(type.getName())) 133 out.print("new Boolean(a" + i + ")"); 134 else if ("byte".equals(type.getName())) 135 out.print("new Byte(a" + i + ")"); 136 else if ("short".equals(type.getName())) 137 out.print("new Short(a" + i + ")"); 138 else if ("int".equals(type.getName())) 139 out.print("new Integer(a" + i + ")"); 140 else if ("long".equals(type.getName())) 141 out.print("new Long(a" + i + ")"); 142 else if ("float".equals(type.getName())) 143 out.print("new Float(a" + i + ")"); 144 else if ("double".equals(type.getName())) 145 out.print("new Double(a" + i + ")"); 146 else if ("char".equals(type.getName())) 147 out.print("new Character(a" + i + ")"); 148 else 149 out.print("a" + i); 150 } 151 out.println("});"); 152 153 out.popDepth(); 154 out.println("}"); 155 156 out.println(); 157 out.println("public java.lang.reflect.Method getMethod() { return _method; }"); 158 out.println("protected java.lang.reflect.Method getSuperMethod() { return _superMethod; }"); 159 160 out.popDepth(); 161 out.println("}"); 162 } 163 164 170 protected void generateCall(JavaWriter out, String []args) 171 throws IOException 172 { 173 super.generateCall(out, args); 174 } 175 } 176 | Popular Tags |