fix: fix default branch position in switch-over-string (PR #2331)

This commit is contained in:
pubiqq 2024-10-31 22:58:35 +03:00 committed by GitHub
parent 2661b91a6f
commit cfbe5ab672
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -103,10 +103,10 @@ public class SwitchOverStringVisitor extends AbstractVisitor implements IRegionI
// 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;

View File

@ -4,13 +4,20 @@ import java.util.ArrayList;
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);
}
@ -65,6 +72,21 @@ public class JadxCodeAssertions extends AbstractStringAssert<JadxCodeAssertions>
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);

View File

@ -46,6 +46,7 @@ public class TestSwitchOverStrings extends IntegrationTest {
.code()
.doesNotContain("case -603257287:")
.doesNotContain("c = ")
.doesNotContainSubsequence("default:", "case ")
.containsOne("case \"frewhyh\":")
.countString(5, "return ");
}