Skip to content

Commit 604a829

Browse files
committed
0.2.0 - Renamed some interfaces, removed unnecessary opcode classes.
1 parent dc1eda0 commit 604a829

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+436
-1456
lines changed

.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="lib" path="C:/Users/TeamworkGuy2/Documents/Java/Libraries/java/eclipse/jar/ecj-4.4M2.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/
44

55

66
--------
7-
###[0.1.0](N/A) - 2017-01-11
7+
### [0.2.0](N/A) - 2017-08-13
8+
#### Changed
9+
* Cleaned up unnecessary twg2.jbcm.opcode package classes
10+
* Renamed twg2.jbcm.modify.IndexChange -> IndexChanger
11+
* Renamed twg2.jbcm.modify.OpcodeChangeCpIndex -> CpIndexChanger
12+
* Renamed twg2.jbcm.modify.OpcodeChangeOffset -> CodeOffsetChanger
13+
14+
15+
--------
16+
### [0.1.0](https://github.com/TeamworkGuy2/ClassLoading/commit/dc1eda0b07c1d9746b8a63affff8d8b7b0d2103f) - 2017-01-11
817
#### Added
918
* Initial versioning of existing code, including classfile loading/parsing, dynamic classfile modification, and runtime class loading.

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
ClassLoading
22
==========
3-
version: 0.1.0
3+
version: 0.2.0
44

55
Java class file parsing and manipulation library.
66
Reference: [Java Virtual Machine Spec](http://docs.oracle.com/javase/specs/jvms/se8/html/index.html)
77

8-
###See `twg2.jbcm.main/`
9-
for example code and interactive command line tools.
10-
11-
####`twg2.jbcm.classFormat`
8+
### `twg2.jbcm.classFormat`
129
Contains implementation of the [class file format](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html)
1310
with related attributes and constant pool types.
1411

15-
####`twg2.jbcm` and `twg2.jbcm.modify`
12+
### `twg2.jbcm` and `twg2.jbcm.modify`
1613
Interfaces and utilities for searching and modifying class files.
1714

18-
####`twg2.jbcm.opcode`
15+
### `twg2.jbcm.opcode`
1916
Partial implementation of [Java instruction set opcodes](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5).
2017

21-
####`twg2.jbcm.dynamicModification`, `twg2.jbcm.parserExamples`
18+
### `twg2.jbcm.dynamicModification` and `twg2.jbcm.parserExamples`
2219
Classes used by the example and test packages.
2320

24-
####`twg2.jbcm.runtimeLoading`
21+
### `twg2.jbcm.runtimeLoading`
2522
Runtime class loading.
2623

27-
####`twg2.jbcm.main`
24+
### `twg2.jbcm.main`
2825
Example console apps.

package-lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version" : "0.1.0",
2+
"version" : "0.2.0",
33
"name" : "class-loading",
44
"description" : "Java class file parsing and manipulation",
55
"homepage" : "https://github.com/TeamworkGuy2/ClassLoading",

src/twg2/jbcm/IoUtility.java

Lines changed: 25 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
import java.util.Arrays;
88

99
import 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
*/
1718
public 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 {

src/twg2/jbcm/Offset.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package twg2.jbcm;
22

3-
import twg2.jbcm.modify.OpcodeChangeOffset;
3+
import twg2.jbcm.modify.CodeOffsetChanger;
44

55
/** An interface that is implemented by attributes or instructions that
66
* have an offset value in their metadata or opcodes.
@@ -9,6 +9,6 @@
99
*/
1010
public interface Offset {
1111

12-
public OpcodeChangeOffset getOffsetModifier();
12+
public CodeOffsetChanger getOffsetModifier();
1313

1414
}

src/twg2/jbcm/Op.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
package twg2.jbcm;
22

3-
import twg2.jbcm.modify.OpcodeChangeCpIndex;
4-
import twg2.jbcm.modify.OpcodeChangeOffset;
3+
import twg2.jbcm.modify.CpIndexChanger;
4+
import twg2.jbcm.modify.CodeOffsetChanger;
55

66
/**
77
* @author TeamworkGuy2
88
* @since 2014-4-20
99
*/
1010
public final class Op {
11-
private OpcodeChangeCpIndex cpIndex;
12-
private OpcodeChangeOffset codeOffset;
11+
private CpIndexChanger cpIndex;
12+
private CodeOffsetChanger codeOffset;
1313

1414
private Op() {}
1515

1616

17-
public final Op add(OpcodeChangeCpIndex cpIndex) {
17+
public final Op add(CpIndexChanger cpIndex) {
1818
this.cpIndex = cpIndex;
1919
return this;
2020
}
2121

2222

23-
public final Op add(OpcodeChangeOffset codeOffset) {
23+
public final Op add(CodeOffsetChanger codeOffset) {
2424
this.codeOffset = codeOffset;
2525
return this;
2626
}
@@ -32,13 +32,13 @@ public final OpcodeOperations create() {
3232
}
3333

3434

35-
public static final Op of(OpcodeChangeCpIndex cpIndex) {
35+
public static final Op of(CpIndexChanger cpIndex) {
3636
Op op = new Op();
3737
return op.add(cpIndex);
3838
}
3939

4040

41-
public static final Op of(OpcodeChangeOffset codeOffset) {
41+
public static final Op of(CodeOffsetChanger codeOffset) {
4242
Op op = new Op();
4343
return op.add(codeOffset);
4444
}

0 commit comments

Comments
 (0)