summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2006-04-09 15:11:11 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2006-04-09 15:11:11 +0000
commiteee86c9995badd1bb12a3b01ceb0189cfa141c95 (patch)
tree748b4110ed615912d0f4c1a63102dd9a6bd22b1c /src/main
parent7b68913ebbbdc6d7f7e48b18e7d7e5ffc6667527 (diff)
downloadjavassist-eee86c9995badd1bb12a3b01ceb0189cfa141c95.tar.gz
javassist-eee86c9995badd1bb12a3b01ceb0189cfa141c95.zip
fixed a bug of replace(String,ExprEditor) in javassist.expr.Expr.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@267 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main')
-rw-r--r--src/main/javassist/bytecode/CodeIterator.java19
-rw-r--r--src/main/javassist/expr/Expr.java9
-rw-r--r--src/main/javassist/expr/ExprEditor.java13
3 files changed, 31 insertions, 10 deletions
diff --git a/src/main/javassist/bytecode/CodeIterator.java b/src/main/javassist/bytecode/CodeIterator.java
index 52b0a4a0..13948c91 100644
--- a/src/main/javassist/bytecode/CodeIterator.java
+++ b/src/main/javassist/bytecode/CodeIterator.java
@@ -18,6 +18,11 @@ package javassist.bytecode;
/**
* An iterator for editing a code attribute.
*
+ * <p>If there are multiple <code>CodeIterator</code>s referring to the
+ * same <code>Code_attribute</code>, then inserting a gap by one
+ * <code>CodeIterator</code> will break the other
+ * <code>CodeIterator</code>.
+ *
* <p>This iterator does not provide <code>remove()</code>.
* If a piece of code in a <code>Code_attribute</code> is unnecessary,
* it should be overwritten with <code>NOP</code>.
@@ -30,7 +35,7 @@ public class CodeIterator implements Opcode {
protected int endPos;
protected int currentPos;
- CodeIterator(CodeAttribute ca) {
+ protected CodeIterator(CodeAttribute ca) {
codeAttr = ca;
bytecode = ca.getCode();
begin();
@@ -468,10 +473,22 @@ public class CodeIterator implements Opcode {
codeAttr.setCode(c);
bytecode = c;
endPos = getCodeLength();
+ updateCursors(pos, length2);
return length2;
}
/**
+ * Is called when a gap is inserted. The default implementation is empty.
+ * A subclass can override this method so that cursors will be updated.
+ *
+ * @param pos the position where a gap is inserted.
+ * @param length the length of the gap.
+ */
+ protected void updateCursors(int pos, int length) {
+ // empty
+ }
+
+ /**
* Copies and inserts the entries in the given exception table
* at the beginning of the exception table in the code attribute
* edited by this object.
diff --git a/src/main/javassist/expr/Expr.java b/src/main/javassist/expr/Expr.java
index c6cf78eb..e2d455f2 100644
--- a/src/main/javassist/expr/Expr.java
+++ b/src/main/javassist/expr/Expr.java
@@ -301,14 +301,13 @@ public abstract class Expr implements Opcode {
codeAttr.setMaxLocals(newLocals);
ExprEditor.LoopContext context
= new ExprEditor.LoopContext(newLocals);
- CodeIterator iterator = codeAttr.iterator();
- iterator.move(currentPos);
- int size = iterator.getCodeLength();
+ int size = oldIterator.getCodeLength();
int endPos = oldIterator.lookAhead();
- if (ed.doit(thisClass, thisMethod, context, iterator, endPos))
+ oldIterator.move(currentPos);
+ if (ed.doit(thisClass, thisMethod, context, oldIterator, endPos))
edited = true;
- oldIterator.move(endPos + iterator.getCodeLength() - size);
+ oldIterator.move(endPos + oldIterator.getCodeLength() - size);
codeAttr.setMaxLocals(orgLocals);
codeAttr.setMaxStack(orgStack);
maxLocals = context.maxLocals;
diff --git a/src/main/javassist/expr/ExprEditor.java b/src/main/javassist/expr/ExprEditor.java
index a713dde3..743b8d2f 100644
--- a/src/main/javassist/expr/ExprEditor.java
+++ b/src/main/javassist/expr/ExprEditor.java
@@ -110,14 +110,19 @@ public class ExprEditor {
* Visits each bytecode in the given range.
*/
boolean doit(CtClass clazz, MethodInfo minfo, LoopContext context,
- CodeIterator iterator, int endPos)
+ CodeIterator iterator, int endPos)
throws CannotCompileException
{
boolean edited = false;
-
- while (iterator.hasNext() && iterator.lookAhead() < endPos)
- if (loopBody(iterator, clazz, minfo, context))
+ while (iterator.hasNext() && iterator.lookAhead() < endPos) {
+ int size = iterator.getCodeLength();
+ if (loopBody(iterator, clazz, minfo, context)) {
edited = true;
+ int size2 = iterator.getCodeLength();
+ if (size != size2) // the body was modified.
+ endPos += size2 - size;
+ }
+ }
return edited;
}