Skip to content

Commit 04c996e

Browse files
committed
Improve VarType analysis with switch expressions
1 parent 0d65b85 commit 04c996e

File tree

5 files changed

+189
-1
lines changed

5 files changed

+189
-1
lines changed

src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchExprent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class SwitchExprent extends Exprent {
1717
private final SwitchStatement backing;
18-
private final VarType type;
18+
private VarType type;
1919
// TODO: is needed?
2020
private final boolean fallthrough;
2121
// Whether the switch expression returns a value, for case type coercion
@@ -222,6 +222,10 @@ private static boolean isSyntheticThrowEdge(StatEdge edge){
222222
return false;
223223
}
224224

225+
public void setType(VarType type) {
226+
this.type = type;
227+
}
228+
225229
@Override
226230
public int getPrecedence() {
227231
return 1; // Should enclose in case of invocation

src/org/jetbrains/java/decompiler/modules/decompiler/vars/VarTypeProcessor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ else if (newType.typeFamily == TypeFamily.INTEGER) {
182182

183183
case FUNCTION:
184184
return changeFunctionExprentType(newType, minMax, (FunctionExprent)exprent);
185+
case SWITCH:
186+
return changeSwitchExprentType(newType, minMax, (SwitchExprent) exprent);
185187
}
186188

187189
return true;
@@ -237,6 +239,16 @@ private boolean changeFunctionExprentType(VarType newType, int minMax, FunctionE
237239
return true;
238240
}
239241

242+
private boolean changeSwitchExprentType(VarType newType, int minMax, SwitchExprent switchExpr) {
243+
if (minMax == 1) { // max
244+
VarType type = switchExpr.getExprType();
245+
if (newType.typeFamily == TypeFamily.INTEGER && type.typeFamily == newType.typeFamily) {
246+
switchExpr.setType(newType);
247+
}
248+
}
249+
return true;
250+
}
251+
240252
public Map<VarVersionPair, VarType> getMapExprentMaxTypes() {
241253
return mapExprentMaxTypes;
242254
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,8 @@ private void registerDefault() {
715715
register(JAVA_8, "TestCatchVariable");
716716
register(JAVA_8, "TestExtraneousImports");
717717
register(JAVA_17, "TestSwitchOnEnumFake");
718+
register(JAVA_16, "TestSwitchExpressionReturnType");
719+
718720
}
719721

720722
private void registerEntireClassPath() {
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package pkg;
2+
3+
public class TestSwitchExpressionReturnType {
4+
public int asInt(TestSwitchExpressionReturnType.Type type) {
5+
return switch (type) {// 5
6+
case A -> 6000;// 6
7+
case B -> 6001;// 7
8+
case C -> 6002;// 8
9+
case D -> 6003;// 9
10+
};
11+
}
12+
13+
public char asChar(TestSwitchExpressionReturnType.Type type) {
14+
return switch (type) {// 14
15+
case A -> 'ᝰ';// 15
16+
case B -> '\u1771';// 16
17+
case C -> 'ᝲ';// 17
18+
case D -> 'ᝳ';// 18
19+
};
20+
}
21+
22+
static enum Type {
23+
A,
24+
B,
25+
C,
26+
D;
27+
}
28+
}
29+
30+
class 'pkg/TestSwitchExpressionReturnType' {
31+
method 'asInt (Lpkg/TestSwitchExpressionReturnType$Type;)I' {
32+
3 4
33+
7 4
34+
8 4
35+
9 4
36+
a 4
37+
b 4
38+
c 4
39+
d 4
40+
e 4
41+
f 4
42+
10 4
43+
11 4
44+
12 4
45+
13 4
46+
14 4
47+
15 4
48+
16 4
49+
17 4
50+
18 4
51+
19 4
52+
1a 4
53+
1b 4
54+
1c 4
55+
1d 4
56+
1e 4
57+
1f 4
58+
20 4
59+
21 4
60+
22 4
61+
23 4
62+
24 4
63+
25 4
64+
26 4
65+
27 4
66+
28 5
67+
29 5
68+
2a 5
69+
2e 6
70+
2f 6
71+
30 6
72+
34 7
73+
35 7
74+
36 7
75+
3a 8
76+
3b 8
77+
3c 8
78+
48 4
79+
}
80+
81+
method 'asChar (Lpkg/TestSwitchExpressionReturnType$Type;)C' {
82+
3 13
83+
7 13
84+
8 13
85+
9 13
86+
a 13
87+
b 13
88+
c 13
89+
d 13
90+
e 13
91+
f 13
92+
10 13
93+
11 13
94+
12 13
95+
13 13
96+
14 13
97+
15 13
98+
16 13
99+
17 13
100+
18 13
101+
19 13
102+
1a 13
103+
1b 13
104+
1c 13
105+
1d 13
106+
1e 13
107+
1f 13
108+
20 13
109+
21 13
110+
22 13
111+
23 13
112+
24 13
113+
25 13
114+
26 13
115+
27 13
116+
28 14
117+
29 14
118+
2a 14
119+
2e 15
120+
2f 15
121+
30 15
122+
34 16
123+
35 16
124+
36 16
125+
3a 17
126+
3b 17
127+
3c 17
128+
48 13
129+
}
130+
}
131+
132+
Lines mapping:
133+
5 <-> 5
134+
6 <-> 6
135+
7 <-> 7
136+
8 <-> 8
137+
9 <-> 9
138+
14 <-> 14
139+
15 <-> 15
140+
16 <-> 16
141+
17 <-> 17
142+
18 <-> 18
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pkg;
2+
3+
public class TestSwitchExpressionReturnType {
4+
public int asInt(Type type) {
5+
return switch (type) {
6+
case A -> 6000;
7+
case B -> 6001;
8+
case C -> 6002;
9+
case D -> 6003;
10+
};
11+
}
12+
13+
public char asChar(Type type) {
14+
return switch (type) {
15+
case A -> 6000;
16+
case B -> 6001;
17+
case C -> 6002;
18+
case D -> 6003;
19+
};
20+
}
21+
22+
enum Type {
23+
A,
24+
B,
25+
C,
26+
D
27+
}
28+
}

0 commit comments

Comments
 (0)