1 15 16 package javassist.expr; 17 18 import javassist.*; 19 import javassist.bytecode.*; 20 import javassist.compiler.*; 21 22 25 public class Handler extends Expr { 26 private static String EXCEPTION_NAME = "$1"; 27 private ExceptionTable etable; 28 private int index; 29 30 33 protected Handler(ExceptionTable et, int nth, 34 CodeIterator it, CtClass declaring, MethodInfo m) { 35 super(et.handlerPc(nth), it, declaring, m); 36 etable = et; 37 index = nth; 38 } 39 40 43 public CtBehavior where() { return super.where(); } 44 45 50 public int getLineNumber() { 51 return super.getLineNumber(); 52 } 53 54 59 public String getFileName() { 60 return super.getFileName(); 61 } 62 63 66 public CtClass[] mayThrow() { 67 return super.mayThrow(); 68 } 69 70 73 public CtClass getType() throws NotFoundException { 74 ConstPool cp = getConstPool(); 75 String name = cp.getClassInfo(etable.catchType(index)); 76 return Descriptor.toCtClass(name, thisClass.getClassPool()); 77 } 78 79 84 public void replace(String statement) throws CannotCompileException { 85 throw new RuntimeException ("not implemented yet"); 86 } 87 88 95 public void insertBefore(String src) throws CannotCompileException { 96 edited = true; 97 98 ConstPool cp = getConstPool(); 99 CodeAttribute ca = iterator.get(); 100 Javac jv = new Javac(thisClass); 101 Bytecode b = jv.getBytecode(); 102 b.setStackDepth(1); 103 b.setMaxLocals(ca.getMaxLocals()); 104 105 try { 106 CtClass type = getType(); 107 int var = jv.recordVariable(type, EXCEPTION_NAME); 108 jv.recordReturnType(type, false); 109 b.addAstore(var); 110 jv.compileStmnt(src); 111 b.addAload(var); 112 113 int oldHandler = etable.handlerPc(index); 114 b.addOpcode(Opcode.GOTO); 115 b.addIndex(oldHandler - iterator.getCodeLength() 116 - b.currentPc() + 1); 117 118 maxStack = b.getMaxStack(); 119 maxLocals = b.getMaxLocals(); 120 121 int pos = iterator.append(b.get()); 122 iterator.append(b.getExceptionTable(), pos); 123 etable.setHandlerPc(index, pos); 124 } 125 catch (NotFoundException e) { 126 throw new CannotCompileException(e); 127 } 128 catch (CompileError e) { 129 throw new CannotCompileException(e); 130 } 131 } 132 } 133 | Popular Tags |