1 15 16 package javassist; 17 18 import javassist.bytecode.*; 19 import javassist.compiler.Javac; 20 import javassist.compiler.CompileError; 21 import javassist.CtMethod.ConstParameter; 22 23 29 public class CtNewMethod { 30 31 41 public static CtMethod make(String src, CtClass declaring) 42 throws CannotCompileException 43 { 44 return make(src, declaring, null, null); 45 } 46 47 64 public static CtMethod make(String src, CtClass declaring, 65 String delegateObj, String delegateMethod) 66 throws CannotCompileException 67 { 68 Javac compiler = new Javac(declaring); 69 try { 70 if (delegateMethod != null) 71 compiler.recordProceed(delegateObj, delegateMethod); 72 73 CtMember obj = compiler.compile(src); 74 if (obj instanceof CtMethod) 75 return (CtMethod)obj; 76 } 77 catch (CompileError e) { 78 throw new CannotCompileException(e); 79 } 80 81 throw new CannotCompileException("not a method"); 82 } 83 84 98 public static CtMethod make(CtClass returnType, 99 String mname, CtClass[] parameters, 100 CtClass[] exceptions, 101 String body, CtClass declaring) 102 throws CannotCompileException 103 { 104 return make(Modifier.PUBLIC, returnType, mname, parameters, exceptions, 105 body, declaring); 106 } 107 108 124 public static CtMethod make(int modifiers, CtClass returnType, 125 String mname, CtClass[] parameters, 126 CtClass[] exceptions, 127 String body, CtClass declaring) 128 throws CannotCompileException 129 { 130 try { 131 CtMethod cm 132 = new CtMethod(returnType, mname, parameters, declaring); 133 cm.setModifiers(modifiers); 134 cm.setExceptionTypes(exceptions); 135 cm.setBody(body); 136 return cm; 137 } 138 catch (NotFoundException e) { 139 throw new CannotCompileException(e); 140 } 141 } 142 143 155 public static CtMethod copy(CtMethod src, CtClass declaring, 156 ClassMap map) throws CannotCompileException { 157 return new CtMethod(src, declaring, map); 158 } 159 160 174 public static CtMethod copy(CtMethod src, String name, CtClass declaring, 175 ClassMap map) throws CannotCompileException { 176 CtMethod cm = new CtMethod(src, declaring, map); 177 cm.setName(name); 178 return cm; 179 } 180 181 192 public static CtMethod abstractMethod(CtClass returnType, 193 String mname, 194 CtClass[] parameters, 195 CtClass[] exceptions, 196 CtClass declaring) 197 throws NotFoundException 198 { 199 CtMethod cm = new CtMethod(returnType, mname, parameters, declaring); 200 cm.setExceptionTypes(exceptions); 201 return cm; 202 } 203 204 213 public static CtMethod getter(String methodName, CtField field) 214 throws CannotCompileException 215 { 216 FieldInfo finfo = field.getFieldInfo2(); 217 String fieldType = finfo.getDescriptor(); 218 String desc = "()" + fieldType; 219 ConstPool cp = finfo.getConstPool(); 220 MethodInfo minfo = new MethodInfo(cp, methodName, desc); 221 minfo.setAccessFlags(AccessFlag.PUBLIC); 222 223 Bytecode code = new Bytecode(cp, 2, 1); 224 try { 225 String fieldName = finfo.getName(); 226 if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { 227 code.addAload(0); 228 code.addGetfield(Bytecode.THIS, fieldName, fieldType); 229 } 230 else 231 code.addGetstatic(Bytecode.THIS, fieldName, fieldType); 232 233 code.addReturn(field.getType()); 234 } 235 catch (NotFoundException e) { 236 throw new CannotCompileException(e); 237 } 238 239 minfo.setCodeAttribute(code.toCodeAttribute()); 240 return new CtMethod(minfo, field.getDeclaringClass()); 241 } 242 243 254 public static CtMethod setter(String methodName, CtField field) 255 throws CannotCompileException 256 { 257 FieldInfo finfo = field.getFieldInfo2(); 258 String fieldType = finfo.getDescriptor(); 259 String desc = "(" + fieldType + ")V"; 260 ConstPool cp = finfo.getConstPool(); 261 MethodInfo minfo = new MethodInfo(cp, methodName, desc); 262 minfo.setAccessFlags(AccessFlag.PUBLIC); 263 264 Bytecode code = new Bytecode(cp, 3, 3); 265 try { 266 String fieldName = finfo.getName(); 267 if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { 268 code.addAload(0); 269 code.addLoad(1, field.getType()); 270 code.addPutfield(Bytecode.THIS, fieldName, fieldType); 271 } 272 else { 273 code.addLoad(1, field.getType()); 274 code.addPutstatic(Bytecode.THIS, fieldName, fieldType); 275 } 276 277 code.addReturn(null); 278 } 279 catch (NotFoundException e) { 280 throw new CannotCompileException(e); 281 } 282 283 minfo.setCodeAttribute(code.toCodeAttribute()); 284 return new CtMethod(minfo, field.getDeclaringClass()); 285 } 286 287 308 public static CtMethod delegator(CtMethod delegate, CtClass declaring) 309 throws CannotCompileException 310 { 311 try { 312 return delegator0(delegate, declaring); 313 } 314 catch (NotFoundException e) { 315 throw new CannotCompileException(e); 316 } 317 } 318 319 private static CtMethod delegator0(CtMethod delegate, CtClass declaring) 320 throws CannotCompileException, NotFoundException 321 { 322 MethodInfo deleInfo = delegate.getMethodInfo2(); 323 String methodName = deleInfo.getName(); 324 String desc = deleInfo.getDescriptor(); 325 ConstPool cp = declaring.getClassFile2().getConstPool(); 326 MethodInfo minfo = new MethodInfo(cp, methodName, desc); 327 minfo.setAccessFlags(deleInfo.getAccessFlags()); 328 329 ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute(); 330 if (eattr != null) 331 minfo.setExceptionsAttribute( 332 (ExceptionsAttribute)eattr.copy(cp, null)); 333 334 Bytecode code = new Bytecode(cp, 0, 0); 335 boolean isStatic = Modifier.isStatic(delegate.getModifiers()); 336 CtClass deleClass = delegate.getDeclaringClass(); 337 CtClass[] params = delegate.getParameterTypes(); 338 int s; 339 if (isStatic) { 340 s = code.addLoadParameters(params, 0); 341 code.addInvokestatic(deleClass, methodName, desc); 342 } 343 else { 344 code.addLoad(0, deleClass); 345 s = code.addLoadParameters(params, 1); 346 code.addInvokespecial(deleClass, methodName, desc); 347 } 348 349 code.addReturn(delegate.getReturnType()); 350 code.setMaxLocals(++s); 351 code.setMaxStack(s < 2 ? 2 : s); minfo.setCodeAttribute(code.toCodeAttribute()); 353 return new CtMethod(minfo, declaring); 354 } 355 356 451 public static CtMethod wrapped(CtClass returnType, 452 String mname, 453 CtClass[] parameterTypes, 454 CtClass[] exceptionTypes, 455 CtMethod body, ConstParameter constParam, 456 CtClass declaring) 457 throws CannotCompileException 458 { 459 return CtNewWrappedMethod.wrapped(returnType, mname, parameterTypes, 460 exceptionTypes, body, constParam, declaring); 461 } 462 } 463 | Popular Tags |