1 15 16 package javassist; 17 18 import javassist.bytecode.*; 19 import javassist.convert.*; 20 21 49 public class CodeConverter { 50 Transformer transformers = null; 51 52 94 public void replaceNew(CtClass newClass, 95 CtClass calledClass, String calledMethod) { 96 transformers = new TransformNew(transformers, newClass.getName(), 97 calledClass.getName(), calledMethod); 98 } 99 100 117 public void redirectFieldAccess(CtField field, 118 CtClass newClass, String newFieldname) { 119 transformers = new TransformFieldAccess(transformers, field, 120 newClass.getName(), 121 newFieldname); 122 } 123 124 157 public void replaceFieldRead(CtField field, 158 CtClass calledClass, String calledMethod) { 159 transformers = new TransformReadField(transformers, field, 160 calledClass.getName(), 161 calledMethod); 162 } 163 164 198 public void replaceFieldWrite(CtField field, 199 CtClass calledClass, String calledMethod) { 200 transformers = new TransformWriteField(transformers, field, 201 calledClass.getName(), 202 calledMethod); 203 } 204 205 220 public void redirectMethodCall(CtMethod origMethod, 221 CtMethod substMethod) 222 throws CannotCompileException 223 { 224 String d1 = origMethod.getMethodInfo2().getDescriptor(); 225 String d2 = substMethod.getMethodInfo2().getDescriptor(); 226 if (!d1.equals(d2)) 227 throw new CannotCompileException("signature mismatch"); 228 229 transformers = new TransformCall(transformers, origMethod, 230 substMethod); 231 } 232 233 267 public void insertBeforeMethod(CtMethod origMethod, 268 CtMethod beforeMethod) 269 throws CannotCompileException 270 { 271 try { 272 transformers = new TransformBefore(transformers, origMethod, 273 beforeMethod); 274 } 275 catch (NotFoundException e) { 276 throw new CannotCompileException(e); 277 } 278 } 279 280 314 public void insertAfterMethod(CtMethod origMethod, 315 CtMethod afterMethod) 316 throws CannotCompileException 317 { 318 try { 319 transformers = new TransformAfter(transformers, origMethod, 320 afterMethod); 321 } 322 catch (NotFoundException e) { 323 throw new CannotCompileException(e); 324 } 325 } 326 327 330 void doit(CtClass clazz, MethodInfo minfo, ConstPool cp) 331 throws CannotCompileException 332 { 333 Transformer t; 334 335 CodeAttribute codeAttr = minfo.getCodeAttribute(); 336 if (codeAttr == null || transformers == null) 337 return; 338 339 for (t = transformers; t != null; t = t.getNext()) 340 t.initialize(cp, codeAttr); 341 342 CodeIterator iterator = codeAttr.iterator(); 343 while (iterator.hasNext()) { 344 try { 345 int pos = iterator.next(); 346 for (t = transformers; t != null; t = t.getNext()) 347 pos = t.transform(clazz, pos, iterator, cp); 348 } 349 catch (BadBytecode e) { 350 throw new CannotCompileException(e); 351 } 352 } 353 354 int locals = 0; 355 for (t = transformers; t != null; t = t.getNext()) { 356 int s = t.extraLocals(); 357 if (s > locals) 358 locals = s; 359 } 360 361 for (t = transformers; t != null; t = t.getNext()) 362 t.clean(); 363 364 codeAttr.setMaxLocals(codeAttr.getMaxLocals() + locals); 365 } 366 } 367 | Popular Tags |