Skip to content

Commit 992db8a

Browse files
committed
Fix SimplifyExprentsHelper accidentally destroying pattern matches
1 parent cd1dfa6 commit 992db8a

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public static boolean simplifyStackVarsStatement(
9090
}
9191
}
9292
} else {
93-
res = simplifyStackVarsExprents(expressions, cl, firstInvocation);
93+
res = simplifyStackVarsExprents(expressions, cl, stat, firstInvocation);
9494
}
9595

9696
return res;
9797
}
9898

99-
private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass cl, boolean firstInvocation) {
99+
private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass cl, Statement stat, boolean firstInvocation) {
100100
boolean res = false;
101101

102102
int index = 0;
@@ -149,7 +149,7 @@ private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass
149149
}
150150
}
151151

152-
if (isAssignmentReturn(current, next)) {
152+
if (isAssignmentReturn(current, next, stat)) {
153153
list.remove(index);
154154
res = true;
155155
continue;
@@ -396,7 +396,7 @@ private static int isArrayInitializer(List<Exprent> list, int index) {
396396
* Note that this is transformation will result into java that is less like the original.
397397
* TODO: put this behind a compiler option.
398398
*/
399-
private static boolean isAssignmentReturn(Exprent first, Exprent second) {
399+
private static boolean isAssignmentReturn(Exprent first, Exprent second, Statement stat) {
400400
//If assignment then exit.
401401
if (first instanceof AssignmentExprent
402402
&& second instanceof ExitExprent) {
@@ -406,12 +406,20 @@ private static boolean isAssignmentReturn(Exprent first, Exprent second) {
406406
if (assignment.getCondType() == null
407407
&& exit.getExitType() == ExitExprent.Type.RETURN
408408
&& exit.getValue() != null
409-
&& assignment.getLeft() instanceof VarExprent
410-
&& exit.getValue() instanceof VarExprent) {
411-
VarExprent assignmentLeft = (VarExprent) assignment.getLeft();
412-
VarExprent exitValue = (VarExprent) exit.getValue();
409+
&& assignment.getLeft() instanceof VarExprent assignmentLeft
410+
&& exit.getValue() instanceof VarExprent exitValue) {
413411
//If the assignment before the return is immediately used in the return, inline it.
414412
if (assignmentLeft.equals(exitValue) && !assignmentLeft.isStack() && !exitValue.isStack()) {
413+
// Avoid doing this transform for potential pattern matches, as they should be processed by the pattern matcher first.
414+
if (stat.getTopParent().mt.getBytecodeVersion().hasIfPatternMatching()
415+
&& stat.getParent() instanceof IfStatement ifst && !ifst.isPatternMatched() && stat.getExprents().indexOf(first) == 0
416+
&& assignment.getRight() instanceof FunctionExprent func && func.getFuncType() == FunctionType.CAST
417+
// Most expensive, do it last
418+
&& ifst.getHeadexprent().getAllExprents(true, false).stream().anyMatch(e -> e instanceof FunctionExprent f && f.getFuncType() == FunctionType.INSTANCEOF)
419+
) {
420+
return false;
421+
}
422+
415423
exit.replaceExprent(exitValue, assignment.getRight());
416424
return true;
417425
}

testData/results/pkg/TestPatternMatchingReturn.dec

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package pkg;
22

33
public class TestPatternMatchingReturn {
44
public String getPattern(Object obj) {
5-
if (obj instanceof String) {// 5
6-
return (String)obj;// 6
5+
if (obj instanceof String s) {// 5
6+
return s;// 6
77
} else {
88
System.out.println("filler");// 9
99
return null;// 11
@@ -30,10 +30,8 @@ class 'pkg/TestPatternMatchingReturn' {
3030
6 4
3131
7 4
3232
8 4
33-
9 5
34-
a 5
35-
b 5
36-
c 5
33+
d 4
34+
e 5
3735
f 5
3836
10 7
3937
11 7

0 commit comments

Comments
 (0)