diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2003-12-31 05:56:29 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2003-12-31 05:56:29 +0000 |
commit | 33894b1cae728946d3363b5ad27fcbfcf33c86d1 (patch) | |
tree | 3f430ab69c49cca58ea0114f60bf211d57948303 /src/main/javassist/CtBehavior.java | |
parent | ba0a3e173968737f8683069775e491d3b8764a9d (diff) | |
download | javassist-33894b1cae728946d3363b5ad27fcbfcf33c86d1.tar.gz javassist-33894b1cae728946d3363b5ad27fcbfcf33c86d1.zip |
CtBehavior#insertAt() and support methods have been implemented.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@63 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/CtBehavior.java')
-rw-r--r-- | src/main/javassist/CtBehavior.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/main/javassist/CtBehavior.java b/src/main/javassist/CtBehavior.java index 636a8105..7824dbe1 100644 --- a/src/main/javassist/CtBehavior.java +++ b/src/main/javassist/CtBehavior.java @@ -672,4 +672,82 @@ public abstract class CtBehavior extends CtMember { throw new CannotCompileException(e); } } + + /** + * Inserts bytecode at the specified line in the body. It is equivalent + * to insertAt(lineNum, true, src). + * + * @param lineNum the line number. + * @param src the source code representing the inserted bytecode. + * It must be a single statement or block. + */ + public int insertAt(int lineNum, String src) + throws CannotCompileException + { + return insertAt(lineNum, true, src); + } + + /** + * Inserts bytecode at the specified line in the body. + * + * @param lineNum the line number. + * @param modify if false, this method does not insert the bytecode. + * It instead only returns the line number at which + * the bytecode would be inserted. + * @param src the source code representing the inserted bytecode. + * It must be a single statement or block. + * If modify is false, the value of src can be null. + */ + public int insertAt(int lineNum, boolean modify, String src) + throws CannotCompileException + { + CodeAttribute ca = methodInfo.getCodeAttribute(); + if (ca == null) + throw new CannotCompileException("no method body"); + + LineNumberAttribute ainfo + = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag); + if (ainfo == null) + throw new CannotCompileException("no line number info"); + + LineNumberAttribute.Pc pc = ainfo.toNearPc(lineNum); + lineNum = pc.line; + int index = pc.index; + if (!modify) + return lineNum; + + declaringClass.checkModify(); + CodeIterator iterator = ca.iterator(); + Javac jv = new Javac(declaringClass); + try { + jv.recordParams(getParameterTypes(), + Modifier.isStatic(getModifiers())); + jv.compileStmnt(src); + Bytecode b = jv.getBytecode(); + int stack = b.getMaxStack(); + int locals = b.getMaxLocals(); + + /* We assume that there is no values in the operand stack + * at the position where the bytecode is inserted. + */ + if (stack > ca.getMaxStack()) + ca.setMaxStack(stack); + + if (locals > ca.getMaxLocals()) + ca.setMaxLocals(locals); + + iterator.insert(index, b.get()); + iterator.insert(b.getExceptionTable(), index); + return lineNum; + } + catch (NotFoundException e) { + throw new CannotCompileException(e); + } + catch (CompileError e) { + throw new CannotCompileException(e); + } + catch (BadBytecode e) { + throw new CannotCompileException(e); + } + } } |