Skip to content

Commit 947c0a4

Browse files
committed
Fix switch on enum false positive, add more tests
1 parent e67b23a commit 947c0a4

File tree

6 files changed

+272
-1
lines changed

6 files changed

+272
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,9 @@ private static boolean isEnumArray(Exprent exprent) {
399399
ClassesProcessor.ClassNode classNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(field.getClassname());
400400

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

406407
StructField stField;

test/org/jetbrains/java/decompiler/SingleClassesTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ private void registerDefault() {
714714
register(JAVA_8, "TestLVTReassignment");
715715
register(JAVA_8, "TestCatchVariable");
716716
register(JAVA_8, "TestExtraneousImports");
717+
register(JAVA_17, "TestSwitchOnEnumFake");
717718
}
718719

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

833835
register(JAVA_17, "TestPatternMatching17");
834836
register(JAVA_17, "TestPatternMatching17Fake");
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package pkg;
2+
3+
public class TestPatternMatchingReturn {
4+
public String getPattern(Object obj) {
5+
if (obj instanceof String) {// 5
6+
return (String)obj;// 6
7+
} else {
8+
System.out.println("filler");// 9
9+
return null;// 11
10+
}
11+
}
12+
13+
public String get(Object obj) {
14+
if (obj instanceof String) {// 15
15+
return (String)obj;// 16
16+
} else {
17+
System.out.println("filler");// 19
18+
return null;// 21
19+
}
20+
}
21+
}
22+
23+
class 'pkg/TestPatternMatchingReturn' {
24+
method 'getPattern (Ljava/lang/Object;)Ljava/lang/String;' {
25+
0 4
26+
2 4
27+
3 4
28+
4 4
29+
5 4
30+
6 4
31+
7 4
32+
8 4
33+
9 5
34+
a 5
35+
b 5
36+
c 5
37+
f 5
38+
10 7
39+
11 7
40+
12 7
41+
13 7
42+
14 7
43+
15 7
44+
16 7
45+
17 7
46+
18 8
47+
19 8
48+
}
49+
50+
method 'get (Ljava/lang/Object;)Ljava/lang/String;' {
51+
0 13
52+
1 13
53+
2 13
54+
3 13
55+
4 13
56+
5 13
57+
6 13
58+
7 14
59+
8 14
60+
9 14
61+
a 14
62+
b 14
63+
c 16
64+
d 16
65+
e 16
66+
f 16
67+
10 16
68+
11 16
69+
12 16
70+
13 16
71+
14 17
72+
15 17
73+
}
74+
}
75+
76+
Lines mapping:
77+
5 <-> 5
78+
6 <-> 6
79+
9 <-> 8
80+
11 <-> 9
81+
15 <-> 14
82+
16 <-> 15
83+
19 <-> 17
84+
21 <-> 18
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package pkg;
2+
3+
public class TestSwitchOnEnumFake {
4+
public byte[] values = new byte[TestSwitchOnEnumFake.Values.values().length];
5+
6+
public int test(TestSwitchOnEnumFake.Values v) {
7+
int a = 0;// 14
8+
int b = 0;// 15
9+
byte var4;
10+
byte var5;
11+
switch (this.values[v.ordinal()]) {// 16
12+
case 1:
13+
var4 = 1;// 18
14+
var5 = 2;// 19
15+
break;// 20
16+
case 2:
17+
var4 = 2;// 22
18+
var5 = 4;// 23
19+
break;// 24
20+
case 3:
21+
var4 = 3;// 26
22+
var5 = 6;// 27
23+
break;// 28
24+
default:
25+
var4 = 1;// 30
26+
var5 = 1;// 31
27+
}
28+
29+
return var4 + var5;// 35
30+
}
31+
32+
static enum Values {
33+
A,
34+
B,
35+
C,
36+
D;
37+
}
38+
}
39+
40+
class 'pkg/TestSwitchOnEnumFake' {
41+
method 'test (Lpkg/TestSwitchOnEnumFake$Values;)I' {
42+
0 6
43+
1 6
44+
2 7
45+
3 7
46+
4 10
47+
5 10
48+
6 10
49+
7 10
50+
8 10
51+
9 10
52+
a 10
53+
b 10
54+
c 10
55+
d 10
56+
e 10
57+
f 10
58+
10 10
59+
11 10
60+
12 10
61+
13 10
62+
14 10
63+
15 10
64+
16 10
65+
17 10
66+
18 10
67+
19 10
68+
1a 10
69+
1b 10
70+
1c 10
71+
1d 10
72+
1e 10
73+
1f 10
74+
20 10
75+
21 10
76+
22 10
77+
23 10
78+
24 10
79+
25 10
80+
26 10
81+
27 10
82+
28 12
83+
29 12
84+
2a 13
85+
2b 13
86+
2c 14
87+
2f 16
88+
30 16
89+
31 17
90+
32 17
91+
33 18
92+
36 20
93+
37 20
94+
38 21
95+
39 21
96+
3a 21
97+
3b 22
98+
3e 24
99+
3f 24
100+
40 25
101+
41 25
102+
42 28
103+
43 28
104+
44 28
105+
45 28
106+
}
107+
}
108+
109+
Lines mapping:
110+
14 <-> 7
111+
15 <-> 8
112+
16 <-> 11
113+
18 <-> 13
114+
19 <-> 14
115+
20 <-> 15
116+
22 <-> 17
117+
23 <-> 18
118+
24 <-> 19
119+
26 <-> 21
120+
27 <-> 22
121+
28 <-> 23
122+
30 <-> 25
123+
31 <-> 26
124+
35 <-> 29
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package pkg;
2+
3+
public class TestPatternMatchingReturn {
4+
public String getPattern(Object obj) {
5+
if (obj instanceof String s) {
6+
return s;
7+
}
8+
9+
System.out.println("filler");
10+
11+
return null;
12+
}
13+
14+
public String get(Object obj) {
15+
if (obj instanceof String) {
16+
return (String) obj;
17+
}
18+
19+
System.out.println("filler");
20+
21+
return null;
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package pkg;
2+
3+
public class TestSwitchOnEnumFake {
4+
enum Values {
5+
A,
6+
B,
7+
C,
8+
D
9+
}
10+
11+
public byte[] values = new byte[Values.values().length];
12+
13+
public int test(Values v) {
14+
int a = 0;
15+
int b = 0;
16+
switch (values[v.ordinal()]) {
17+
case 1 -> {
18+
a = 1;
19+
b = 2;
20+
}
21+
case 2 -> {
22+
a = 2;
23+
b = 4;
24+
}
25+
case 3 -> {
26+
a = 3;
27+
b = 6;
28+
}
29+
default -> {
30+
a = 1;
31+
b = 1;
32+
}
33+
}
34+
35+
return a + b;
36+
}
37+
}

0 commit comments

Comments
 (0)