Skip to content

Commit

Permalink
Fix switch on enum false positive, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Jan 10, 2025
1 parent e67b23a commit 947c0a4
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ private static boolean isEnumArray(Exprent exprent) {
ClassesProcessor.ClassNode classNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(field.getClassname());

if (classNode == null || !"[I".equals(field.getDescriptor().descriptorString)) {
// TODO: tighten up this check to avoid false positives
return field.getName().startsWith("$SwitchMap") || //This is non-standard but we don't have any more information so..
(index instanceof InvocationExprent && ((InvocationExprent) index).getName().equals("ordinal"));
(index instanceof InvocationExprent && ((InvocationExprent) index).getName().equals("ordinal")) && field.isStatic();
}

StructField stField;
Expand Down
2 changes: 2 additions & 0 deletions test/org/jetbrains/java/decompiler/SingleClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ private void registerDefault() {
register(JAVA_8, "TestLVTReassignment");
register(JAVA_8, "TestCatchVariable");
register(JAVA_8, "TestExtraneousImports");
register(JAVA_17, "TestSwitchOnEnumFake");
}

private void registerEntireClassPath() {
Expand Down Expand Up @@ -829,6 +830,7 @@ private void registerPatternMatching() {
// TODO: local variables aren't merged properly, bring out of nodebug when they are
register(JAVA_16_NODEBUG, "TestPatternMatchingAssign");
register(JAVA_16, "TestPatternMatchingLocalCapture");
register(JAVA_16, "TestPatternMatchingReturn");

register(JAVA_17, "TestPatternMatching17");
register(JAVA_17, "TestPatternMatching17Fake");
Expand Down
84 changes: 84 additions & 0 deletions testData/results/pkg/TestPatternMatchingReturn.dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package pkg;

public class TestPatternMatchingReturn {
public String getPattern(Object obj) {
if (obj instanceof String) {// 5
return (String)obj;// 6
} else {
System.out.println("filler");// 9
return null;// 11
}
}

public String get(Object obj) {
if (obj instanceof String) {// 15
return (String)obj;// 16
} else {
System.out.println("filler");// 19
return null;// 21
}
}
}

class 'pkg/TestPatternMatchingReturn' {
method 'getPattern (Ljava/lang/Object;)Ljava/lang/String;' {
0 4
2 4
3 4
4 4
5 4
6 4
7 4
8 4
9 5
a 5
b 5
c 5
f 5
10 7
11 7
12 7
13 7
14 7
15 7
16 7
17 7
18 8
19 8
}

method 'get (Ljava/lang/Object;)Ljava/lang/String;' {
0 13
1 13
2 13
3 13
4 13
5 13
6 13
7 14
8 14
9 14
a 14
b 14
c 16
d 16
e 16
f 16
10 16
11 16
12 16
13 16
14 17
15 17
}
}

Lines mapping:
5 <-> 5
6 <-> 6
9 <-> 8
11 <-> 9
15 <-> 14
16 <-> 15
19 <-> 17
21 <-> 18
124 changes: 124 additions & 0 deletions testData/results/pkg/TestSwitchOnEnumFake.dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package pkg;

public class TestSwitchOnEnumFake {
public byte[] values = new byte[TestSwitchOnEnumFake.Values.values().length];

public int test(TestSwitchOnEnumFake.Values v) {
int a = 0;// 14
int b = 0;// 15
byte var4;
byte var5;
switch (this.values[v.ordinal()]) {// 16
case 1:
var4 = 1;// 18
var5 = 2;// 19
break;// 20
case 2:
var4 = 2;// 22
var5 = 4;// 23
break;// 24
case 3:
var4 = 3;// 26
var5 = 6;// 27
break;// 28
default:
var4 = 1;// 30
var5 = 1;// 31
}

return var4 + var5;// 35
}

static enum Values {
A,
B,
C,
D;
}
}

class 'pkg/TestSwitchOnEnumFake' {
method 'test (Lpkg/TestSwitchOnEnumFake$Values;)I' {
0 6
1 6
2 7
3 7
4 10
5 10
6 10
7 10
8 10
9 10
a 10
b 10
c 10
d 10
e 10
f 10
10 10
11 10
12 10
13 10
14 10
15 10
16 10
17 10
18 10
19 10
1a 10
1b 10
1c 10
1d 10
1e 10
1f 10
20 10
21 10
22 10
23 10
24 10
25 10
26 10
27 10
28 12
29 12
2a 13
2b 13
2c 14
2f 16
30 16
31 17
32 17
33 18
36 20
37 20
38 21
39 21
3a 21
3b 22
3e 24
3f 24
40 25
41 25
42 28
43 28
44 28
45 28
}
}

Lines mapping:
14 <-> 7
15 <-> 8
16 <-> 11
18 <-> 13
19 <-> 14
20 <-> 15
22 <-> 17
23 <-> 18
24 <-> 19
26 <-> 21
27 <-> 22
28 <-> 23
30 <-> 25
31 <-> 26
35 <-> 29
23 changes: 23 additions & 0 deletions testData/src/java16/pkg/TestPatternMatchingReturn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pkg;

public class TestPatternMatchingReturn {
public String getPattern(Object obj) {
if (obj instanceof String s) {
return s;
}

System.out.println("filler");

return null;
}

public String get(Object obj) {
if (obj instanceof String) {
return (String) obj;
}

System.out.println("filler");

return null;
}
}
37 changes: 37 additions & 0 deletions testData/src/java17/pkg/TestSwitchOnEnumFake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pkg;

public class TestSwitchOnEnumFake {
enum Values {
A,
B,
C,
D
}

public byte[] values = new byte[Values.values().length];

public int test(Values v) {
int a = 0;
int b = 0;
switch (values[v.ordinal()]) {
case 1 -> {
a = 1;
b = 2;
}
case 2 -> {
a = 2;
b = 4;
}
case 3 -> {
a = 3;
b = 6;
}
default -> {
a = 1;
b = 1;
}
}

return a + b;
}
}

0 comments on commit 947c0a4

Please sign in to comment.