77import java .util .Arrays ;
88
99import twg2 .jbcm .modify .ByteCodeConsumer ;
10- import twg2 .jbcm .modify .OpcodeChangeCpIndex ;
11- import twg2 .jbcm .modify .OpcodeChangeOffset ;
10+ import twg2 .jbcm .modify .ChangeCpIndex ;
11+ import twg2 .jbcm .modify .CpIndexChanger ;
12+ import twg2 .jbcm .modify .CodeOffsetChanger ;
1213
1314/**
1415 * @author TeamworkGuy2
1516 * @since 2014-4-19
1617 */
1718public final class IoUtility {
1819
19- public static final OpcodeChangeCpIndex EMPTY_OPCODE_CP_INDEX = new OpcodeChangeCpIndex () {
20+ public static final CpIndexChanger EMPTY_OPCODE_CP_INDEX = new CpIndexChanger () {
2021 @ Override public void shiftIndex (byte [] code , int location , int offset ) {
2122 }
2223
2324 @ Override
2425 public void changeCpIndexIf (byte [] code , int location , int currentIndex , int newIndex ) {
2526 }
2627 };
27- public static final OpcodeChangeOffset EMPTY_OPCODE_OFFSET = new OpcodeChangeOffset () {
28+ public static final CodeOffsetChanger EMPTY_OPCODE_OFFSET = new CodeOffsetChanger () {
2829 @ Override public void shiftIndex (byte [] code , int location , int offset ) {
2930 }
3031 };
@@ -122,12 +123,11 @@ public static final short readShort(byte[] b, int offset) {
122123 * @param opcode the opcode to look for in the code
123124 * @param offset the offset to add to offset values
124125 * @param offsetOffset the number of bytes ahead of the opcode at which the offset to adjust starts (1 for an offset that immediately follows an opcode)
125- * @param offsetLen the length of the offset value (2 for a short, 4 for an int)
126126 * @param code the array of code to search through for the opcode
127127 * @param codeOffset the offset into the code array at which to update the opcode's offset value
128128 * @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + offsetLen}
129129 */
130- private static final int shift1Offset (final int opcode , final int offset , final int offsetOffset , byte [] code , int codeOffset ) {
130+ public static final int shift1Offset (final int opcode , final int offset , final int offsetOffset , byte [] code , int codeOffset ) {
131131 byte op = code [codeOffset ];
132132 if (op == opcode ) {
133133 codeOffset +=offsetOffset ;
@@ -154,12 +154,11 @@ private static final int shift1Offset(final int opcode, final int offset, final
154154 * @param opcode the opcode to look for in the code
155155 * @param offset the offset to add to offset values
156156 * @param offsetOffset the number of bytes ahead of the opcode at which the offset to adjust starts (1 for an offset that immediately follows an opcode)
157- * @param offsetLen the length of the offset value (2 for a short, 4 for an int)
158157 * @param code the array of code to search through for the opcode
159158 * @param codeOffset the offset into the code array at which to update the opcode's offset value
160159 * @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + offsetLen}
161160 */
162- private static final int shift2Offset (final int opcode , final int offset , final int offsetOffset , byte [] code , int codeOffset ) {
161+ public static final int shift2Offset (final int opcode , final int offset , final int offsetOffset , byte [] code , int codeOffset ) {
163162 byte op = code [codeOffset ];
164163 if (op == opcode ) {
165164 codeOffset +=offsetOffset ;
@@ -186,12 +185,11 @@ private static final int shift2Offset(final int opcode, final int offset, final
186185 * @param opcode the opcode to look for in the code
187186 * @param offset the offset to add to offset values
188187 * @param offsetOffset the number of bytes ahead of the opcode at which the offset to adjust starts (1 for an offset that immediately follows an opcode)
189- * @param offsetLen the length of the offset value (2 for a short, 4 for an int)
190188 * @param code the array of code to search through for the opcode
191189 * @param codeOffset the offset into the code array at which to update the opcode's offset value
192190 * @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + offsetLen}
193191 */
194- private static final int shift4Offset (final int opcode , final int offset , final int offsetOffset , byte [] code , int codeOffset ) {
192+ public static final int shift4Offset (final int opcode , final int offset , final int offsetOffset , byte [] code , int codeOffset ) {
195193 byte op = code [codeOffset ];
196194 if (op == opcode ) {
197195 codeOffset +=offsetOffset ;
@@ -253,15 +251,15 @@ private static final int loadOperands(int numOperands, byte[] code, int index) {
253251 }
254252
255253
256- /** A default implementation of {@link OpcodeChangeOffset }.
254+ /** A default implementation of {@link CodeOffsetChanger }.
257255 * The {@link #accept(byte[], int, int)} in this implementation simply calls {@code IoUtility.shiftOffset(...)}
258- * using the parameters from the constructor and the {@link OpcodeChangeOffset #accept(byte[], int, int)} method.
256+ * using the parameters from the constructor and the {@link CodeOffsetChanger #accept(byte[], int, int)} method.
259257 * @author TeamworkGuy2
260258 * @since 2014-419
261259 */
262260 /*
263- public static class ChangeOffsetDefault implements OpcodeChangeOffset {
264- public static final OpcodeChangeOffset EMPTY = new OpcodeChangeOffset () {
261+ public static class ChangeOffsetDefault implements CodeOffsetChanger {
262+ public static final CodeOffsetChanger EMPTY = new CodeOffsetChanger () {
265263 @Override public void accept(byte[] code, int location, int offset) {
266264 }
267265 };
@@ -302,81 +300,17 @@ else if(offsetLen == 1) {
302300 */
303301
304302
305- /** A default implementation of {@link OpcodeChangeCpIndex}.
306- * The {@link #shiftIndex(byte[], int, int)} method calls {@code IoUtility.shiftOffset(...)}
307- * using the parameters from this object's constructor and the {@code shiftCpIndex} method call.
308- * @author TeamworkGuy2
309- * @since 2014-4-20
310- */
311- public static class ChangeCpIndex implements OpcodeChangeCpIndex , OpcodeChangeOffset {
312- private final int opcode ;
313- private final int offsetOffset ;
314- private final int offsetLen ;
315-
316- public ChangeCpIndex (int opcode , int offsetOffset , int offsetLen ) {
317- if (offsetLen != 1 && offsetLen != 2 && offsetLen != 4 ) {
318- throw new IllegalArgumentException ("cannot shift offsets values that are not 1, 2, or 4 bytes long: " + offsetLen );
319- }
320- this .opcode = opcode ;
321- this .offsetOffset = offsetOffset ;
322- this .offsetLen = offsetLen ;
323- }
324-
325- @ Override
326- public void shiftIndex (byte [] code , int location , int offset ) {
327- if (offsetLen == 2 ) {
328- IoUtility .shift2Offset (opcode , offset , offsetOffset , code , location );
329- }
330- else if (offsetLen == 4 ) {
331- IoUtility .shift4Offset (opcode , offset , offsetOffset , code , location );
332- }
333- else if (offsetLen == 1 ) {
334- IoUtility .shift1Offset (opcode , offset , offsetOffset , code , location );
335- }
336- else {
337- throw new IllegalStateException ("offset length must be 1, 2, or 4" );
338- }
339- }
340-
341- @ Override
342- public void changeCpIndexIf (byte [] code , int location , int currentIndex , int newIndex ) {
343- location +=offsetOffset ;
344- if (offsetLen == 2 ) {
345- short index = IoUtility .readShort (code , location );
346- if (index == currentIndex ) {
347- IoUtility .writeShort ((short )newIndex , code , location );
348- }
349- }
350- else if (offsetLen == 4 ) {
351- int index = IoUtility .readInt (code , location );
352- if (index == currentIndex ) {
353- IoUtility .writeInt (newIndex , code , location );
354- }
355- }
356- else if (offsetLen == 1 ) {
357- byte index = code [location ];
358- if (index == currentIndex ) {
359- code [location ] = (byte )newIndex ;
360- }
361- }
362- else {
363- throw new IllegalStateException ("offset length must be 1, 2, or 4" );
364- }
365- }
366- }
367-
368-
369- public static OpcodeChangeCpIndex cpIndex (int opcode , int offset , int len ) {
303+ public static CpIndexChanger cpIndex (int opcode , int offset , int len ) {
370304 return new ChangeCpIndex (opcode , offset , len );
371305 }
372306
373307
374- public static OpcodeChangeOffset offsetModifier (int opcode , int offset , int len ) {
308+ public static CodeOffsetChanger offsetModifier (int opcode , int offset , int len ) {
375309 return new ChangeCpIndex (opcode , offset , len );
376310 }
377311
378312
379- public static final OpcodeChangeOffset TableswitchOffsetModifier = new OpcodeChangeOffset () {
313+ public static final CodeOffsetChanger TableswitchOffsetModifier = new CodeOffsetChanger () {
380314 /** Add an offset to all of the tableswitch instructions in the
381315 * specified chunk of code
382316 * @param offset the offset to adjust all tableswitch offsets by.
@@ -395,15 +329,15 @@ public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len)
395329 int defaultOffset = IoUtility .readInt (code , location );
396330 defaultOffset += offset ;
397331 IoUtility .writeInt (defaultOffset , code , location );
398- location += 4 ;
332+ location += 4 ;
399333 // low
400334 int low = IoUtility .readInt (code , location );
401- location += 4 ;
335+ location += 4 ;
402336 // high
403337 int high = IoUtility .readInt (code , location );
404- location += 4 ;
338+ location += 4 ;
405339 // For each jump-offset 32bit value
406- for (int ii = 0 ; ii < (high -low +1 ); ii ++, location += 4 ) {
340+ for (int ii = 0 ; ii < (high -low +1 ); ii ++, location += 4 ) {
407341 int matchOffset = IoUtility .readInt (code , location );
408342 matchOffset += offset ;
409343 IoUtility .writeInt (matchOffset , code , location );
@@ -416,7 +350,7 @@ public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len)
416350 };
417351
418352
419- public static final OpcodeChangeOffset LookupswitchOffsetModifier = new OpcodeChangeOffset () {
353+ public static final CodeOffsetChanger LookupswitchOffsetModifier = new CodeOffsetChanger () {
420354 /** Add an offset to all of the lookupswitch instructions in the
421355 * specified chunk of code
422356 * @param offset the offset to adjust all lookupswitch offsets by.
@@ -435,15 +369,15 @@ public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len)
435369 int defaultOffset = IoUtility .readInt (code , location );
436370 defaultOffset += offset ;
437371 IoUtility .writeInt (defaultOffset , code , location );
438- location += 4 ;
372+ location += 4 ;
439373 // Number of pairs
440374 int npairs = IoUtility .readInt (code , location );
441- location += 4 ;
375+ location += 4 ;
442376 // For each pair of match-offset 32bit values
443- for (int ii = 0 ; ii < npairs ; ii ++, location += 8 ) {
444- int matchOffset = IoUtility .readInt (code , location + 4 );
377+ for (int ii = 0 ; ii < npairs ; ii ++, location += 8 ) {
378+ int matchOffset = IoUtility .readInt (code , location + 4 );
445379 matchOffset += offset ;
446- IoUtility .writeInt (matchOffset , code , location + 4 );
380+ IoUtility .writeInt (matchOffset , code , location + 4 );
447381 }
448382 }
449383 else {
0 commit comments