diff options
Diffstat (limited to 'src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java | 121 |
1 files changed, 46 insertions, 75 deletions
diff --git a/src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java b/src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java index aadbeb2..fc2bac3 100644 --- a/src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java +++ b/src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java | |||
| @@ -18,67 +18,53 @@ import javassist.bytecode.CodeAttribute; | |||
| 18 | import javassist.bytecode.CodeIterator; | 18 | import javassist.bytecode.CodeIterator; |
| 19 | import javassist.bytecode.Opcode; | 19 | import javassist.bytecode.Opcode; |
| 20 | 20 | ||
| 21 | public class BytecodeIndexIterator implements Iterator<BytecodeIndexIterator.Index> | 21 | public class BytecodeIndexIterator implements Iterator<BytecodeIndexIterator.Index> { |
| 22 | { | 22 | |
| 23 | public static class Index | 23 | public static class Index { |
| 24 | { | 24 | |
| 25 | private CodeIterator m_iter; | 25 | private CodeIterator m_iter; |
| 26 | private int m_pos; | 26 | private int m_pos; |
| 27 | private boolean m_isWide; | 27 | private boolean m_isWide; |
| 28 | 28 | ||
| 29 | protected Index( CodeIterator iter, int pos, boolean isWide ) | 29 | protected Index(CodeIterator iter, int pos, boolean isWide) { |
| 30 | { | ||
| 31 | m_iter = iter; | 30 | m_iter = iter; |
| 32 | m_pos = pos; | 31 | m_pos = pos; |
| 33 | m_isWide = isWide; | 32 | m_isWide = isWide; |
| 34 | } | 33 | } |
| 35 | 34 | ||
| 36 | public int getIndex( ) | 35 | public int getIndex() { |
| 37 | { | 36 | if (m_isWide) { |
| 38 | if( m_isWide ) | 37 | return m_iter.s16bitAt(m_pos); |
| 39 | { | 38 | } else { |
| 40 | return m_iter.s16bitAt( m_pos ); | 39 | return m_iter.byteAt(m_pos); |
| 41 | } | ||
| 42 | else | ||
| 43 | { | ||
| 44 | return m_iter.byteAt( m_pos ); | ||
| 45 | } | 40 | } |
| 46 | } | 41 | } |
| 47 | 42 | ||
| 48 | public void setIndex( int val ) | 43 | public void setIndex(int val) throws BadBytecode { |
| 49 | throws BadBytecode | 44 | if (m_isWide) { |
| 50 | { | 45 | m_iter.write16bit(val, m_pos); |
| 51 | if( m_isWide ) | 46 | } else { |
| 52 | { | 47 | if (val < 256) { |
| 53 | m_iter.write16bit( val, m_pos ); | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | if( val < 256 ) | ||
| 58 | { | ||
| 59 | // we can write the byte | 48 | // we can write the byte |
| 60 | m_iter.writeByte( val, m_pos ); | 49 | m_iter.writeByte(val, m_pos); |
| 61 | } | 50 | } else { |
| 62 | else | ||
| 63 | { | ||
| 64 | // we need to upgrade this instruction to LDC_W | 51 | // we need to upgrade this instruction to LDC_W |
| 65 | assert( m_iter.byteAt( m_pos - 1 ) == Opcode.LDC ); | 52 | assert (m_iter.byteAt(m_pos - 1) == Opcode.LDC); |
| 66 | m_iter.insertGap( m_pos - 1, 1 ); | 53 | m_iter.insertGap(m_pos - 1, 1); |
| 67 | m_iter.writeByte( Opcode.LDC_W, m_pos - 1 ); | 54 | m_iter.writeByte(Opcode.LDC_W, m_pos - 1); |
| 68 | m_iter.write16bit( val, m_pos ); | 55 | m_iter.write16bit(val, m_pos); |
| 69 | m_isWide = true; | 56 | m_isWide = true; |
| 70 | 57 | ||
| 71 | // move the iterator to the next opcode | 58 | // move the iterator to the next opcode |
| 72 | m_iter.move( m_pos + 2 ); | 59 | m_iter.move(m_pos + 2); |
| 73 | } | 60 | } |
| 74 | } | 61 | } |
| 75 | 62 | ||
| 76 | // sanity check | 63 | // sanity check |
| 77 | assert( val == getIndex() ); | 64 | assert (val == getIndex()); |
| 78 | } | 65 | } |
| 79 | 66 | ||
| 80 | public boolean isValid( Bytecode bytecode ) | 67 | public boolean isValid(Bytecode bytecode) { |
| 81 | { | ||
| 82 | return getIndex() >= 0 && getIndex() < bytecode.getConstPool().getSize(); | 68 | return getIndex() >= 0 && getIndex() < bytecode.getConstPool().getSize(); |
| 83 | } | 69 | } |
| 84 | } | 70 | } |
| @@ -88,9 +74,7 @@ public class BytecodeIndexIterator implements Iterator<BytecodeIndexIterator.Ind | |||
| 88 | private CodeIterator m_iter; | 74 | private CodeIterator m_iter; |
| 89 | private Index m_next; | 75 | private Index m_next; |
| 90 | 76 | ||
| 91 | public BytecodeIndexIterator( Bytecode bytecode ) | 77 | public BytecodeIndexIterator(Bytecode bytecode) throws BadBytecode { |
| 92 | throws BadBytecode | ||
| 93 | { | ||
| 94 | m_bytecode = bytecode; | 78 | m_bytecode = bytecode; |
| 95 | m_attribute = bytecode.toCodeAttribute(); | 79 | m_attribute = bytecode.toCodeAttribute(); |
| 96 | m_iter = m_attribute.iterator(); | 80 | m_iter = m_attribute.iterator(); |
| @@ -99,41 +83,32 @@ public class BytecodeIndexIterator implements Iterator<BytecodeIndexIterator.Ind | |||
| 99 | } | 83 | } |
| 100 | 84 | ||
| 101 | @Override | 85 | @Override |
| 102 | public boolean hasNext( ) | 86 | public boolean hasNext() { |
| 103 | { | ||
| 104 | return m_next != null; | 87 | return m_next != null; |
| 105 | } | 88 | } |
| 106 | 89 | ||
| 107 | @Override | 90 | @Override |
| 108 | public Index next( ) | 91 | public Index next() { |
| 109 | { | ||
| 110 | Index out = m_next; | 92 | Index out = m_next; |
| 111 | try | 93 | try { |
| 112 | { | ||
| 113 | m_next = getNext(); | 94 | m_next = getNext(); |
| 114 | } | 95 | } catch (BadBytecode ex) { |
| 115 | catch( BadBytecode ex ) | 96 | throw new Error(ex); |
| 116 | { | ||
| 117 | throw new Error( ex ); | ||
| 118 | } | 97 | } |
| 119 | return out; | 98 | return out; |
| 120 | } | 99 | } |
| 121 | 100 | ||
| 122 | @Override | 101 | @Override |
| 123 | public void remove( ) | 102 | public void remove() { |
| 124 | { | ||
| 125 | throw new UnsupportedOperationException(); | 103 | throw new UnsupportedOperationException(); |
| 126 | } | 104 | } |
| 127 | 105 | ||
| 128 | private Index getNext( ) | 106 | private Index getNext() throws BadBytecode { |
| 129 | throws BadBytecode | 107 | while (m_iter.hasNext()) { |
| 130 | { | ||
| 131 | while( m_iter.hasNext() ) | ||
| 132 | { | ||
| 133 | int pos = m_iter.next(); | 108 | int pos = m_iter.next(); |
| 134 | int opcode = m_iter.byteAt( pos ); | 109 | int opcode = m_iter.byteAt(pos); |
| 135 | switch( opcode ) | 110 | switch (opcode) { |
| 136 | { | 111 | |
| 137 | // for only these opcodes, the next two bytes are a const pool reference | 112 | // for only these opcodes, the next two bytes are a const pool reference |
| 138 | case Opcode.ANEWARRAY: | 113 | case Opcode.ANEWARRAY: |
| 139 | case Opcode.CHECKCAST: | 114 | case Opcode.CHECKCAST: |
| @@ -151,30 +126,26 @@ public class BytecodeIndexIterator implements Iterator<BytecodeIndexIterator.Ind | |||
| 151 | case Opcode.PUTSTATIC: | 126 | case Opcode.PUTSTATIC: |
| 152 | case Opcode.GETFIELD: | 127 | case Opcode.GETFIELD: |
| 153 | case Opcode.GETSTATIC: | 128 | case Opcode.GETSTATIC: |
| 154 | return new Index( m_iter, pos + 1, true ); | 129 | return new Index(m_iter, pos + 1, true); |
| 155 | 130 | ||
| 156 | case Opcode.LDC: | 131 | case Opcode.LDC: |
| 157 | return new Index( m_iter, pos + 1, false ); | 132 | return new Index(m_iter, pos + 1, false); |
| 158 | } | 133 | } |
| 159 | } | 134 | } |
| 160 | 135 | ||
| 161 | return null; | 136 | return null; |
| 162 | } | 137 | } |
| 163 | 138 | ||
| 164 | public Iterable<Index> indices( ) | 139 | public Iterable<Index> indices() { |
| 165 | { | 140 | return new Iterable<Index>() { |
| 166 | return new Iterable<Index>( ) | ||
| 167 | { | ||
| 168 | @Override | 141 | @Override |
| 169 | public Iterator<Index> iterator( ) | 142 | public Iterator<Index> iterator() { |
| 170 | { | ||
| 171 | return BytecodeIndexIterator.this; | 143 | return BytecodeIndexIterator.this; |
| 172 | } | 144 | } |
| 173 | }; | 145 | }; |
| 174 | } | 146 | } |
| 175 | 147 | ||
| 176 | public void saveChangesToBytecode( ) | 148 | public void saveChangesToBytecode() { |
| 177 | { | 149 | BytecodeTools.setBytecode(m_bytecode, m_attribute.getCode()); |
| 178 | BytecodeTools.setBytecode( m_bytecode, m_attribute.getCode() ); | ||
| 179 | } | 150 | } |
| 180 | } | 151 | } |