Skip to content

Commit

Permalink
feat(objectionary#540): fix all the unit and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Oct 8, 2024
1 parent 6e5e7bd commit 74fbcd1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ public boolean isBranchInstruction() {
case IF_ACMPNE:
case IFNULL:
case IFNONNULL:
case TABLESWITCH:
case LOOKUPSWITCH:
return true;
default:
return false;
Expand All @@ -521,6 +523,18 @@ public boolean isConditionalBranchInstruction() {
case IF_ACMPNE:
case IFNULL:
case IFNONNULL:
case TABLESWITCH:
case LOOKUPSWITCH:
return true;
default:
return false;
}
}

public boolean isSwitchInstruction() {
switch (Instruction.find(this.opcode)) {
case TABLESWITCH:
case LOOKUPSWITCH:
return true;
default:
return false;
Expand All @@ -541,6 +555,38 @@ public boolean isReturnInstruction() {
}
}

public List<Label> offsets() {
if (!this.isSwitchInstruction()) {
throw new IllegalStateException(
String.format(
"Instruction %s is not a switch instruction",
new OpcodeName(this.opcode).simplified()
)
);
}
switch (Instruction.find(this.opcode)) {
case TABLESWITCH: {
return this.args.stream()
.filter(Label.class::isInstance)
.map(Label.class::cast)
.collect(Collectors.toList());
}
case LOOKUPSWITCH: {
return this.args.stream()
.filter(Label.class::isInstance)
.map(Label.class::cast)
.collect(Collectors.toList());
}
default:
throw new IllegalStateException(
String.format(
"Instruction %s is not a switch instruction",
new OpcodeName(this.opcode).simplified()
)
);
}
}

public Label offset() {
if (!this.isBranchInstruction()) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,20 +457,37 @@ private int computeStack() {
// current, (k, v) -> v == null ? finalStack : Math.max(v, finalStack)
// );
if (var.isBranchInstruction()) {
final int jump = this.index(var.offset());
if (visited.get(jump) == null || visited.get(jump) < stack) {
worklist.add(jump);
if (var.isSwitchInstruction()) {
final List<Label> offsets = var.offsets();
for (Label offset : offsets) {
final int target = this.index(offset);
if (visited.get(target) == null || visited.get(target) < stack) {
worklist.add(target);
}
}
if (var.isConditionalBranchInstruction()) {
final int next = current + 1;
if (visited.get(next) == null || visited.get(next) < stack) {
worklist.add(next);
}
}
break;
} else {
final int jump = this.index(var.offset());
if (visited.get(jump) == null || visited.get(jump) < stack) {
worklist.add(jump);
if (var.isConditionalBranchInstruction()) {
final int next = current + 1;
if (visited.get(next) == null || visited.get(next) < stack) {
worklist.add(next);
}
}
break;
}
}
if (var.isReturnInstruction()) {
break;
}
break;
}
if (var.isReturnInstruction()) {
break;
}
}
current++;
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/maxs/Maxs.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,19 @@ public static int problematicStatic(String[] args) {
return obj.length();
}

public int switchInsideLoopCase(byte x) {
for (int i = 0; i < 10; ++i) {
switch (x) {
case 1:
break;
default:
throw new IllegalArgumentException("Unexpected value: " + x);
}
}
return 2;
}


// Inner class to add complexity
private class Inner {
public String partOne() {
Expand Down

0 comments on commit 74fbcd1

Please sign in to comment.