Skip to content

Commit

Permalink
0.2.0 - Renamed some interfaces, removed unnecessary opcode classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamworkGuy2 committed Aug 14, 2017
1 parent dc1eda0 commit 604a829
Show file tree
Hide file tree
Showing 42 changed files with 436 additions and 1,456 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="C:/Users/TeamworkGuy2/Documents/Java/Libraries/java/eclipse/jar/ecj-4.4M2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/


--------
###[0.1.0](N/A) - 2017-01-11
### [0.2.0](N/A) - 2017-08-13
#### Changed
* Cleaned up unnecessary twg2.jbcm.opcode package classes
* Renamed twg2.jbcm.modify.IndexChange -> IndexChanger
* Renamed twg2.jbcm.modify.OpcodeChangeCpIndex -> CpIndexChanger
* Renamed twg2.jbcm.modify.OpcodeChangeOffset -> CodeOffsetChanger


--------
### [0.1.0](https://github.com/TeamworkGuy2/ClassLoading/commit/dc1eda0b07c1d9746b8a63affff8d8b7b0d2103f) - 2017-01-11
#### Added
* Initial versioning of existing code, including classfile loading/parsing, dynamic classfile modification, and runtime class loading.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
ClassLoading
==========
version: 0.1.0
version: 0.2.0

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

###See `twg2.jbcm.main/`
for example code and interactive command line tools.

####`twg2.jbcm.classFormat`
### `twg2.jbcm.classFormat`
Contains implementation of the [class file format](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html)
with related attributes and constant pool types.

####`twg2.jbcm` and `twg2.jbcm.modify`
### `twg2.jbcm` and `twg2.jbcm.modify`
Interfaces and utilities for searching and modifying class files.

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

####`twg2.jbcm.dynamicModification`, `twg2.jbcm.parserExamples`
### `twg2.jbcm.dynamicModification` and `twg2.jbcm.parserExamples`
Classes used by the example and test packages.

####`twg2.jbcm.runtimeLoading`
### `twg2.jbcm.runtimeLoading`
Runtime class loading.

####`twg2.jbcm.main`
### `twg2.jbcm.main`
Example console apps.
2 changes: 1 addition & 1 deletion package-lib.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "0.1.0",
"version" : "0.2.0",
"name" : "class-loading",
"description" : "Java class file parsing and manipulation",
"homepage" : "https://github.com/TeamworkGuy2/ClassLoading",
Expand Down
116 changes: 25 additions & 91 deletions src/twg2/jbcm/IoUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
import java.util.Arrays;

import twg2.jbcm.modify.ByteCodeConsumer;
import twg2.jbcm.modify.OpcodeChangeCpIndex;
import twg2.jbcm.modify.OpcodeChangeOffset;
import twg2.jbcm.modify.ChangeCpIndex;
import twg2.jbcm.modify.CpIndexChanger;
import twg2.jbcm.modify.CodeOffsetChanger;

/**
* @author TeamworkGuy2
* @since 2014-4-19
*/
public final class IoUtility {

public static final OpcodeChangeCpIndex EMPTY_OPCODE_CP_INDEX = new OpcodeChangeCpIndex() {
public static final CpIndexChanger EMPTY_OPCODE_CP_INDEX = new CpIndexChanger() {
@Override public void shiftIndex(byte[] code, int location, int offset) {
}

@Override
public void changeCpIndexIf(byte[] code, int location, int currentIndex, int newIndex) {
}
};
public static final OpcodeChangeOffset EMPTY_OPCODE_OFFSET = new OpcodeChangeOffset() {
public static final CodeOffsetChanger EMPTY_OPCODE_OFFSET = new CodeOffsetChanger() {
@Override public void shiftIndex(byte[] code, int location, int offset) {
}
};
Expand Down Expand Up @@ -122,12 +123,11 @@ public static final short readShort(byte[] b, int offset) {
* @param opcode the opcode to look for in the code
* @param offset the offset to add to offset values
* @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)
* @param offsetLen the length of the offset value (2 for a short, 4 for an int)
* @param code the array of code to search through for the opcode
* @param codeOffset the offset into the code array at which to update the opcode's offset value
* @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + offsetLen}
*/
private static final int shift1Offset(final int opcode, final int offset, final int offsetOffset, byte[] code, int codeOffset) {
public static final int shift1Offset(final int opcode, final int offset, final int offsetOffset, byte[] code, int codeOffset) {
byte op = code[codeOffset];
if(op == opcode) {
codeOffset+=offsetOffset;
Expand All @@ -154,12 +154,11 @@ private static final int shift1Offset(final int opcode, final int offset, final
* @param opcode the opcode to look for in the code
* @param offset the offset to add to offset values
* @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)
* @param offsetLen the length of the offset value (2 for a short, 4 for an int)
* @param code the array of code to search through for the opcode
* @param codeOffset the offset into the code array at which to update the opcode's offset value
* @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + offsetLen}
*/
private static final int shift2Offset(final int opcode, final int offset, final int offsetOffset, byte[] code, int codeOffset) {
public static final int shift2Offset(final int opcode, final int offset, final int offsetOffset, byte[] code, int codeOffset) {
byte op = code[codeOffset];
if(op == opcode) {
codeOffset+=offsetOffset;
Expand All @@ -186,12 +185,11 @@ private static final int shift2Offset(final int opcode, final int offset, final
* @param opcode the opcode to look for in the code
* @param offset the offset to add to offset values
* @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)
* @param offsetLen the length of the offset value (2 for a short, 4 for an int)
* @param code the array of code to search through for the opcode
* @param codeOffset the offset into the code array at which to update the opcode's offset value
* @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + offsetLen}
*/
private static final int shift4Offset(final int opcode, final int offset, final int offsetOffset, byte[] code, int codeOffset) {
public static final int shift4Offset(final int opcode, final int offset, final int offsetOffset, byte[] code, int codeOffset) {
byte op = code[codeOffset];
if(op == opcode) {
codeOffset+=offsetOffset;
Expand Down Expand Up @@ -253,15 +251,15 @@ private static final int loadOperands(int numOperands, byte[] code, int index) {
}


/** A default implementation of {@link OpcodeChangeOffset}.
/** A default implementation of {@link CodeOffsetChanger}.
* The {@link #accept(byte[], int, int)} in this implementation simply calls {@code IoUtility.shiftOffset(...)}
* using the parameters from the constructor and the {@link OpcodeChangeOffset#accept(byte[], int, int)} method.
* using the parameters from the constructor and the {@link CodeOffsetChanger#accept(byte[], int, int)} method.
* @author TeamworkGuy2
* @since 2014-419
*/
/*
public static class ChangeOffsetDefault implements OpcodeChangeOffset {
public static final OpcodeChangeOffset EMPTY = new OpcodeChangeOffset() {
public static class ChangeOffsetDefault implements CodeOffsetChanger {
public static final CodeOffsetChanger EMPTY = new CodeOffsetChanger() {
@Override public void accept(byte[] code, int location, int offset) {
}
};
Expand Down Expand Up @@ -302,81 +300,17 @@ else if(offsetLen == 1) {
*/


/** A default implementation of {@link OpcodeChangeCpIndex}.
* The {@link #shiftIndex(byte[], int, int)} method calls {@code IoUtility.shiftOffset(...)}
* using the parameters from this object's constructor and the {@code shiftCpIndex} method call.
* @author TeamworkGuy2
* @since 2014-4-20
*/
public static class ChangeCpIndex implements OpcodeChangeCpIndex, OpcodeChangeOffset {
private final int opcode;
private final int offsetOffset;
private final int offsetLen;

public ChangeCpIndex(int opcode, int offsetOffset, int offsetLen) {
if(offsetLen != 1 && offsetLen != 2 && offsetLen != 4) {
throw new IllegalArgumentException("cannot shift offsets values that are not 1, 2, or 4 bytes long: " + offsetLen);
}
this.opcode = opcode;
this.offsetOffset = offsetOffset;
this.offsetLen = offsetLen;
}

@Override
public void shiftIndex(byte[] code, int location, int offset) {
if(offsetLen == 2) {
IoUtility.shift2Offset(opcode, offset, offsetOffset, code, location);
}
else if(offsetLen == 4) {
IoUtility.shift4Offset(opcode, offset, offsetOffset, code, location);
}
else if(offsetLen == 1) {
IoUtility.shift1Offset(opcode, offset, offsetOffset, code, location);
}
else {
throw new IllegalStateException("offset length must be 1, 2, or 4");
}
}

@Override
public void changeCpIndexIf(byte[] code, int location, int currentIndex, int newIndex) {
location+=offsetOffset;
if(offsetLen == 2) {
short index = IoUtility.readShort(code, location);
if(index == currentIndex) {
IoUtility.writeShort((short)newIndex, code, location);
}
}
else if(offsetLen == 4) {
int index = IoUtility.readInt(code, location);
if(index == currentIndex) {
IoUtility.writeInt(newIndex, code, location);
}
}
else if(offsetLen == 1) {
byte index = code[location];
if(index == currentIndex) {
code[location] = (byte)newIndex;
}
}
else {
throw new IllegalStateException("offset length must be 1, 2, or 4");
}
}
}


public static OpcodeChangeCpIndex cpIndex(int opcode, int offset, int len) {
public static CpIndexChanger cpIndex(int opcode, int offset, int len) {
return new ChangeCpIndex(opcode, offset, len);
}


public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len) {
public static CodeOffsetChanger offsetModifier(int opcode, int offset, int len) {
return new ChangeCpIndex(opcode, offset, len);
}


public static final OpcodeChangeOffset TableswitchOffsetModifier = new OpcodeChangeOffset() {
public static final CodeOffsetChanger TableswitchOffsetModifier = new CodeOffsetChanger() {
/** Add an offset to all of the tableswitch instructions in the
* specified chunk of code
* @param offset the offset to adjust all tableswitch offsets by.
Expand All @@ -395,15 +329,15 @@ public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len)
int defaultOffset = IoUtility.readInt(code, location);
defaultOffset += offset;
IoUtility.writeInt(defaultOffset, code, location);
location+=4;
location += 4;
// low
int low = IoUtility.readInt(code, location);
location+=4;
location += 4;
// high
int high = IoUtility.readInt(code, location);
location+=4;
location += 4;
// For each jump-offset 32bit value
for(int ii = 0; ii < (high-low+1); ii++, location+=4) {
for(int ii = 0; ii < (high-low+1); ii++, location += 4) {
int matchOffset = IoUtility.readInt(code, location);
matchOffset += offset;
IoUtility.writeInt(matchOffset, code, location);
Expand All @@ -416,7 +350,7 @@ public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len)
};


public static final OpcodeChangeOffset LookupswitchOffsetModifier = new OpcodeChangeOffset() {
public static final CodeOffsetChanger LookupswitchOffsetModifier = new CodeOffsetChanger() {
/** Add an offset to all of the lookupswitch instructions in the
* specified chunk of code
* @param offset the offset to adjust all lookupswitch offsets by.
Expand All @@ -435,15 +369,15 @@ public static OpcodeChangeOffset offsetModifier(int opcode, int offset, int len)
int defaultOffset = IoUtility.readInt(code, location);
defaultOffset += offset;
IoUtility.writeInt(defaultOffset, code, location);
location+=4;
location += 4;
// Number of pairs
int npairs = IoUtility.readInt(code, location);
location+=4;
location += 4;
// For each pair of match-offset 32bit values
for(int ii = 0; ii < npairs; ii++, location+=8) {
int matchOffset = IoUtility.readInt(code, location+4);
for(int ii = 0; ii < npairs; ii++, location += 8) {
int matchOffset = IoUtility.readInt(code, location + 4);
matchOffset += offset;
IoUtility.writeInt(matchOffset, code, location+4);
IoUtility.writeInt(matchOffset, code, location + 4);
}
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/twg2/jbcm/Offset.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package twg2.jbcm;

import twg2.jbcm.modify.OpcodeChangeOffset;
import twg2.jbcm.modify.CodeOffsetChanger;

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

public OpcodeChangeOffset getOffsetModifier();
public CodeOffsetChanger getOffsetModifier();

}
16 changes: 8 additions & 8 deletions src/twg2/jbcm/Op.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package twg2.jbcm;

import twg2.jbcm.modify.OpcodeChangeCpIndex;
import twg2.jbcm.modify.OpcodeChangeOffset;
import twg2.jbcm.modify.CpIndexChanger;
import twg2.jbcm.modify.CodeOffsetChanger;

/**
* @author TeamworkGuy2
* @since 2014-4-20
*/
public final class Op {
private OpcodeChangeCpIndex cpIndex;
private OpcodeChangeOffset codeOffset;
private CpIndexChanger cpIndex;
private CodeOffsetChanger codeOffset;

private Op() {}


public final Op add(OpcodeChangeCpIndex cpIndex) {
public final Op add(CpIndexChanger cpIndex) {
this.cpIndex = cpIndex;
return this;
}


public final Op add(OpcodeChangeOffset codeOffset) {
public final Op add(CodeOffsetChanger codeOffset) {
this.codeOffset = codeOffset;
return this;
}
Expand All @@ -32,13 +32,13 @@ public final OpcodeOperations create() {
}


public static final Op of(OpcodeChangeCpIndex cpIndex) {
public static final Op of(CpIndexChanger cpIndex) {
Op op = new Op();
return op.add(cpIndex);
}


public static final Op of(OpcodeChangeOffset codeOffset) {
public static final Op of(CodeOffsetChanger codeOffset) {
Op op = new Op();
return op.add(codeOffset);
}
Expand Down
Loading

0 comments on commit 604a829

Please sign in to comment.