diff options
Diffstat (limited to 'src/main/javassist/bytecode/CodeIterator.java')
-rw-r--r-- | src/main/javassist/bytecode/CodeIterator.java | 113 |
1 files changed, 62 insertions, 51 deletions
diff --git a/src/main/javassist/bytecode/CodeIterator.java b/src/main/javassist/bytecode/CodeIterator.java index e1edcbb7..b9a23c2c 100644 --- a/src/main/javassist/bytecode/CodeIterator.java +++ b/src/main/javassist/bytecode/CodeIterator.java @@ -17,6 +17,7 @@ package javassist.bytecode; import java.util.ArrayList; +import java.util.List; /** * An iterator for editing a code attribute. @@ -305,8 +306,8 @@ public class CodeIterator implements Opcode { String cname = cp.getMethodrefClassName(mref); if (cname.equals(thisClassName) == (skipThis > 0)) return index; - else - break; + + break; } } } @@ -772,19 +773,15 @@ public class CodeIterator implements Opcode { return index + 6; else return index + 4; // WIDE ... - else { - int index2 = (index & ~3) + 8; - if (opcode == LOOKUPSWITCH) { - int npairs = ByteArray.read32bit(code, index2); - return index2 + npairs * 8 + 4; - } - else if (opcode == TABLESWITCH) { - int low = ByteArray.read32bit(code, index2); - int high = ByteArray.read32bit(code, index2 + 4); - return index2 + (high - low + 1) * 4 + 8; - } - // else - // throw new BadBytecode(opcode); + int index2 = (index & ~3) + 8; + if (opcode == LOOKUPSWITCH) { + int npairs = ByteArray.read32bit(code, index2); + return index2 + npairs * 8 + 4; + } + else if (opcode == TABLESWITCH) { + int low = ByteArray.read32bit(code, index2); + int high = ByteArray.read32bit(code, index2 + 4); + return index2 + (high - low + 1) * 4 + 8; } } catch (IndexOutOfBoundsException e) { @@ -796,7 +793,10 @@ public class CodeIterator implements Opcode { // methods for implementing insertGap(). - static class AlignmentException extends Exception {} + static class AlignmentException extends Exception { + + /** default serialVersionUID */ + private static final long serialVersionUID = 1L;} /** * insertGapCore0() inserts a gap (some NOPs). @@ -1075,7 +1075,7 @@ public class CodeIterator implements Opcode { throws BadBytecode { Pointers pointers = new Pointers(0, 0, 0, etable, ca); - ArrayList jumps = makeJumpList(code, code.length, pointers); + List<Branch> jumps = makeJumpList(code, code.length, pointers); while (ldcs != null) { addLdcW(ldcs, jumps); ldcs = ldcs.next; @@ -1085,12 +1085,12 @@ public class CodeIterator implements Opcode { return r; } - private static void addLdcW(CodeAttribute.LdcEntry ldcs, ArrayList jumps) { + private static void addLdcW(CodeAttribute.LdcEntry ldcs, List<Branch> jumps) { int where = ldcs.where; LdcW ldcw = new LdcW(where, ldcs.index); int s = jumps.size(); for (int i = 0; i < s; i++) - if (where < ((Branch)jumps.get(i)).orgPos) { + if (where < jumps.get(i).orgPos) { jumps.add(i, ldcw); return; } @@ -1119,7 +1119,7 @@ public class CodeIterator implements Opcode { return code; Pointers pointers = new Pointers(currentPos, mark, where, etable, ca); - ArrayList jumps = makeJumpList(code, code.length, pointers); + List<Branch> jumps = makeJumpList(code, code.length, pointers); byte[] r = insertGap2w(code, where, gapLength, exclusive, jumps, pointers); currentPos = pointers.cursor; mark = pointers.mark; @@ -1136,42 +1136,39 @@ public class CodeIterator implements Opcode { } private static byte[] insertGap2w(byte[] code, int where, int gapLength, - boolean exclusive, ArrayList jumps, Pointers ptrs) + boolean exclusive, List<Branch> jumps, Pointers ptrs) throws BadBytecode { - int n = jumps.size(); if (gapLength > 0) { ptrs.shiftPc(where, gapLength, exclusive); - for (int i = 0; i < n; i++) - ((Branch)jumps.get(i)).shift(where, gapLength, exclusive); + for (Branch b:jumps) + b.shift(where, gapLength, exclusive); } boolean unstable = true; do { while (unstable) { unstable = false; - for (int i = 0; i < n; i++) { - Branch b = (Branch)jumps.get(i); + for (Branch b:jumps) { if (b.expanded()) { unstable = true; int p = b.pos; int delta = b.deltaSize(); ptrs.shiftPc(p, delta, false); - for (int j = 0; j < n; j++) - ((Branch)jumps.get(j)).shift(p, delta, false); + for (Branch bb:jumps) + bb.shift(p, delta, false); } } } - for (int i = 0; i < n; i++) { - Branch b = (Branch)jumps.get(i); + for (Branch b:jumps) { int diff = b.gapChanged(); if (diff > 0) { unstable = true; int p = b.pos; ptrs.shiftPc(p, diff, false); - for (int j = 0; j < n; j++) - ((Branch)jumps.get(j)).shift(p, diff, false); + for (Branch bb:jumps) + bb.shift(p, diff, false); } } } while (unstable); @@ -1179,10 +1176,10 @@ public class CodeIterator implements Opcode { return makeExapndedCode(code, jumps, where, gapLength); } - private static ArrayList makeJumpList(byte[] code, int endPos, Pointers ptrs) + private static List<Branch> makeJumpList(byte[] code, int endPos, Pointers ptrs) throws BadBytecode { - ArrayList jumps = new ArrayList(); + List<Branch> jumps = new ArrayList<Branch>(); int nextPos; for (int i = 0; i < endPos; i = nextPos) { nextPos = nextOpcode(code, i); @@ -1240,16 +1237,14 @@ public class CodeIterator implements Opcode { return jumps; } - private static byte[] makeExapndedCode(byte[] code, ArrayList jumps, + private static byte[] makeExapndedCode(byte[] code, List<Branch> jumps, int where, int gapLength) throws BadBytecode { int n = jumps.size(); int size = code.length + gapLength; - for (int i = 0; i < n; i++) { - Branch b = (Branch)jumps.get(i); + for (Branch b:jumps) size += b.deltaSize(); - } byte[] newcode = new byte[size]; int src = 0, dest = 0, bindex = 0; @@ -1257,12 +1252,12 @@ public class CodeIterator implements Opcode { Branch b; int bpos; if (0 < n) { - b = (Branch)jumps.get(0); + b = jumps.get(0); bpos = b.orgPos; } else { b = null; - bpos = len; // src will be never equal to bpos + bpos = len; // src will be never equal to bpos } while (src < len) { @@ -1279,7 +1274,7 @@ public class CodeIterator implements Opcode { src += s; dest += s + b.deltaSize(); if (++bindex < n) { - b = (Branch)jumps.get(bindex); + b = jumps.get(bindex); bpos = b.orgPos; } else { @@ -1341,17 +1336,19 @@ public class CodeIterator implements Opcode { state = true; } + @Override boolean expanded() { if (state) { state = false; return true; } - else - return false; + return false; } + @Override int deltaSize() { return 1; } + @Override int write(int srcPos, byte[] code, int destPos, byte[] newcode) { newcode[destPos] = LDC_W; ByteArray.write16bit(index, newcode, destPos + 1); @@ -1372,6 +1369,7 @@ public class CodeIterator implements Opcode { state = BIT16; } + @Override void shift(int where, int gapLength, boolean exclusive) { offset = shiftOffset(pos, offset, where, gapLength, exclusive); super.shift(where, gapLength, exclusive); @@ -1380,18 +1378,20 @@ public class CodeIterator implements Opcode { state = EXPAND; } + @Override boolean expanded() { if (state == EXPAND) { state = BIT32; return true; } - else - return false; + return false; } + @Override abstract int deltaSize(); abstract void write32(int src, byte[] code, int dest, byte[] newcode); + @Override int write(int src, byte[] code, int dest, byte[] newcode) { if (state == BIT32) write32(src, code, dest, newcode); @@ -1410,10 +1410,12 @@ public class CodeIterator implements Opcode { super(p, off); } + @Override int deltaSize() { return state == BIT32 ? 2 : 0; } + @Override void write32(int src, byte[] code, int dest, byte[] newcode) { newcode[dest] = (byte)(((code[src] & 0xff) == GOTO) ? GOTO_W : JSR_W); ByteArray.write32bit(offset, newcode, dest + 1); @@ -1426,10 +1428,12 @@ public class CodeIterator implements Opcode { super(p, off); } + @Override int deltaSize() { return state == BIT32 ? 5 : 0; } + @Override void write32(int src, byte[] code, int dest, byte[] newcode) { newcode[dest] = (byte)opcode(code[src] & 0xff); newcode[dest + 1] = 0; @@ -1443,12 +1447,9 @@ public class CodeIterator implements Opcode { return IFNONNULL; else if (op == IFNONNULL) return IFNULL; - else { - if (((op - IFEQ) & 1) == 0) - return op + 1; - else - return op - 1; - } + if (((op - IFEQ) & 1) == 0) + return op + 1; + return op - 1; } } @@ -1460,11 +1461,13 @@ public class CodeIterator implements Opcode { offset = off; } + @Override void shift(int where, int gapLength, boolean exclusive) { offset = shiftOffset(pos, offset, where, gapLength, exclusive); super.shift(where, gapLength, exclusive); } + @Override int write(int src, byte[] code, int dest, byte[] newcode) { newcode[dest] = code[src]; ByteArray.write32bit(offset, newcode, dest + 1); @@ -1485,6 +1488,7 @@ public class CodeIterator implements Opcode { this.pointers = ptrs; } + @Override void shift(int where, int gapLength, boolean exclusive) { int p = pos; defaultByte = shiftOffset(p, defaultByte, where, gapLength, exclusive); @@ -1495,6 +1499,7 @@ public class CodeIterator implements Opcode { super.shift(where, gapLength, exclusive); } + @Override int gapChanged() { int newGap = 3 - (pos & 3); if (newGap > gap) { @@ -1506,10 +1511,12 @@ public class CodeIterator implements Opcode { return 0; } + @Override int deltaSize() { return gap - (3 - (orgPos & 3)); } + @Override int write(int src, byte[] code, int dest, byte[] newcode) throws BadBytecode { int padding = 3 - (pos & 3); int nops = gap - padding; @@ -1562,6 +1569,7 @@ public class CodeIterator implements Opcode { this.high = high; } + @Override int write2(int dest, byte[] newcode) { ByteArray.write32bit(low, newcode, dest); ByteArray.write32bit(high, newcode, dest + 4); @@ -1575,6 +1583,7 @@ public class CodeIterator implements Opcode { return 8 + 4 * n; } + @Override int tableSize() { return 8 + 4 * offsets.length; } } @@ -1586,6 +1595,7 @@ public class CodeIterator implements Opcode { this.matches = matches; } + @Override int write2(int dest, byte[] newcode) { int n = matches.length; ByteArray.write32bit(n, newcode, dest); @@ -1599,6 +1609,7 @@ public class CodeIterator implements Opcode { return 4 + 8 * n; } + @Override int tableSize() { return 4 + 8 * matches.length; } } } |