Skip to content

Commit

Permalink
fix: Distinguish between switch expressions and finally blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Mar 30, 2023
1 parent 15ee6fc commit add9a3e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main/java/reincarnation/JavaMethodDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ private Operand accessClassField(Class owner, String name) {
*/
@Override
public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
if (nStack != 0 && !tries.isCatch(current)) {
if (nStack != 0 && !tries.isCatch(current) && !tries.isFinally(current)) {
I.signal(current)
.recurseMap(x -> x.map(n -> n.previous).skipNull())
.skip(current)
Expand Down Expand Up @@ -2847,12 +2847,17 @@ private boolean match(CopiedFinally copied) {
* @return
*/
private boolean isCatch(Node node) {
for (TryCatchFinally block : blocks) {
if (block.catcher == node) {
return true;
}
}
return false;
return blocks.stream().flatMap(x -> x.blocks.stream()).anyMatch(x -> x.isCatch(node));
}

/**
* Test whether the given node is catch node or not.
*
* @param node
* @return
*/
private boolean isFinally(Node node) {
return blocks.stream().flatMap(x -> x.blocks.stream()).anyMatch(x -> x.isFinally(node));
}

/**
Expand Down Expand Up @@ -3071,6 +3076,14 @@ static class CatchOrFinally {
this.exception = exception;
this.node = node;
}

private boolean isCatch(Node node) {
return exception != null && this.node == node;
}

private boolean isFinally(Node node) {
return exception == null && this.node == node;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;

import reincarnation.CodeVerifier;
import reincarnation.Debuggable;
import reincarnation.TestCode;

class TryCatchFinallyTest extends CodeVerifier {
Expand Down Expand Up @@ -280,6 +281,7 @@ public int run(@Param(from = 0, to = 5) int param) {
}

@Test
@Debuggable
void insideSwitchWithBreak() {
verify(new TestCode.IntParam() {

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/reincarnation/decompiler/flow/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.jupiter.api.Test;

import reincarnation.CodeVerifier;
import reincarnation.Debuggable;
import reincarnation.TestCode;

class TryCatchTest extends CodeVerifier {
Expand Down Expand Up @@ -480,6 +481,7 @@ public int run(@Param(from = 0, to = 5) int param) {
}

@Test
@Debuggable
void insideSwitchWithBreak() {
verify(new TestCode.IntParam() {

Expand Down

0 comments on commit add9a3e

Please sign in to comment.