Skip to content

Commit

Permalink
feat: add throw instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 committed Aug 10, 2024
1 parent 1744dd1 commit 4806937
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.classfile.instruction.ReturnInstruction;
import java.lang.classfile.instruction.StackInstruction;
import java.lang.classfile.instruction.StoreInstruction;
import java.lang.classfile.instruction.ThrowInstruction;
import java.lang.classfile.instruction.TypeCheckInstruction;
import java.lang.reflect.Field;
import java.util.Stack;
Expand Down Expand Up @@ -305,6 +306,11 @@ private void addInstructionNode(Instruction instruction) {
Tree node = treeContext.createTree(type);
addLeafNode(node);
}
case ThrowInstruction throwInstruction -> {
Type type = TypeSet.type(throwInstruction.opcode().name());
Tree node = treeContext.createTree(type);
addLeafNode(node);
}
default -> {
// todo: to be implemented
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ClassFileVisitorTest {
private static final Path RESOURCES = Path.of("src/test/resources");

private static final Path EQ = RESOURCES.resolve("EQ");
private static final Path NEQ1 = RESOURCES.resolve("NEQ1");

@Test
void shouldShowDiffIn_majorVersion() throws IOException {
Expand Down Expand Up @@ -176,7 +177,7 @@ void gumtreeShowsDifferenceBetween_tryCatchFinally_and_tryWithResource() throws

// assert
List<Action> rootOperations = diff.getRootOperations();
assertThat(rootOperations).size().isEqualTo(28);
assertThat(rootOperations).size().isEqualTo(29);
}

@Test
Expand Down Expand Up @@ -378,6 +379,21 @@ void shouldDelete3Instructions() throws IOException {
}
}

@Nested
class NEQ1 {
@Disabled("after difference in exception table is implemented")
@Test
void shouldShowDifferenceInExceptionTable() throws IOException {
Path oldClass = NEQ1.resolve("2").resolve("commons-codec-1.15").resolve("BaseNCodecOutputStream.class");
Path newClass = NEQ1.resolve("2").resolve("commons-codec-1.16.0").resolve("BaseNCodecOutputStream.class");
DiffImpl diff = getDiff(oldClass, newClass);

// assert
List<Action> rootOperations = diff.getRootOperations();
assertThat(rootOperations).size().isEqualTo(1);
}
}

private static DiffImpl getDiff(Path oldClass, Path newClass) throws IOException {
byte[] bytes1 = Files.readAllBytes(oldClass);
byte[] bytes2 = Files.readAllBytes(newClass);
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/NEQ1/2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Gumtree shows difference between the classfile version which is ignored by
diffoscope.
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions src/test/resources/NEQ1/2/diffoscope.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--- commons-codec-1.15/BaseNCodecOutputStream.class
+++ commons-codec-1.16.0/BaseNCodecOutputStream.class
├── procyon -ec {}
│ @@ -12,30 +12,30 @@
│ public class BaseNCodecOutputStream extends FilterOutputStream
│ {
│ private final boolean doEncode;
│ private final BaseNCodec baseNCodec;
│ private final byte[] singleByte;
│ private final BaseNCodec.Context context;
│ - public BaseNCodecOutputStream(final OutputStream output, final BaseNCodec basedCodec, final boolean doEncode) {
│ - super(output);
│ + public BaseNCodecOutputStream(final OutputStream outputStream, final BaseNCodec basedCodec, final boolean doEncode) {
│ + super(outputStream);
│ this.singleByte = new byte[1];
│ this.context = new BaseNCodec.Context();
│ this.baseNCodec = basedCodec;
│ this.doEncode = doEncode;
│ }
│ @Override
│ public void close() throws IOException {
│ this.eof();
│ this.flush();
│ this.out.close();
│ }
│ - public void eof() throws IOException {
│ + public void eof() {
│ if (this.doEncode) {
│ this.baseNCodec.encode(this.singleByte, 0, -1, this.context);
│ }
│ else {
│ this.baseNCodec.decode(this.singleByte, 0, -1, this.context);
│ }
│ }
5 changes: 5 additions & 0 deletions src/test/resources/NEQ1/2/gumtree.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[===
update-node
---
majorVersion: 51 [6,8]
replace 51 by 52]

0 comments on commit 4806937

Please sign in to comment.