Skip to content

Commit

Permalink
fix: fix default branch position in switch-over-string (PR #2331)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubiqq authored Oct 31, 2024
1 parent 2661b91 commit cfbe5ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ private boolean restoreSwitchOverString(MethodNode mth, SwitchRegion switchRegio
// all checks passed, replace with new switch
IRegion parentRegion = switchRegion.getParent();
SwitchRegion replaceRegion = new SwitchRegion(parentRegion, switchRegion.getHeader());
replaceRegion.addDefaultCase(switchData.getDefaultCode());
for (CaseData caseData : switchData.getCases()) {
replaceRegion.addCase(Collections.unmodifiableList(caseData.getStrValues()), caseData.getCode());
}
replaceRegion.addDefaultCase(switchData.getDefaultCode());
if (!parentRegion.replaceSubBlock(switchRegion, replaceRegion)) {
mth.addWarnComment("Failed to restore switch over string. Please report as a decompilation issue");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.assertj.core.api.AbstractStringAssert;
import org.assertj.core.internal.Failures;

import jadx.tests.api.utils.TestUtils;

import static org.assertj.core.error.ShouldNotContainSubsequence.shouldNotContainSubsequence;

public class JadxCodeAssertions extends AbstractStringAssert<JadxCodeAssertions> {

private Failures failures = Failures.instance();

public JadxCodeAssertions(String code) {
super(code, JadxCodeAssertions.class);
}
Expand Down Expand Up @@ -65,6 +72,21 @@ public JadxCodeAssertions containsLines(int commonIndent, String... lines) {
return containsOnlyOnce(sb.substring(1));
}

public JadxCodeAssertions doesNotContainSubsequence(CharSequence... values) {
final var regex = Arrays.stream(values)
.map(value -> Pattern.quote(value.toString()))
.collect(Collectors.joining(".*"));

final var pattern = Pattern.compile(regex, Pattern.DOTALL);

final var matcher = pattern.matcher(actual);
if (matcher.find()) {
throw failures.failure(info, shouldNotContainSubsequence(actual, values, matcher.start()));
}

return this;
}

public JadxCodeAssertions removeBlockComments() {
String code = actual.replaceAll("/\\*.*\\*/", "");
JadxCodeAssertions newCode = new JadxCodeAssertions(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void test() {
.code()
.doesNotContain("case -603257287:")
.doesNotContain("c = ")
.doesNotContainSubsequence("default:", "case ")
.containsOne("case \"frewhyh\":")
.countString(5, "return ");
}
Expand Down

0 comments on commit cfbe5ab

Please sign in to comment.