ddc-dec 0.1.11

DEX → Java decompiler front-end over jdc-core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
//! Class-level rendering: header, fields (with static initializers),
//! constructors, methods and nested member classes.

use jdc_core::emit::{escape_string, format_float, Printer};
use jdc_core::types::JavaType;
use jdc_core::Ctx;

use crate::access::*;
use crate::PoolField;
use crate::ctx::{java_type_to_generic, DexCtx};
use crate::method::decompile_method;
use crate::{desc_type, DexPool, PoolClass, PoolMethod, StaticValue};
use ddc_dex::insn::InsnKind;

#[derive(Debug, Clone)]
pub struct ClassOptions {
    /// Prefix each file with a provenance comment.
    pub provenance: bool,
}

impl Default for ClassOptions {
    fn default() -> Self {
        ClassOptions { provenance: true }
    }
}

/// Decompile one class (plus nested member classes) to a Java source file.
///
/// Classes with large method bodies run in a monitored thread with a
/// deadline: pathological CFGs can drive the shared structurer's walk into
/// an exponential exploration that never returns. On timeout the class is
/// reported failed and the thread is abandoned (reaped at process exit).
/// One registered monitored decompile: the receiver the worker polls at
/// the tail, the class name (for diagnostics), and the deadline counted
/// from the spawn.
pub type PendingMonitor = (
    std::sync::mpsc::Receiver<Result<String, String>>,
    String,
    std::time::Instant,
);

pub fn decompile_class(
    pool: &std::sync::Arc<DexPool>,
    class: &PoolClass,
    opts: &ClassOptions,
    pending: &std::sync::Mutex<Vec<PendingMonitor>>,
) -> anyhow::Result<String> {
    if class_is_risky(pool, class) {
        // Detached monitored thread: the CALLER registers the receiver and
        // moves on (awaiting happens at the end of the run) — a spinning
        // pathological method no longer stalls its worker.
        let pool2 = pool.clone();
        let cls = class.clone();
        let opts2 = opts.clone();
        let (tx, rx) = std::sync::mpsc::channel::<Result<String, String>>();
        let name = class.name.clone();
        // 10s: pure insurance — the structurer's walk budgets and the
        // ladder giant short-circuit bound pathological methods
        // deterministically now (weixin cdp/l1: 4.2s single-class), but
        // full-run worker contention can ~2× that; the old 5s dropped
        // l1's file entirely (every referrer cannot-finds).
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        let _ = std::thread::Builder::new()
            .stack_size(64 * 1024 * 1024)
            .spawn(move || {
                let r = decompile_class_impl(&pool2, &cls, &opts2).map_err(|e| format!("{:#}", e));
                let _ = tx.send(r);
            });
        pending.lock().unwrap().push((rx, name, deadline));
        return Err(anyhow::anyhow!(
            "deferred to monitored thread (pathological-CFG guard)"
        ));
    }
    decompile_class_impl(pool, class, opts)
}

/// Any method in the exponential-walk danger zone → run under the
/// deadline. Calibrated between real workloads: routine R8 classes top out
/// near 11k insns (reqable's biggest `<clinit>`); the exponential hangs
/// (weibo's gson TypeAdapters) sit at 24k+.
fn class_is_risky(pool: &DexPool, class: &PoolClass) -> bool {
    // Header peek only — decoding every body here would double the APK's
    // total decode work.
    for m in class.all_methods() {
        if m.code_off == 0 {
            continue;
        }
        if let Some(dex) = pool.dex(m.dex_idx) {
            if let Some((_regs, insns)) = dex.code_stats(m.code_off) {
                if insns > 16_000 {
                    return true;
                }
            }
        }
    }
    false
}

fn decompile_class_impl(
    pool: &DexPool,
    class: &PoolClass,
    opts: &ClassOptions,
) -> anyhow::Result<String> {
    let ctx = DexCtx::new(pool, class);
    // Size the class buffer up front: corpus-average classes render to
    // ~1KB per method — without the reserve the String doubles through
    // 4-6 realloc+copy rounds per class (~2× the final size in memmove).
    // Cap the reserve: weixin's monster classes (hundreds of methods)
    // would reserve megabytes of untouched capacity per class — large
    // blocks take mimalloc's commit/purge path (madvise churn showed up
    // in profiles). 256KB covers the corpus-average class dozens of
    // times over; bigger classes pay a few extra doublings.
    let mut out = String::with_capacity(
        (class.all_methods().count() * 1024).min(256 * 1024) + 512,
    );
    if opts.provenance {
        static BANNER: std::sync::OnceLock<String> = std::sync::OnceLock::new();
        out.push_str(BANNER.get_or_init(|| {
            format!(
                "// Decompiled by https://github.com/ejfkdev/ddc {}\n",
                env!("CARGO_PKG_VERSION")
            )
        }));
        // Provenance: which input image this class was lifted from (jadx's
        // `loaded from: classes.dex` pattern). Byte-stable across runs —
        // deliberately NO timestamp so outputs diff cleanly. Direct
        // push_str: the per-class format! temporaries were 3 allocations
        // × every class in the corpus.
        if let Some(dex) = pool.dex(class.dex_idx) {
            if let Some(label) = pool.dex_labels.get(class.dex_idx) {
                out.push_str("// From: ");
                out.push_str(label);
                out.push_str(" (DEX ");
                out.push_str(&dex.version);
                out.push_str(")\n");
            }
        }
    }
    if let Some(src) = &class.source_file {
        out.push_str("// Source file: ");
        out.push_str(src);
        out.push('\n');
    }
    if class.is_synthetic() {
        out.push_str("// synthetic\n");
    }
    let (pkg, _) = split_name(&class.name);
    // Two-pass import assembly: the body renders FIRST (into a temp
    // buffer) with the obscured render state installed — expression
    // positions DISCOVER additional obscured refs on the fly — then the
    // file assembles as provenance + package + imports + body. The one
    // extra body copy is the price of correct import placement.
    let mut obscured_map = compute_obscured_renders(pool, class);
    // Same-package simple-name collisions: an import shadows every
    // same-package use of that simple in this file — drop those from
    // the map (their refs stay qualified, erroring honestly).
    let blocked: jdc_core::FxHashSet<String> = pool
        .package_simples()
        .get(&pkg)
        .cloned()
        .unwrap_or_default();
    obscured_map.retain(|_internal, simple| !blocked.contains(simple));
    set_obscured_state(class.name.clone(), obscured_map.clone(), blocked.clone());
    let mut body_buf = String::with_capacity(out.capacity() / 2);
    let body_res = emit_class_body(pool, class, &ctx, opts, &mut body_buf, 0);
    let recorded = take_recorded_and_clear();
    body_res?;
    if !pkg.is_empty() {
        out.push('\n');
        out.push_str("package ");
        let dotted = pkg.replace('/', ".");
        out.push_str(&sanitize_fq(&dotted));
        out.push_str(";\n");
        // Imports: the metadata map plus the expression-level
        // discoveries, pool-known only (an import of an unknown class
        // is a hard error), display-renamed, sorted for determinism.
        let mut import_set: jdc_core::FxHashSet<String> =
            obscured_map.keys().cloned().collect();
        for r in recorded {
            if pool.get(&r).is_some() {
                let simple = r.rsplit('/').next().unwrap_or("");
                if !blocked.contains(simple) {
                    import_set.insert(r);
                }
            }
        }
        let mut imports: Vec<String> = import_set
            .iter()
            .map(|internal| crate::classdec::dotted(&crate::apply_class_rename(internal)))
            .collect();
        imports.sort();
        for display in imports {
            out.push_str(&format!("import {};\n", display));
        }
    }
    out.push('\n');
    out.push_str(&body_buf);
    Ok(out)
}

/// Render the class header, fields, methods and nested member classes into
/// `out` (used at top level and recursively for nested members).
// `opts` is only consumed by the nested-member recursion below — that
// is its purpose (propagating emission options into inline children).
/// One enum constant: the source identifier plus constructor
/// arguments beyond the compiler-mandated `(String name, int ordinal)`.
use jdc_core::ir::expr::{ConstVal, Expr};
use jdc_core::ir::stmt::Stmt;
struct EnumConst {
    #[allow(dead_code)]
    field: String,
    name: String,
    extra_args: Vec<Expr>,
}

/// Collect enum constants for a true `enum` rendering. Every ACC_ENUM
/// static field must be initialized in `<clinit>` by
/// `Self.field = new Self("NAME", ordinal, ...)` — the shape javac/d8
/// always emit. Returns None (caller falls back to the desugared
/// `/* enum */ class` form) when anything is missing: R8 variance,
/// constant-specific bodies (the field holds an anonymous subclass), or
/// a <clinit> that failed to decompile.
fn collect_enum_constants(
    pool: &DexPool,
    class: &PoolClass,
    ctx: &DexCtx<'_>,
) -> Option<(Vec<EnumConst>, crate::method::MethodBody)> {
    let _ = ctx;
    let const_fields: Vec<&PoolField> = class
        .static_fields
        .iter()
        .filter(|f| f.access & crate::access::ACC_ENUM != 0)
        .collect();
    // No ACC_ENUM constant fields: R8 drops them ALL when nothing
    // outside reads them (weixin sp2/ji — only the synthetic $VALUES
    // array survives). An ACC_ENUM CLASS still promotes: the synthetic-
    // constant scan below lifts the constants out of the array literal.
    // Non-enum classes have no business here.
    if const_fields.is_empty() && class.access & crate::access::ACC_ENUM == 0 {
        return None;
    }
    let clinit = class.all_methods().find(|m| &*m.name == "<clinit>")?;
    let mut body = decompile_method(pool, class, clinit).ok().flatten()?;

    // R8/d8 split the constant build across an intermediate local:
    //   Self v0 = new Self("NAME", i, ...);
    //   a = v0;                       // sput to the ACC_ENUM field
    //   Self[] v5 = new Self[n]; v5[k] = v0; ...; $VALUES = v5;
    // Pass 1 registers each intermediate (or direct) new; pass 2 binds
    // them to the ACC_ENUM fields; references to the locals are then
    // rewritten into constant identifiers so the $VALUES build keeps
    // compiling once the definitions drop.
    use std::collections::HashMap;
    let mut const_name: Vec<String> = Vec::new();
    let mut const_field: Vec<String> = Vec::new();
    let mut const_extra: Vec<Vec<Expr>> = Vec::new();
    // Declared ordinal per constant (the ctor's int arg) — the merge at
    // the end orders field-bound and synthetic constants by it and
    // demands a dense 0..n run (Java derives ordinals from declaration
    // position).
    let mut const_ord: Vec<i64> = Vec::new();
    let mut var_of: HashMap<u32, usize> = HashMap::default(); // local id -> const idx
    let mut drop_stmts: Vec<usize> = Vec::new();

    // Collect (immutable borrows) first; the mutable passes come after.
    if !matches!(&body.body, Stmt::Block(_)) { return None; }

    // Pass 1: definitions. (immutable borrow; rewrite comes later)
    // Rolling reaching-defs of clinit locals for resolving enum-ctor
    // extra args (resolve_enum_arg); `tainted` flips at the first
    // non-straight-line statement — past it, linear-scan defs are no
    // longer provable and Local args reject the enum mode.
    let mut defs: HashMap<u32, &Expr> = HashMap::default();
    let mut tainted = false;
    let self_name: std::sync::Arc<str> = class.name.as_str().into();
    let self_ty =
        jdc_core::ir::expr::TypeRef::J(JavaType::Object(class.name.as_str().into()));
    // A REUSED intermediate local: `v = new Self(..); A = v; v = new
    // Self(..); B = v;` — the first def is a LocalDef, the rest are
    // Assigns to the same var. Register every def site; pass 2 resolves
    // a local sput to the def site CURRENT at that statement (rolling),
    // not to the var (Kotlin EnumEntries enums, RegexOption: 8→7 count
    // mismatch used to abort the whole promotion).
    let mut def_site: HashMap<usize, (u32, usize)> = HashMap::default();
    for (i, st) in match &body.body {
        Stmt::Block(v) => v.iter().enumerate(),
        _ => return None,
    } {
        // (var, New) for both def shapes: LocalDef and Assign-to-local.
        let def_shape: Option<(u32, &Expr)> = match st {
            Stmt::LocalDef {
                var,
                init: Some(e),
                ..
            } => Some((*var, e)),
            Stmt::ExprStmt(Expr::Assign { target, value, .. }) => {
                match (&**target, &**value) {
                    (Expr::Local { var, .. }, e) => Some((*var, e)),
                    _ => None,
                }
            }
            _ => None,
        };
        if let Some((var, Expr::New { cls: ncls, args, .. })) = def_shape {
            if ncls.as_ref() == class.name && args.len() >= 2 {
                if let (Expr::Const(ConstVal::Str(n)), Expr::Const(ConstVal::Int(ord0))) =
                    (&args[0], &args[1])
                {
                    if java_ident(n).as_ref() != &**n || n.is_empty() { return None; }
                    var_of.insert(var, const_name.len());
                    def_site.insert(i, (var, const_name.len()));
                    const_ord.push(*ord0 as i64);
                    const_name.push(n.to_string());
                    const_field.push(String::new());
                    const_extra.push(resolve_enum_extras(
                        &args[2..],
                        &defs,
                        tainted,
                        &var_of,
                        &const_name,
                        &self_name,
                        &self_ty,
                    )?);
                    drop_stmts.push(i);
                }
            }
        }
        track_def(st, &mut defs, &mut tainted);
    }

    // Pass 2: sputs to the ACC_ENUM fields (direct new or intermediate).
    // Local sputs resolve through the ROLLING def map (updated at each
    // def-site statement) — a reused `v` must bind to the constant
    // defined most recently, not to the var's first registration.
    let mut defs2: HashMap<u32, &Expr> = HashMap::default();
    let mut tainted2 = false;
    let mut cur_def: HashMap<u32, usize> = HashMap::default();
    for (i, st) in match &body.body {
        Stmt::Block(v) => v.iter().enumerate(),
        _ => return None,
    } {
        if let Some((var, idx)) = def_site.get(&i) {
            cur_def.insert(*var, *idx);
        }
        if let Stmt::ExprStmt(Expr::Assign { target, value, .. }) = st {
            if let Expr::Field {
                cls,
                name: fname,
                is_static: true,
                ..
            } = &**target
            {
                if cls.as_ref() != class.name {
                    continue;
                }
                if !const_fields.iter().any(|f| f.name.as_str() == &**fname) {
                    continue;
                }
                if const_field.iter().any(|f| !f.is_empty() && f == &**fname) { { return None; } // duplicate assignment
                }
                let idx = match &**value {
                    Expr::Local { var, .. } => cur_def.get(var).copied()?,
                    Expr::New { cls: ncls, args, .. }
                        if ncls.as_ref() == class.name && args.len() >= 2 =>
                    {
                        if let (Expr::Const(ConstVal::Str(n)), Expr::Const(ConstVal::Int(ord0))) =
                            (&args[0], &args[1])
                        {
                            if java_ident(n).as_ref() != &**n || n.is_empty() { return None; }
                            let idx = const_name.len();
                            const_ord.push(*ord0 as i64);
                            const_name.push(n.to_string());
                            const_field.push(String::new());
                            const_extra.push(resolve_enum_extras(
                                &args[2..],
                                &defs2,
                                tainted2,
                                &var_of,
                                &const_name,
                                &self_name,
                                &self_ty,
                            )?);
                            idx
                        } else {
                            { return None; }
                        }
                    }
                    _ => return None,
                };
                const_field[idx] = fname.to_string();
                drop_stmts.push(i);
            }
        }
        track_def(st, &mut defs2, &mut tainted2);
    }
    // Own the reaching-def values: defs2 borrows body.body, and the
    // synthetic-constant scan below mutates the $VALUES array in place.
    let defs2_owned: std::collections::HashMap<u32, Expr> = defs2
        .iter()
        .map(|(k, v)| (*k, (*v).clone()))
        .collect();
    drop(defs2);
    let defs2: std::collections::HashMap<u32, &Expr> =
        defs2_owned.iter().map(|(k, v)| (*k, v)).collect();

    // Every ACC_ENUM field bound, every intermediate matched.
    // Every ACC_ENUM FIELD must be bound to a constant. The converse
    // need not hold: R8 drops the FIELD of a constant nothing reads
    // externally while the $VALUES array keeps it (weixin lite/api/n's
    // ON_DESTROY — a pass-1 local with no pass-2 sput). Field-less
    // constants keep their ctor-string source name.
    if const_field.iter().filter(|f| !f.is_empty()).count() != const_fields.len() {
               return None;
    }
    // Obfuscated enums rename the ACC_ENUM FIELD (d/e/f) while the ctor's
    // name STRING keeps the source identifier — the promoted constant
    // must be declared under the FIELD name: every reference in the
    // pool resolves through it. Using the name string broke all
    // cross-file references (weixin +2.9k when newly-promoted enums
    // swapped d/e/f for TEXT_ENTER_EDITING/…).
    for i in 0..const_name.len() {
        let field = &const_field[i];
        if !field.is_empty() && field != &const_name[i] {
            let id = java_ident(field);
            if id.is_empty() || id.as_ref() != field.as_str() {
                { return None; }
            }
            const_name[i] = field.clone();
        }
    }
    {
        let mut seen: std::collections::HashSet<&str> = std::collections::HashSet::default();
        if !const_name.iter().all(|c| seen.insert(c.as_str())) { return None; }
    }

    // Synthetic constants: entries built INLINE inside the $VALUES
    // array (`new Self("NAME", ord, ..)` with no ACC_ENUM field — R8
    // drops the field when nothing outside reads it; weixin u2/i's
    // "CONSTANT"). Promote them so the header declares the constant:
    // the raw `new` in the array is "无法实例化枚举类", and every
    // constant after the gap carries a SHIFTED ordinal (e was 2 in the
    // dex, renders as position 1).
    let mut synth: Vec<(i64, String, Vec<Expr>)> = Vec::new();
    {
        let self_cls: &str = class.name.as_str();
        if let Stmt::Block(vs) = &mut body.body {
            for st in vs.iter_mut() {
                crate::passes::walk_stmt_exprs(st, &mut |e| {
                    crate::passes::deep_rewrite(e, &mut |x| {
                        let Expr::NewArray { elem, init: Some(list), .. } = x else {
                            return;
                        };
                        if elem.erased() != JavaType::Object(self_cls.into()) {
                            return;
                        }
                        for slot in list.iter_mut() {
                            let (n, ord, rest) = match slot {
                                Expr::New { cls, args, raw: false, .. }
                                    if cls.as_ref() == self_cls && args.len() >= 2 =>
                                {
                                    match (&args[0], &args[1]) {
                                        (
                                            Expr::Const(ConstVal::Str(sv)),
                                            Expr::Const(ConstVal::Int(o)),
                                        ) => (sv.to_string(), *o as i64, args[2..].to_vec()),
                                        _ => continue,
                                    }
                                }
                                _ => continue,
                            };
                            if java_ident(&n).as_ref() != n.as_str() || n.is_empty() {
                                continue;
                            }
                            if const_name.contains(&n)
                                || synth.iter().any(|(_, s2, _)| *s2 == n)
                            {
                                continue;
                            }
                            let Some(extras) = resolve_enum_extras(
                                &rest,
                                &defs2,
                                tainted2,
                                &var_of,
                                &const_name,
                                &self_name,
                                &self_ty,
                            ) else {
                                continue;
                            };
                            *slot = Expr::Field {
                                owner: None,
                                cls: self_name.clone(),
                                name: std::sync::Arc::from(n.as_str()),
                                ty: self_ty.clone(),
                                is_static: true,
                            };
                            synth.push((ord, n, extras));
                        }
                    });
                });
            }
        }
    }

    // Pass 3: rewrite references to the intermediate locals into the
    // constant identifiers (static-field reads on Self). POSITION-AWARE:
    // a REUSED local must read as the constant defined most recently at
    // that statement — a var-level map made every `arr[k] = v` in the
    // $VALUES build reference the LAST constant (weixin +2.9k when this
    // regressed values() contents).
    {
        let mut cur: HashMap<u32, usize> = HashMap::default();
        if let Stmt::Block(vs) = &mut body.body {
            for (i, st) in vs.iter_mut().enumerate() {
                if let Some((var, idx)) = def_site.get(&i) {
                    cur.insert(*var, *idx);
                }
                crate::passes::walk_stmt_exprs(st, &mut |e| {
                    crate::passes::deep_rewrite(e, &mut |x| {
                        if let Expr::Local { var, .. } = x {
                            if let Some(&idx) = cur.get(var) {
                                let name: std::sync::Arc<str> =
                                    std::sync::Arc::from(const_name[idx].as_str());
                                *x = Expr::Field {
                                    owner: None,
                                    cls: self_name.clone(),
                                    name,
                                    ty: self_ty.clone(),
                                    is_static: true,
                                };
                            }
                        }
                    });
                });
            }
        }
    }

    // Pass 4: drop the definitions and their sputs.
    if let Stmt::Block(vs) = &mut body.body {
        let drop_set: std::collections::HashSet<usize> =
            drop_stmts.iter().copied().collect();
        vs.retain(|_| true);
        let mut idx = 0usize;
        vs.retain(|_| {
            let keep = !drop_set.contains(&idx);
            idx += 1;
            keep
        });
    }
    // The resolved args consumed the clinit locals' readers; prune the
    // now-dead defs from the remnant (impure inits survive as bare
    // expression statements — drop_dead_locals' standard contract).
    crate::passes::drop_dead_locals(&mut body.body);

    let mut merged: Vec<(i64, EnumConst)> = const_ord
        .into_iter()
        .zip(
            const_field
                .into_iter()
                .zip(const_name)
                .zip(const_extra)
                .map(|((field, name), extra_args)| EnumConst {
                    field,
                    name,
                    extra_args,
                }),
        )
        .collect();
    for (ord, name, extra_args) in synth {
        merged.push((
            ord,
            EnumConst {
                field: name.clone(),
                name,
                extra_args,
            },
        ));
    }
    if merged.is_empty() {
        return None; // nothing extracted — keep the desugared form
    }
    merged.sort_by_key(|(o, _)| *o);
    // Ordinals must be UNIQUE and ASCENDING; gaps are legal — R8 drops
    // unused constants wholesale (weixin lite/api/n starts at ordinal
    // 1). Java derives ordinal() from declaration position, so a gap
    // shifts every later constant. Pad each gap with a synthetic
    // constant (`_r<k>`) to keep positions faithful — only when no
    // constant carries extra ctor args (a pad cannot invent them).
    // The synthetic values()/valueOf() the true-enum render suppresses
    // then see the pad (values() drift vs the dex $VALUES array — the
    // compilable-and-ordinal-faithful side of the trade).
    let has_extras = merged.iter().any(|(_, c)| !c.extra_args.is_empty());
    let mut member_taken: jdc_core::FxHashSet<String> = merged
        .iter()
        .map(|(_, c)| c.name.clone())
        .chain(
            class
                .static_fields
                .iter()
                .chain(class.instance_fields.iter())
                .map(|f| f.name.to_string()),
        )
        .chain(class.all_methods().map(|m| m.name.to_string()))
        .collect();
    let mut padded: Vec<EnumConst> = Vec::with_capacity(merged.len());
    let mut expect: i64 = 0;
    let mut pad_k = 0u32;
    for (ord, c) in merged {
        if ord < expect {
            { return None; } // duplicate/colliding ordinals — unfaithful
        }
        if ord > expect {
            if has_extras {
                { return None; } // cannot synthesize the missing ctor args
            }
            while expect < ord {
                let pname = loop {
                    let cand = format!("_r{pad_k}");
                    pad_k += 1;
                    if !member_taken.contains(&cand) {
                        member_taken.insert(cand.clone());
                        break cand;
                    }
                };
                padded.push(EnumConst {
                    field: pname.clone(),
                    name: pname,
                    extra_args: Vec::new(),
                });
                expect += 1;
            }
        }
        expect = ord + 1;
        padded.push(c);
    }
    let out = padded;
    Some((out, body))
}

/// Update the rolling reaching-def map for enum-arg resolution. Only
/// flat single-target definitions keep the tracking sound; any control
/// flow taints it (a linear scan can no longer prove WHICH definition
/// reaches later capture sites).
fn track_def<'e>(
    st: &'e Stmt,
    defs: &mut std::collections::HashMap<u32, &'e Expr>,
    tainted: &mut bool,
) {
    match st {
        Stmt::LocalDef { var, init, .. } => match init {
            Some(e) => {
                defs.insert(*var, e);
            }
            None => {
                defs.remove(var);
            }
        },
        Stmt::ExprStmt(Expr::Assign { target, value, op, .. }) => {
            if let Expr::Local { var, .. } = &**target {
                if matches!(op, jdc_core::ir::expr::AssignOp::Plain) {
                    defs.insert(*var, value);
                } else {
                    defs.remove(var);
                }
            }
        }
        // Plain expression statements (calls) don't define locals.
        Stmt::ExprStmt(_) => {}
        _ => *tainted = true,
    }
}

/// Resolve every enum-ctor extra arg to a self-contained expression, or
/// reject the enum mode (None). R8 reuses ONE register across all
/// constant constructions — revenuecat's LogIntent builds 11 of 12
/// emoji lists through the same `list` local, reassigned between the
/// `new Self(.., list)` sites — and enum constants render OUTSIDE the
/// clinit where no local is in scope: an unresolved `Local` used to
/// print as the vt-dummy name (`DEBUG(var0)` — per-constant
/// cannot-find). Each Local is replaced by its reaching pure definition
/// (recursively) or, when it names an earlier constant's intermediate,
/// by a static-field reference to that constant. Any expression shape
/// WITHOUT locals passes through untouched (enum args may be arbitrary
/// expressions — BinOp/Cast/Method/New — each renders exactly once per
/// constant, so no duplication concern applies). Only an unresolvable
/// Local (missing/tainted def, depth > 4) rejects the whole enum
/// detection; the class then falls back to plain-field rendering, which
/// always compiles.
fn resolve_enum_extras(
    extras: &[Expr],
    defs: &std::collections::HashMap<u32, &Expr>,
    tainted: bool,
    var_of: &std::collections::HashMap<u32, usize>,
    const_name: &[String],
    self_name: &std::sync::Arc<str>,
    self_ty: &jdc_core::ir::expr::TypeRef,
) -> Option<Vec<Expr>> {
    extras
        .iter()
        .map(|e| {
            let mut out = e.clone();
            let mut fail = false;
            resolve_locals_in(
                &mut out, defs, tainted, var_of, const_name, self_name, self_ty, 0,
                &mut fail,
            );
            if fail {
                None
            } else {
                Some(out)
            }
        })
        .collect()
}

#[allow(clippy::too_many_arguments)]
fn resolve_locals_in(
    e: &mut Expr,
    defs: &std::collections::HashMap<u32, &Expr>,
    tainted: bool,
    var_of: &std::collections::HashMap<u32, usize>,
    const_name: &[String],
    self_name: &std::sync::Arc<str>,
    self_ty: &jdc_core::ir::expr::TypeRef,
    depth: u32,
    fail: &mut bool,
) {
    crate::passes::deep_rewrite(e, &mut |x| {
        if let Expr::Local { var, .. } = x {
            // An earlier constant's intermediate: a static-field read of
            // that constant (declared above — backward reference, legal
            // in enum ctor args).
            if let Some(&ci) = var_of.get(var) {
                if let Some(nm) = const_name.get(ci) {
                    *x = Expr::Field {
                        owner: None,
                        cls: self_name.clone(),
                        name: std::sync::Arc::from(nm.as_str()),
                        ty: self_ty.clone(),
                        is_static: true,
                    };
                    return;
                }
            }
            if tainted || depth > 4 {
                *fail = true;
                return;
            }
            let Some(d) = defs.get(var).copied() else {
                *fail = true;
                return;
            };
            let mut sub = d.clone();
            resolve_locals_in(
                &mut sub, defs, tainted, var_of, const_name, self_name, self_ty,
                depth + 1, fail,
            );
            if *fail {
                return;
            }
            *x = sub;
        }
    });
}

/// Render enum-constant constructor arguments via the shared expression
/// emitter (the VarTable is irrelevant for argument printing — no local
/// names appear — but the API requires one).
fn render_enum_args(ctx: &DexCtx<'_>, pool: &DexPool, args: &[Expr], out: &mut String) {
    let dummy_vt = jdc_core::var::VarTable::default();
    let mut p = Printer::new(ctx, &dummy_vt);
    for (i, a) in args.iter().enumerate() {
        if i > 0 {
            out.push_str(", ");
        }
        p.expr(a, 0, out);
    }
    let _ = pool;
}

#[allow(clippy::only_used_in_recursion)]
fn emit_class_body(
    pool: &DexPool,
    class: &PoolClass,
    ctx: &DexCtx<'_>,
    opts: &ClassOptions,
    out: &mut String,
    depth: usize,
) -> anyhow::Result<()> {
    let ind = indent(depth);
    // The class's own header must use the RENAMED display name — the
    // ctor path (is_init) already renames; a header declaring `class a`
    // while the file/ctor say `a_2` leaves the ctor looking like a
    // method with no return type (MinisApp's Y2.a vs y2.a package
    // case-collision: 2,518 javac parse errors).
    let cname = crate::apply_class_rename(&class.name);
    let (_, simple) = split_name(&cname);
    // A class emitted as its OWN top-level file (depth 0) must declare a
    // flat `$` name — `class Outer.Inner` is not declarable at file
    // scope. An INLINE nested member (depth > 0) declares its own
    // segment inside the parent's body.
    let simple = if depth == 0 {
        simple.to_string()
    } else {
        // R8 names can END in `$`: an empty last `$`-segment would
        // render `class  {` — keep the whole simple name.
        let seg = simple.rsplit('$').next().unwrap_or(&simple);
        if seg.is_empty() {
            simple.to_string()
        } else {
            seg.to_string()
        }
    };
    // A class NAMED `var`-style (taobao ships `tb.var`) cannot be
    // declared — restricted contextual type names escape here and in
    // the ctor, file name, and every type reference.
    let simple = if is_restricted_type_name(&simple) {
        format!("_{simple}")
    } else {
        simple
    };
    let is_iface = class.is_interface();
    let is_enum = class.is_enum();

    let mut head = String::new();
    let a = class.access;
    if a & ACC_PUBLIC != 0 || widen_class(&class.name) {
        head.push_str("public ");
    }
    // Inline nested members need their `static` (interfaces/annotations
    // are implicitly static; a missing `static` on a member class makes
    // every `new Report(...)` site an "outer instance required" error).
    if depth > 0 && !is_iface && ctx.nested_is_static(&class.name) {
        head.push_str("static ");
    }
    if a & ACC_FINAL != 0 && !is_enum {
        head.push_str("final ");
    }
    if a & ACC_ABSTRACT != 0 && !is_iface {
        head.push_str("abstract ");
    }
    if a & ACC_ANNOTATION != 0 {
        head.push('@');
    }
    let mut enum_consts: Option<(Vec<EnumConst>, crate::method::MethodBody)> = if is_enum {
        collect_enum_constants(pool, class, ctx)
    } else {
        None
    };
    if let Some(ecs) = &enum_consts {
        // True `enum` declaration: constants render in the header, the
        // desugared boilerplate (const fields, their <clinit> inits —
        // stripped by strip_enum_const_inits — and the ACC_ENUM flags)
        // disappears. R8-renamed values()/valueOf() stay (they do not
        // collide with the compiler-generated ones); javac-named ones
        // are skipped at the method loop below.
        head.push_str("enum ");
        head.push_str(&java_ident(&simple));
        let _ = ecs;
    } else if is_enum {
        // An enum with constant-specific bodies carries ACC_ABSTRACT —
        // `abstract final` is an illegal modifier combination; abstract
        // (already pushed above) suppresses the hardcoded final.
        let abstract_ = a & ACC_ABSTRACT != 0;
        head.push_str(if abstract_ {
            "/* enum */ class "
        } else {
            "/* enum */ final class "
        });
        head.push_str(&java_ident(&simple));
    } else if is_iface {
        head.push_str("interface ");
        head.push_str(&java_ident(&simple));
    } else {
        head.push_str("class ");
        head.push_str(&java_ident(&simple));
    }
    // The obscured-super import was emitted at the package line; the
    // clause must render the SIMPLE name (the qualified form binds to
    // the class itself even with the import present).
    let render_super = |sup: &String| -> String { print_class_name(pool, sup) };
    if is_iface {
        // A @interface cannot declare extends at all (JLS 9.6): the dex
        // interface table lists java/lang/annotation/Annotation for
        // every annotation type — rendering it produced "对于
        // @interfaces, 不允许 'extends'" (weibo 1100).
        let is_annot = a & ACC_ANNOTATION != 0;
        if !class.interfaces.is_empty() && !is_annot {
            head.push_str(" extends ");
            head.push_str(&join_dotted(pool, &class.interfaces));
        }
    } else {
        if let Some(sup) = &class.super_name {
            if sup != "java/lang/Object" && !is_enum {
                head.push_str(" extends ");
                head.push_str(&render_super(sup));
            }
        }
        if !class.interfaces.is_empty() {
            head.push_str(if is_iface {
                " extends "
            } else {
                " implements "
            });
            head.push_str(&join_dotted(pool, &class.interfaces));
        }
    }

    out.push_str(&ind);
    out.push_str(&head);
    out.push_str(" {\n");

    // True-enum constant list: the constants lead the body, ahead of
    // any remaining fields.
    if let Some((ecs, _)) = &enum_consts {
        for (i, ec) in ecs.iter().enumerate() {
            out.push_str(&indent(depth + 1));
            out.push_str(&java_ident(&ec.name));
            if !ec.extra_args.is_empty() {
                let mut line = String::from("(");
                render_enum_args(ctx, pool, &ec.extra_args, &mut line);
                line.push(')');
                out.push_str(&line);
            }
            if i + 1 < ecs.len() {
                out.push_str(",\n");
            } else {
                out.push_str(";\n");
            }
        }
        out.push('\n');
    }

    // Fields.
    // Kotlin `object`/lazy singletons: the dex static value for INSTANCE
    // is null while the clinit assigns it — rendering `= null` as the
    // field initializer made the clinit assignment illegal ("无法为
    // static final 变量 INSTANCE 分配值", okio SegmentPool family). A
    // static final whose static value is null AND which the clinit
    // sputs renders as a blank final. Scan the clinit's raw SPuts once.
    let clinit_sputs: Option<jdc_core::FxHashSet<(std::sync::Arc<str>, std::sync::Arc<str>)>> =
        // Gate: only classes with a null-valued final static can produce
        // a blank final — skip the clinit decode otherwise (decoding it
        // for every class cost seconds on 98k-class corpora).
        (|| {
            let has_null_final = class.static_fields.iter().enumerate().any(|(i, f)| {
                f.access & crate::access::ACC_FINAL != 0
                    && matches!(class.static_values.get(i), Some(StaticValue::Null))
            });
            if !has_null_final {
                return None;
            }
            let m = class.all_methods().find(|m| &*m.name == "<clinit>")?;
            let dex = pool.dex(m.dex_idx)?;
            let ci = dex.code_at(m.code_off)?;
            let mut set: jdc_core::FxHashSet<(
                std::sync::Arc<str>,
                std::sync::Arc<str>,
            )> = jdc_core::FxHashSet::default();
            for ins in &ci.insns {
                if let InsnKind::SPut { field_idx, .. } = ins.kind {
                    let fid = dex.field(field_idx);
                    // clinit only writes own-class fields here; match
                    // (name, type) against the field list below.
                    set.insert((
                        std::sync::Arc::from(dex.string(fid.name_idx)),
                        std::sync::Arc::from(dex.type_name(fid.type_idx)),
                    ));
                }
            }
            Some(set)
        })();
    let mut field_emitted = false;
    for (i, f) in class.static_fields.iter().enumerate() {
        // Enum constant fields became the header list above.
        if enum_consts.is_some() && f.access & crate::access::ACC_ENUM != 0 {
            continue;
        }
        if field_emitted || !class.instance_fields.is_empty() {
            out.push('\n');
        }
        field_emitted = true;
        // A blank-final: static final, null static value, clinit-assigned.
        let blank_final = f.access & crate::access::ACC_FINAL != 0
            && matches!(class.static_values.get(i), Some(StaticValue::Null))
            && clinit_sputs
                .as_ref()
                .is_some_and(|s| {
                    s.contains(&(
                        std::sync::Arc::from(f.name.as_str()),
                        std::sync::Arc::from(f.desc.as_str()),
                    ))
                });
        emit_field(
            pool,
            f,
            if blank_final {
                None
            } else {
                class.static_values.get(i)
            },
            &class.name,
            out,
            depth + 1,
            true,
            class.is_interface(),
        );
    }
    if !class.instance_fields.is_empty() && !class.static_fields.is_empty() {
        out.push('\n');
    }
    for (i, f) in class.instance_fields.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        emit_field(pool, f, None, &class.name, out, depth + 1, false, false);
    }

    // Methods.
    // Duplicated (name, parameter-types) pairs in one class are javac
    // "method is already defined" errors (weibo: 11.5k hits). Source
    // cannot declare them, so exactly one survives: covariant bridges
    // (`Object get(int)` beside `ByteString get(int)` — the compiler
    // GENERATES bridges), plus R8 output that lost the bridge flag
    // (okio Buffer.clone, interface getView re-declarations). A
    // non-bridge method outranks a bridge for the same signature
    // (bridge bodies are delegation stubs); first occurrence otherwise.
    // Never drop a signature outright — the last method standing for a
    // key is always rendered.
    fn sig_key(m: &PoolMethod) -> (&str, &str) {
        let d: &str = &m.desc;
        let lo = d.find('(').map(|i| i + 1).unwrap_or(0);
        let hi = d.find(')').unwrap_or(d.len());
        (&*m.name, &d[lo..hi])
    }
    let methods: Vec<&PoolMethod> = class.all_methods().collect();
    let mut claim: jdc_core::FxHashMap<(&str, &str), usize> = jdc_core::FxHashMap::default();
    for (i, m) in methods.iter().enumerate() {
        let key = sig_key(m);
        match claim.get(&key) {
            Some(&j)
                if m.access & crate::access::ACC_BRIDGE == 0
                    && methods[j].access & crate::access::ACC_BRIDGE != 0 =>
            {
                claim.insert(key, i);
            }
            None => {
                claim.insert(key, i);
            }
            _ => {}
        }
    }
    // NOTE: a same-signature bridge is already retired by the claim map
    // above. An ERASURE-shaped bridge (SAM/variance: params are the
    // Object-erased types of a sibling's) must NOT be skipped: with raw
    // (non-generic) interface rendering the bridge is exactly what
    // satisfies the interface — skipping it turned every Kotlin lambda
    // class into "不是抽象的, 并且未覆盖…invoke(Object,Object)" (reqable
    // +17), while the "对invoke的引用不明确" it appeared to fix was a
    // missing-kotlin-classpath cascade, not a ddc bug.
    let mut emitted_any = !class.static_fields.is_empty() || !class.instance_fields.is_empty();
    for (i, m) in methods.iter().enumerate() {
        if &*m.name == "<clinit>" {
            continue; // rendered after the fields
        }
        if claim.get(&sig_key(m)) != Some(&i) {
            continue;
        }
        // True-enum rendering: javac auto-generates values()/valueOf()
        // — the original-named ones must not re-declare (R8-renamed
        // copies stay, they do not collide).
        if let Some((ecs, _)) = &enum_consts {
            let d = m.parsed_desc();
            let self_arr = d.as_ref().map(|d| d.ret == JavaType::Array(Box::new(JavaType::Object(class.name.as_str().into()))));
            let self_ret = d.as_ref().map(|d| d.ret == JavaType::Object(class.name.as_str().into()));
            let no_args = d.as_ref().map(|d| d.args.is_empty()).unwrap_or(false);
            let one_str = d
                .as_ref()
                .map(|d| d.args.len() == 1 && matches!(d.args[0], JavaType::Object(ref n) if n.as_ref() == "java/lang/String"))
                .unwrap_or(false);
            let static_ = m.is_static();
            if static_ && no_args && self_arr == Some(true) && &*m.name == "values" {
                continue;
            }
            if static_ && one_str && self_ret == Some(true) && &*m.name == "valueOf" {
                continue;
            }
            // Enum constructors must be private in source form.
            let m_owned: Option<PoolMethod> = if &*m.name == "<init>" {
                let mut c = (*m).clone();
                c.access = (c.access & !(crate::access::ACC_PUBLIC | crate::access::ACC_PROTECTED | crate::access::ACC_PRIVATE)) | crate::access::ACC_PRIVATE;
                Some(c)
            } else {
                None
            };
            let m_ref: &PoolMethod = m_owned.as_ref().unwrap_or(m);
            let mark = out.len();
            if emitted_any {
                out.push('\n');
            }
            if emit_method(pool, class, ctx, m_ref, depth + 1, true, out)? {
                emitted_any = true;
            } else {
                out.truncate(mark);
            }
            let _ = ecs;
            continue;
        }
        let mark = out.len();
        if emitted_any {
            out.push('\n');
        }
        if emit_method(pool, class, ctx, m, depth + 1, enum_consts.is_some(), out)? {
            emitted_any = true;
        } else {
            out.truncate(mark);
        }
    }
    // Inherited-ctor bridges: dex method refs resolve through the
    // hierarchy, so `new C(args)` against a class C with NO declared
    // <init> legally targets the SUPERCLASS ctor (weixin tenpay
    // `new m(map)` — m declares nothing, i.<init>(HashMap) does). Java
    // has no inherited constructors: without a bridge, C's implicit
    // default ctor is the only one and every arg-carrying construction
    // fails ("无法将类 m中的构造器 m应用到给定类型; 需要: 没有参数").
    // Mirror the nearest ctor-declaring ancestor's public/protected
    // ctors as thin `super(..)` delegations.
    if !class.is_interface()
        && enum_consts.is_none()
        && !class.all_methods().any(|m| &*m.name == "<init>")
    {
        if let Some(mut sup) = class.super_name.clone() {
            loop {
                if sup == "java/lang/Object" {
                    break;
                }
                let Some(sc) = pool.get(&sup) else { break };
                let ctors: Vec<&PoolMethod> = sc
                    .all_methods()
                    .filter(|m| &*m.name == "<init>")
                    .collect();
                if !ctors.is_empty() {
                    for sm in ctors {
                        if sm.access & crate::access::ACC_PRIVATE != 0
                            || (sm.access
                                & (crate::access::ACC_PUBLIC | crate::access::ACC_PROTECTED)
                                == 0)
                        {
                            continue; // private/package-private: no legal bridge
                        }
                        let Some(d) = sm.parsed_desc() else { continue };
                        let mods = if sm.access & crate::access::ACC_PUBLIC != 0 {
                            "public "
                        } else {
                            "protected "
                        };
                        if emitted_any {
                            out.push('\n');
                        }
                        out.push_str(&format!("    {}", "    ".repeat(depth)));
                        out.push_str(mods);
                        out.push_str(&java_ident(&simple));
                        out.push('(');
                        let mut names = Vec::with_capacity(d.args.len());
                        for (i, a) in d.args.iter().enumerate() {
                            if i > 0 {
                                out.push_str(", ");
                            }
                            out.push_str(&type_name(pool, a));
                            let nm = format!("p{}", i + 1);
                            out.push(' ');
                            out.push_str(&nm);
                            names.push(nm);
                        }
                        out.push_str(") {\n");
                        out.push_str(&format!("    {}    super({});\n", "    ".repeat(depth), names.join(", ")));
                        out.push_str(&format!("    {}}}\n", "    ".repeat(depth)));
                        emitted_any = true;
                    }
                    break;
                }
                match &sc.super_name {
                    Some(n) => sup = n.clone(),
                    None => break,
                }
            }
        }
    }

    // Referenced-but-undeclared inherited-ctor bridges. The block above
    // covers a class with NO declared ctor; this covers a class that
    // DECLARES ctors but is missing one that dex callers reach THROUGH
    // it. Dex resolves a `<init>` method-ref up the superclass chain, so
    // a subclass `super(2, recv, owner, name, sig, flags)` may target
    // `m.<init>(int,Object,Class,String,String,int)` that `m`
    // (FunctionReferenceImpl) does NOT declare — its super `l` does.
    // Java has no inherited ctors, so without a bridge on `m` every such
    // subclass fails ("无法将类 m中的构造器 m应用到给定类型"). Mirror each
    // referenced sig C lacks from the nearest ancestor declaring it, as a
    // thin `super(..)` delegation. Ref-gated (only sigs actually called
    // through C) so ordinary classes gain nothing.
    if !class.is_interface()
        && enum_consts.is_none()
        && class.all_methods().any(|m| &*m.name == "<init>")
    {
        if let Some(refs) = pool.ctor_ref_sigs(&class.name) {
            fn arg_sig(d: &str) -> &str {
                let lo = d.find('(').map(|i| i + 1).unwrap_or(0);
                let hi = d.find(')').unwrap_or(d.len());
                &d[lo..hi]
            }
            let declared: jdc_core::FxHashSet<&str> = class
                .all_methods()
                .filter(|m| &*m.name == "<init>")
                .map(|m| arg_sig(&m.desc))
                .collect();
            let mut missing: Vec<&String> = refs
                .iter()
                .filter(|s| !declared.contains(s.as_str()))
                .collect();
            missing.sort();
            for sig in missing {
                // Nearest ancestor declaring a public/protected <init>
                // with this exact arg signature.
                let mut sup = class.super_name.clone();
                let mut found: Option<&PoolMethod> = None;
                while let Some(s) = sup {
                    if s == "java/lang/Object" {
                        break;
                    }
                    let Some(sc) = pool.get(&s) else { break };
                    if let Some(sm) = sc.all_methods().find(|m| {
                        &*m.name == "<init>"
                            && arg_sig(&m.desc) == sig.as_str()
                            && m.access & crate::access::ACC_PRIVATE == 0
                            && m.access
                                & (crate::access::ACC_PUBLIC | crate::access::ACC_PROTECTED)
                                != 0
                    }) {
                        found = Some(sm);
                        break;
                    }
                    sup = sc.super_name.clone();
                }
                let Some(sm) = found else { continue };
                let Some(d) = sm.parsed_desc() else { continue };
                let mods = if sm.access & crate::access::ACC_PUBLIC != 0 {
                    "public "
                } else {
                    "protected "
                };
                if emitted_any {
                    out.push('\n');
                }
                out.push_str(&format!("    {}", "    ".repeat(depth)));
                out.push_str(mods);
                out.push_str(&java_ident(&simple));
                out.push('(');
                let mut names = Vec::with_capacity(d.args.len());
                for (i, a) in d.args.iter().enumerate() {
                    if i > 0 {
                        out.push_str(", ");
                    }
                    out.push_str(&type_name(pool, a));
                    let nm = format!("p{}", i + 1);
                    out.push(' ');
                    out.push_str(&nm);
                    names.push(nm);
                }
                out.push_str(") {\n");
                out.push_str(&format!(
                    "    {}    super({});\n",
                    "    ".repeat(depth),
                    names.join(", ")
                ));
                out.push_str(&format!("    {}}}\n", "    ".repeat(depth)));
                emitted_any = true;
            }
        }
    }

    // Framework-ancestor ctor bridge. The two blocks above mirror POOL
    // ancestors; both stop at a framework (off-pool) superclass. A class
    // whose DIRECT superclass is a framework type (in android.jar) that
    // declares a ctor the class lacks also needs a bridge — `super(args)`
    // resolves against the jar. Only the DIRECT-super-is-framework case is
    // safe: an intermediate pool ancestor with no matching ctor would
    // break the `super()` chain (that ancestor needs its own bridge, which
    // its own refs may not trigger). Covers weixin g3 extends
    // org.xml.sax.SAXException (`new g3(String)` → SAXException(String)),
    // the custom-exception / Parcelable family the pool walk can't reach.
    if !class.is_interface() && enum_consts.is_none() {
        let direct_fw = class
            .super_name
            .as_ref()
            .map(|s| s != "java/lang/Object" && pool.get(s).is_none())
            .unwrap_or(false);
        if direct_fw {
            if let Some(refs) = pool.ctor_ref_sigs(&class.name) {
                fn arg_sig(d: &str) -> &str {
                    let lo = d.find('(').map(|i| i + 1).unwrap_or(0);
                    let hi = d.find(')').unwrap_or(d.len());
                    &d[lo..hi]
                }
                let declared: jdc_core::FxHashSet<&str> = class
                    .all_methods()
                    .filter(|m| &*m.name == "<init>")
                    .map(|m| arg_sig(&m.desc))
                    .collect();
                let mut missing: Vec<&String> = refs
                    .iter()
                    .filter(|s| !declared.contains(s.as_str()))
                    .collect();
                missing.sort();
                for sig in missing {
                    // A no-ctor class already gets Java's default
                    // `C(){super();}` — never bridge the empty sig for it.
                    if sig.is_empty() && declared.is_empty() {
                        continue;
                    }
                    let args = split_arg_descs(sig);
                    if emitted_any {
                        out.push('\n');
                    }
                    out.push_str(&format!("    {}", "    ".repeat(depth)));
                    out.push_str("public ");
                    out.push_str(&java_ident(&simple));
                    out.push('(');
                    let mut names = Vec::with_capacity(args.len());
                    for (i, a) in args.iter().enumerate() {
                        if i > 0 {
                            out.push_str(", ");
                        }
                        out.push_str(&type_name(pool, a));
                        let nm = format!("p{}", i + 1);
                        out.push(' ');
                        out.push_str(&nm);
                        names.push(nm);
                    }
                    out.push_str(") {\n");
                    out.push_str(&format!(
                        "    {}    super({});\n",
                        "    ".repeat(depth),
                        names.join(", ")
                    ));
                    out.push_str(&format!("    {}}}\n", "    ".repeat(depth)));
                    emitted_any = true;
                }
            }
        }
    }

    // Static initializer. INTERFACES cannot carry a `static { }` block in
    // Java — their clinit only assigns constants, which static_values (or
    // the `= null` default) already render as field initializers; skip
    // the block entirely.
    let skip_clinit = class.is_interface();
    if let Some((_, clinit_body)) = enum_consts.take() {
        // True-enum <clinit>: the constant assignments are already gone;
        // render the remainder (the $VALUES array build) directly.
        // Skip an empty remainder (all statements were constant inits).
        let empty = match &clinit_body.body {
            Stmt::Block(v) => v.iter().all(|s| matches!(s, Stmt::Block(b) if b.is_empty())),
            _ => false,
        };
        if !empty {
            if emitted_any {
                out.push('\n');
            }
            out.push_str(&indent(depth + 1));
            out.push_str("static {\n");
            let p = Printer::new(ctx, &clinit_body.vt);
            let text = p.with_indent(depth + 2).into_string(&clinit_body.body);
            for line in text.lines() {
                if line.trim().is_empty() {
                    continue;
                }
                out.push_str(line);
                out.push('\n');
            }
            out.push_str(&indent(depth + 1));
            out.push_str("}\n");
            emitted_any = true;
        }
    } else if let Some(clinit) = (!skip_clinit)
        .then(|| class.all_methods().find(|m| &*m.name == "<clinit>"))
        .flatten()
    {
        let mark = out.len();
        if emitted_any {
            out.push('\n');
        }
        if emit_method(pool, class, ctx, clinit, depth + 1, false, out)? {
            emitted_any = true;
        } else {
            out.truncate(mark);
        }
    }

    // Nested member classes (clean `$` tails only; anonymous/local/lambda
    // classes are emitted as their own top-level files by the driver).
    for nested in nested_members(pool, class, ctx) {
        if emitted_any {
            out.push('\n');
        }
        let nested_ctx = DexCtx::new(pool, nested);
        out.push('\n');
        emit_class_body(pool, nested, &nested_ctx, opts, out, depth + 1)?;
        emitted_any = true;
    }

    out.push_str(&ind);
    out.push_str("}\n");
    Ok(())
}

/// Member classes of `class` (direct children by the `$` chain / member
/// annotations), excluding anonymous / local / lambda shapes.
fn nested_members<'a>(
    pool: &'a DexPool,
    class: &'a PoolClass,
    ctx: &DexCtx<'_>,
) -> Vec<&'a PoolClass> {
    let mut out = Vec::new();
    for name in pool.children_of(&class.name) {
        let Some(pc) = pool.get(name) else { continue };
        // Only a REAL `outer$tail` name is an inline member: the
        // children index also carries annotation-derived outers
        // (EnclosingClass) with no naming relationship to the child
        // (obfuscated apps pair a 1-char name with a long enclosing
        // descriptor) — the blind slice panicked there (alipay
        // `a.a.a.a.c`, exposed once children_of actually returned data).
        let Some(rest) = name
            .strip_prefix(class.name.as_str())
            .and_then(|t| t.strip_prefix('$'))
        else {
            continue;
        };
        if rest.is_empty() || rest.starts_with('-') {
            continue;
        }
        if ctx.find_outer(name).as_deref() != Some(class.name.as_str()) {
            continue;
        }
        let tail = rest.rsplit('$').next().unwrap_or(rest);
        if !tail.is_empty() && tail.chars().all(|c| c.is_ascii_digit()) {
            continue; // anonymous
        }
        if tail.starts_with(|c: char| c.is_ascii_digit()) {
            continue; // local
        }
        out.push(pc);
    }
    out
}

// Eight parameters are all load-bearing (pool, field, static value,
// owner class, output, depth, staticness, interface-init requirement);
// bundling them into a struct would obscure each call site.
#[allow(clippy::too_many_arguments)]
fn emit_field(
    pool: &DexPool,
    f: &crate::PoolField,
    init: Option<&StaticValue>,
    f_class: &str,
    out: &mut String,
    depth: usize,
    is_static: bool,
    require_init: bool,
) {
    let ind = indent(depth);
    let mut line = String::new();
    let a = f.access;
    if a & ACC_PUBLIC != 0 || widen_field(f_class, &f.name) {
        line.push_str("public ");
    } else if a & ACC_PRIVATE != 0 {
        line.push_str("private ");
    } else if a & ACC_PROTECTED != 0 {
        line.push_str("protected ");
    }
    if is_static {
        line.push_str("static ");
    }
    if a & ACC_FINAL != 0 {
        line.push_str("final ");
    }
    if a & ACC_SYNTHETIC != 0 {
        line.push_str("/* synthetic */ ");
    }
    if a & ACC_TRANSIENT_HINT != 0 {
        line.push_str("transient ");
    }
    if a & ACC_VOLATILE_HINT != 0 {
        line.push_str("volatile ");
    }
    let ty = type_name(pool, &desc_type(&f.desc));
    line.push_str(&ty);
    line.push(' ');
    let fname = jdc_core::rename::field_display(f_class, &f.name, &f.desc).unwrap_or(&*f.name);
    line.push_str(&java_ident(fname));
    let mut rendered = None;
    if let Some(v) = init {
        rendered = render_static_value(pool, v, f_class);
    }
    if rendered.is_none() && require_init {
        // Interface fields MUST have an initializer in Java; the dex may
        // not carry a static_values entry for a compile-time-constant the
        // compiler folded away. Keep it compilable.
        let default = match desc_type(&f.desc) {
            JavaType::Boolean => "false",
            JavaType::Byte | JavaType::Short | JavaType::Char | JavaType::Int => "0",
            JavaType::Long => "0L",
            JavaType::Float => "0.0F",
            JavaType::Double => "0.0",
            _ => "null",
        };
        rendered = Some(default.to_string());
    }
    if let Some(text) = rendered {
        // A long field holding an `Int(i64)` static value needs the `L`
        // suffix: without it the literal is an int and overflows
        // (`long d = -6343169151696340687` failed javac).
        let text = if matches!(desc_type(&f.desc), JavaType::Long)
            && text.chars().all(|c| c.is_ascii_digit() || c == '-')
        {
            format!("{text}L")
        } else {
            text
        };
        line.push_str(" = ");
        line.push_str(&text);
    }
    line.push(';');
    out.push_str(&ind);
    out.push_str(&line);
    out.push('\n');
}

// DEX field access_flags bits 0x40/0x80 are bridge/varargs for METHODS and
// volatile(0x40)/transient(0x80) for fields.
pub const ACC_VOLATILE_HINT: u32 = 0x40;
pub const ACC_TRANSIENT_HINT: u32 = 0x80;

fn render_static_value(pool: &DexPool, v: &StaticValue, owner: &str) -> Option<String> {
    Some(match v {
        StaticValue::Int(i) => i.to_string(),
        StaticValue::Float(f) => format_float(*f as f64, true),
        StaticValue::Double(d) => format_float(*d, false),
        StaticValue::Str(s) => format!("\"{}\"", escape_string(s)),
        StaticValue::Type(t) => format!("{}.class", dotted(t)),
        StaticValue::Boolean(b) => b.to_string(),
        StaticValue::Null => "null".into(),
        StaticValue::Field(cls, name) => {
            let n = java_ident(name);
            if cls == owner {
                n.into_owned()
            } else {
                format!("{}.{}", print_class_name(pool, cls), n)
            }
        }
        StaticValue::Other => return None,
    })
}

/// Render one method straight into the class buffer `out`. Returns
/// whether anything was written (skips: no descriptor, deferred to a
/// monitored thread). The old shape returned a per-method String that
/// the caller copied in — one extra full copy of every method body.
/// `enum_promoted`: the class rendered as a true `enum` declaration
/// (constants in the header), which changes what a ctor signature may
/// declare.
fn emit_method(
    pool: &DexPool,
    class: &PoolClass,
    ctx: &DexCtx<'_>,
    m: &PoolMethod,
    depth: usize,
    enum_promoted: bool,
    out: &mut String,
) -> anyhow::Result<bool> {
    let ind = indent(depth);
    let desc = m.parsed_desc();

    // Signature.
    let mut sig = String::with_capacity(192);
    let a = m.access;
    if a & ACC_PUBLIC != 0 || widen_method(&class.name, &m.name, &m.desc) {
        sig.push_str("public ");
    } else if a & ACC_PRIVATE != 0 {
        sig.push_str("private ");
    } else if a & ACC_PROTECTED != 0 {
        sig.push_str("protected ");
    }
    let is_clinit = &*m.name == "<clinit>";
    let is_init = &*m.name == "<init>";
    if a & ACC_STATIC != 0 || is_clinit {
        sig.push_str("static ");
    }
    if a & ACC_FINAL != 0 {
        sig.push_str("final ");
    }
    // `abstract synchronized` is an illegal combination — obfuscated
    // builds mark abstract bridges synchronized (WhatsApp
    // SQLiteOpenHelper).
    if a & (ACC_SYNCHRONIZED | ACC_DECLARED_SYNCHRONIZED) != 0 && a & ACC_ABSTRACT == 0 {
        sig.push_str("synchronized ");
    }
    if a & ACC_NATIVE != 0 {
        sig.push_str("native ");
    }
    if a & ACC_ABSTRACT != 0 {
        sig.push_str("abstract ");
    }
    if a & ACC_SYNTHETIC != 0 {
        sig.push_str("/* synthetic */ ");
    }

    // Body (needed for parameter names even for abstract methods).
    let mut body = decompile_method(pool, class, m).ok().flatten();
    // An interface method WITH a body is a `default` method (JLS 9.4.3)
    // unless static/private — dex carries no `default` flag, so the
    // plain form rendered an abstract signature with a body and javac
    // rejected every one ("接口抽象方法不能带有主体", weixin 1138). A
    // default REQUIRES a body: only push it once the body is confirmed
    // (a failed decompile renders a body-less declaration).
    if class.is_interface()
        && a & (ACC_STATIC | ACC_ABSTRACT | ACC_NATIVE | ACC_PRIVATE | ACC_ANNOTATION) == 0
        && body.is_some()
    {
        sig.insert_str(0, "default ");
    }
    let param_names: Vec<String> = body
        .as_ref()
        .map(|b| {
            let mut ps: Vec<(u16, String)> =
                b.vt.vars
                    .iter()
                    .filter(|v| v.is_param && v.name != "this")
                    .map(|v| (v.slot, v.name.clone()))
                    .collect();
            ps.sort_by_key(|(s, _)| *s);
            ps.into_iter().map(|(_, n)| n).collect()
        })
        .unwrap_or_else(|| {
            (0..desc.as_ref().map(|d| d.args.len()).unwrap_or(0))
                .map(|i| format!("p{}", i + 1))
                .collect()
        });

    // Non-static member-inner ctor: the synthetic outer instance rides
    // as args[0] (typed as the direct enclosing class). Every emission
    // site already passes it implicitly — qualified `outer.new Inner(..)`,
    // `this.new Inner(..)` from inside the outer, `super(..)` after
    // skip_outer_arg — so the signature drops the param and the body's
    // references become `Outer.this`. Without this, every construction
    // site fails javac arity ("无法将类…构造器…应用到给定类型" — the
    // guava androidx inner-class families, d8 capture lambdas).
    let mut inner_arg0 = 0usize;
    if is_init {
        let tail = class.name.rsplit('$').next().unwrap_or("");
        let digit_simple = !tail.is_empty() && tail.chars().all(|c| c.is_ascii_digit());
        if let Some(d) = desc.as_ref() {
            if let Some(JavaType::Object(outer)) = d.args.first() {
                // The direct enclosing class: nesting annotation when
                // present, else the `$`-chain parent (find_outer_name —
                // d8 lambdas are `Outer$$ExternalSyntheticLambdaN`, where
                // a plain rsplit leaves a trailing `$`). It must agree
                // with the this$0 type for the rewrite to fire.
                let enclosing: Option<String> = class
                    .nesting
                    .enclosing_class
                    .clone()
                    .or_else(|| crate::find_outer_name(pool, &class.name));
                let direct = enclosing.as_deref() == Some(outer.as_ref());
                if direct && !digit_simple && ctx.class_has_this0(&class.name) {
                    let param0 = body.as_ref().and_then(|b| {
                        b.vt.vars
                            .iter()
                            .find(|v| v.is_param && v.name != "this")
                            .map(|v| v.id)
                    });
                    let written = match (&body, param0) {
                        (Some(b), Some(p0)) => crate::passes::local_is_written(&b.body, p0),
                        _ => false,
                    };
                    if let (Some(p0), false) = (param0, written) {
                        if let Some(b) = body.as_mut() {
                            // this()-delegations to the SAME class drop the
                            // outer arg (all of its ctors share the strip);
                            // a super target joins when it is an inner of
                            // the same enclosing family.
                            let mut eligible: Vec<String> = vec![class.name.clone()];
                            if let Some(sup) = &class.super_name {
                                let sup_enclosing =
                                    crate::find_outer_name(pool, sup);
                                if sup_enclosing.as_deref() == Some(outer.as_ref())
                                    && ctx.class_has_this0(sup)
                                {
                                    eligible.push(sup.clone());
                                }
                            }
                            let renamed = crate::apply_class_rename(outer);
                            let display = dotted(renamed.as_ref());
                            let ty =
                                jdc_core::ir::expr::TypeRef::J(JavaType::Object(outer.clone()));
                            crate::passes::rewrite_inner_ctor_outer_param(
                                &mut b.body,
                                p0,
                                &display,
                                &ty,
                                &eligible,
                            );
                            inner_arg0 = 1;
                        }
                    }
                }
            }
        }
    }

    if is_clinit {
        // `static { ... }` — the caller strips the method name/params.
    } else {
        let Some(d) = &desc else { return Ok(false) };
        if is_init {
            // The ctor name must equal the DECLARED class name of its
            // file: flat `$` at depth 0 (own file), own segment when
            // inlined in the parent at depth > 0. Case-renamed classes
            // use their display name.
            let dname = crate::apply_class_rename(&class.name);
            let (_, simple) = split_name(&dname);
            // emit_method's depth is the METHOD indent = class depth + 1:
            // own-file classes (depth 0 header → method depth 1) need the
            // flat `$` ctor name; inline nested members use their segment.
            let base = if depth <= 1 {
                simple.to_string()
            } else {
                // R8 names can END in `$` (`ThreadMsg$$$`): the last
                // `$`-segment is empty — keep the whole simple name.
                let seg = simple.rsplit('$').next().unwrap_or(&simple);
                if seg.is_empty() {
                    simple.to_string()
                } else {
                    seg.to_string()
                }
            };
            let name = if is_restricted_type_name(&base) {
                format!("_{base}")
            } else {
                base
            };
            sig.push_str(&java_ident(&name));
        } else {
            sig.push_str(&type_name(pool, &d.ret));
            sig.push(' ');
            let mname =
                jdc_core::rename::field_display(&class.name, &m.name, &m.desc).unwrap_or(&*m.name);
            sig.push_str(&java_ident(mname));
        }
        sig.push('(');
        // A promoted enum ctor's dex descriptor carries the compiler-
        // synthesized `(String name, int ordinal)` prefix — JLS forbids
        // declaring those (they are implicit in the `A(args)` constant
        // declarations the promotion emits, and strip_enum_ctor_super
        // already removed the `super(name, ordinal, ..)` delegation).
        // Skip the pair in the signature, or every constant declaration
        // fails javac arity ("无法将枚举…构造器…应用到给定类型"). Only
        // when the body never reads the two params — code that genuinely
        // uses them keeps the declared form.
        let mut arg0 = 0;
        if enum_promoted
            && is_init
            && matches!(d.args.first(), Some(JavaType::Object(s)) if s.as_ref() == "java/lang/String")
            && matches!(d.args.get(1), Some(JavaType::Int))
        {
            // The Kotlin default-arg bridge ctor reads name/ordinal in
            // its `this(str, p2, ..)` delegation only — the LEADING pair
            // of a this()-delegation drops together with the params, so
            // those reads do not block the strip (the constants' extra
            // args match the bridge's user params, not the 2-param user
            // ctor).
            let synthetic: Vec<u32> = body
                .as_ref()
                .map(|b| {
                    b.vt
                        .vars
                        .iter()
                        .filter(|v| v.is_param && v.name != "this")
                        .take(2)
                        .map(|v| v.id)
                        .collect()
                })
                .unwrap_or_default();
            // The delegation-leading extension only applies to the
            // Kotlin default-arg BRIDGE: its descriptor ends with
            // kotlin/jvm/internal/DefaultConstructorMarker. A REAL user
            // ctor whose first two params are (String, int) and merely
            // forwards them must keep its signature (weixin +2.9k when
            // ungated).
            let is_bridge = matches!(
                d.args.last(),
                Some(JavaType::Object(m)) if m.as_ref() == "kotlin/jvm/internal/DefaultConstructorMarker"
            );
            let mut lead = [0usize, 0usize];
            let refs_ok = body.as_ref().is_some_and(|b| {
                let uses = crate::passes::count_locals_stmts(std::slice::from_ref(&b.body));
                let mut c = b.body.clone();
                let cls_name = class.name.as_str();
                crate::passes::walk_stmt_exprs(&mut c, &mut |e| {
                    if let Expr::Method { name: mn, cls: mc, args, is_special, .. } = e {
                        if &**mn == "<init>" && *is_special && mc.as_ref() == cls_name {
                            for (k, a) in args.iter().enumerate() {
                                if let Expr::Local { var: v, .. } = a {
                                    if let Some(pi) = synthetic.iter().position(|p| p == v) {
                                        if k == pi && k < 2 {
                                            lead[pi] += 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
                synthetic.iter().enumerate().all(|(pi, id)| {
                    let total = uses.get(id).copied().unwrap_or(0);
                    if is_bridge {
                        total == lead[pi]
                    } else {
                        total == 0
                    }
                })
            });
            if refs_ok {
                arg0 = 2;
                // Drop the leading (name, ordinal) args of the this()
                // delegations.
                if let (Some(b), true) = (body.as_mut(), is_bridge) {
                    let cls_name = class.name.as_str();
                    let syn = synthetic;
                    crate::passes::walk_stmt_exprs(&mut b.body, &mut |e| {
                        if let Expr::Method { name: mn, cls: mc, args, is_special, .. } = e {
                            if &**mn == "<init>" && *is_special && mc.as_ref() == cls_name {
                                let mut drop_n = 0;
                                for a in args.iter().take(2) {
                                    if let Expr::Local { var: v, .. } = a {
                                        if syn.contains(v) && drop_n == v - syn[0] {
                                            drop_n += 1;
                                            continue;
                                        }
                                    }
                                    break;
                                }
                                for _ in 0..drop_n {
                                    args.remove(0);
                                }
                            }
                        }
                    });
                }
            }
        }
        let n = d.args.len();
        let varargs = a & ACC_VARARGS != 0 && n > 0;
        let arg0 = arg0 + inner_arg0;
        for (i, arg) in d.args.iter().enumerate().skip(arg0) {
            if i > arg0 {
                sig.push_str(", ");
            }
            let name = param_names
                .get(i)
                .cloned()
                .unwrap_or_else(|| format!("p{}", i));
            if varargs && i + 1 == n {
                if let JavaType::Array(inner) = arg {
                    sig.push_str(&type_name(pool, inner));
                    sig.push_str("...");
                } else {
                    sig.push_str(&type_name(pool, arg));
                }
            } else {
                sig.push_str(&type_name(pool, arg));
            }
            sig.push(' ');
            // Param names come from dex debug info — obfuscated apps
            // name them `_` (reserved since Java 9) or after keywords.
            sig.push_str(&java_ident(&name));
        }
        sig.push(')');
    }

    if is_clinit {
        let Some(b) = body else { return Ok(false) };
        // Direct render: the printer starts at the method's ABSOLUTE
        // indent and appends into the same buffer that carries the
        // header — no intermediate body string, no per-line re-indent
        // pass (two full copies of every method body saved).
        out.push_str(&ind);
        out.push_str("static {\n");
        let hdr = out.len();
        let printer = Printer::new(ctx, &b.vt)
            .with_indent(depth + 1)
            .with_output(std::mem::take(out));
        let t_print = std::time::Instant::now();
        let mut rendered = printer.into_string(&b.body);
        crate::method::phase_hit(3, t_print);
        if rendered[hdr..].trim().is_empty() {
            rendered.truncate(hdr);
        }
        rendered.push_str(&ind);
        rendered.push_str("}\n");
        *out = rendered;
        return Ok(true);
    }
    if a & (ACC_ABSTRACT | ACC_NATIVE) != 0 || body.is_none() {
        out.push_str(&ind);
        out.push_str(&sig);
        out.push_str(";\n");
        return Ok(true);
    }
    let Some(b) = body else { return Ok(false) };

    // Direct render at the absolute indent level (see the clinit path).
    let mut printer = Printer::new(ctx, &b.vt).with_indent(depth + 1);
    match &b.desc.ret {
        JavaType::Boolean => {
            printer = printer.with_ret_bool(true);
        }
        JavaType::Char => {
            printer = printer.with_ret_char(true);
        }
        JavaType::Byte => {
            printer = printer.with_ret_narrow(true, false);
        }
        JavaType::Short => {
            printer = printer.with_ret_narrow(false, true);
        }
        _ => {}
    }
    out.push_str(&ind);
    out.push_str(&sig);
    out.push_str(" {\n");
    let hdr = out.len();
    let printer = printer.with_output(std::mem::take(out));
    let t_print = std::time::Instant::now();
    let mut rendered = printer.into_string(&b.body);
    crate::method::phase_hit(3, t_print);
    if rendered[hdr..].trim().is_empty() {
        rendered.truncate(hdr);
    }
    rendered.push_str(&ind);
    rendered.push_str("}\n");
    *out = rendered;
    Ok(true)
}

// ---------------------------------------------------------------------------
// Naming helpers
// ---------------------------------------------------------------------------

fn indent(depth: usize) -> String {
    "    ".repeat(depth)
}

/// `$`-separated nesting rendered with dots — but ONLY when every
/// segment is a clean Java identifier (a genuine member class chain).
/// Anonymous (`Outer$1`), Kotlin synthetic (`Version$bigInteger$2`,
/// `...$$inlined$collect$1`) and local-class tails are NOT member
/// classes Java can name; the whole name stays flat with `$` (ddc emits
/// them as their own top-level files).
/// Kotlin emits method names like `invokeSuspend$lambda-0` — `-` (and
/// any other non-identifier character) is not legal Java. Deterministic
/// mapping, applied identically at declaration and call sites.
pub(crate) fn java_ident(name: &str) -> std::borrow::Cow<'_, str> {
    // env::var_os is an environ lock+scan — java_ident runs per IDENTIFIER
    // (millions per APK), where it profiled as __NSGetEnviron.
    static DBG_IDENT: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    if *DBG_IDENT.get_or_init(|| std::env::var_os("DDC_DBG_IDENT").is_some()) {
        eprintln!("[ident] {name:?}");
    }
    let clean = name
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$');
    // Kotlin names fields/methods `default` (Companion.default) — no Java
    // program can declare or reference a keyword; the identical mapping
    // lives in jdc-core's call-site sanitizer.
    let keyword = matches!(
        name,
        "abstract"
            | "assert"
            | "boolean"
            | "break"
            | "byte"
            | "case"
            | "catch"
            | "char"
            | "class"
            | "const"
            | "continue"
            | "default"
            | "do"
            | "double"
            | "else"
            | "enum"
            | "extends"
            | "final"
            | "finally"
            | "float"
            | "for"
            | "goto"
            | "if"
            | "implements"
            | "import"
            | "instanceof"
            | "int"
            | "interface"
            | "long"
            | "native"
            | "new"
            | "package"
            | "private"
            | "protected"
            | "public"
            | "return"
            | "short"
            | "static"
            | "strictfp"
            | "super"
            | "switch"
            | "synchronized"
            | "this"
            | "throw"
            | "throws"
            | "transient"
            | "try"
            | "void"
            | "volatile"
            | "while"
            | "true"
            | "false"
            | "null"
            // `_` is a reserved identifier since Java 9 (Alipay's
            // instant-run fields are named `_`) — the `_<name>` mapping
            // turns it into `__`, matching jdc-core's call sites.
            | "_"
    );
    // A simple name may not START with a digit either (WhatsApp nests
    // `X/0Xx`): the declaration site and every reference (jdc-core's
    // sanitize_source_name) prefix the same underscore.
    let digit_start = name.chars().next().is_some_and(|c| c.is_ascii_digit());
    if clean && !keyword && !digit_start {
        std::borrow::Cow::Borrowed(name)
    } else if keyword || digit_start {
        std::borrow::Cow::Owned(format!("_{name}"))
    } else {
        // Non-ASCII single chars (Alipay names a field `支`) map to a
        // lone `_` — itself reserved since Java 9. Escape it.
        let mapped: String = name
            .chars()
            .map(|c| {
                if c.is_ascii_alphanumeric() || c == '_' || c == '$' {
                    c
                } else {
                    '_'
                }
            })
            .collect();
        if mapped == "_" {
            std::borrow::Cow::Owned("__".to_string())
        } else {
            std::borrow::Cow::Owned(mapped)
        }
    }
}

fn split_name(internal: &str) -> (String, String) {
    match internal.rfind('/') {
        Some(i) => (internal[..i].to_string(), internal[i + 1..].to_string()),
        None => (String::new(), internal.to_string()),
    }
}

/// Dotted source form of an internal name.
pub fn dotted(internal: &str) -> String {
    let mut out = internal.replace('/', ".");
    // `$` → `.` only when the following segment can start a Java
    // identifier (anonymous/synthetic tails stay `$`).
    let mut i = 0;
    while let Some(p) = out[i..].find('$') {
        let at = i + p;
        // A LEADING `$` (ProGuard keeps `$Gson$Types`) is part of the
        // source name — dotting it produced a leading `.Gson.Types`.
        let head_ok = at > 0
            && out[..at]
                .chars()
                .next_back()
                .is_some_and(|c| c.is_ascii_alphanumeric() || c == '_');
        let tail_ok = out[at + 1..]
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
        if tail_ok && head_ok {
            out.replace_range(at..at + 1, ".");
            i = at + 1;
        } else {
            i = at + 1;
        }
    }
    // Every `.`-segment must start an identifier: WhatsApp's `X/0Hl`
    // reached `extends` with the digit start unmapped.
    sanitize_fq(&out)
}

fn join_dotted(pool: &DexPool, names: &[String]) -> String {
    names
        .iter()
        .map(|n| print_class_name(pool, n))
        .collect::<Vec<_>>()
        .join(", ")
}

/// A printable type name (arrays render with `[]` suffixes). Nested class
/// names dot their `$` when the outer chain is known to the pool.
/// Split a concatenated method-descriptor ARGUMENT string
/// (`"Ljava/lang/String;I[Ljava/lang/Object;"`) into per-parameter
/// `JavaType`s. Used by the framework-ancestor ctor bridge, where the
/// referenced sig comes from the method-id table (no pool ancestor
/// method to read a parsed descriptor from).
fn split_arg_descs(sig: &str) -> Vec<JavaType> {
    let mut out = Vec::new();
    let b = sig.as_bytes();
    let mut i = 0usize;
    while i < b.len() {
        let start = i;
        while i < b.len() && b[i] == b'[' {
            i += 1;
        }
        if i < b.len() {
            if b[i] == b'L' {
                while i < b.len() && b[i] != b';' {
                    i += 1;
                }
                if i < b.len() {
                    i += 1; // consume ';'
                }
            } else {
                i += 1; // primitive single char
            }
        }
        if i > start {
            out.push(desc_type(&sig[start..i]));
        } else {
            break; // malformed: avoid an infinite loop
        }
    }
    out
}

pub fn type_name(pool: &DexPool, t: &JavaType) -> String {
    match t {
        JavaType::Void => "void".into(),
        JavaType::Object(n) => print_class_name(pool, n),
        JavaType::Array(inner) => format!("{}[]", type_name(pool, inner)),
        other => other.to_java(false),
    }
}

/// `com/foo/Outer$Inner` → `com.foo.Outer.Inner` — each `$` dots only when
/// its left side names a known class (literal `$` top-level names survive).
// ---------------------------------------------------------------------------
// The per-class import map (JLS 6.4.2 obscuring repair).
//
// A referenced FQN whose FIRST segment equals an in-scope class simple
// name binds the qualifier to the CLASS, not the package — `class j3
// implements j3.g` is cyclic inheritance and collapses javac's
// attribution for the whole package (the Object-cascade root, weixin
// 84 files + contagion). The repair: emit `import j3.g;` and render
// the SIMPLE name in every type position. The set is populated per
// class from a metadata pre-scan (supertypes, field types, method
// signature descriptors) and read by print_class_name and the
// DexCtx::obscured_simple implementation (which jdc-core's shorten /
// java_type_name consult).
struct ObscureState {
    /// The ROOT class being rendered (its simple name is the obscuring
    /// in-scope name).
    class: String,
    /// Metadata pre-scan results: internal → simple render.
    map: jdc_core::FxHashMap<String, String>,
    /// Expression-level obscured refs DISCOVERED during the body render
    /// (their imports emit at assembly time).
    recorded: jdc_core::FxHashSet<String>,
    /// Simple names of the CURRENT PACKAGE's own classes: importing a
    /// name that collides would SHADOW every same-package use of it in
    /// this file (JLS 7.5.1) — those refs stay qualified (obscured,
    /// erroring) rather than corrupt.
    blocked: jdc_core::FxHashSet<String>,
}

thread_local! {
    static OBSCURE: std::cell::RefCell<Option<ObscureState>> =
        const { std::cell::RefCell::new(None) };
}

/// Install the per-class render state (call at class render entry).
pub(crate) fn set_obscured_state(
    class: String,
    map: jdc_core::FxHashMap<String, String>,
    blocked: jdc_core::FxHashSet<String>,
) {
    OBSCURE.with(|m| {
        *m.borrow_mut() = Some(ObscureState {
            class,
            map,
            recorded: jdc_core::FxHashSet::default(),
            blocked,
        });
    });
}

/// Simple names this class's file renders through the import layer
/// (metadata map values + dynamically recorded nested tails). A method
/// local with one of these names captures the imported simple name in
/// expression position (locals beat single-type imports — `h.e` binds
/// a local int `h`, 无法取消引用int); deshadow_locals renames them.
/// Empty when no render state is installed (progressive paths).
pub(crate) fn obscured_simples_snapshot() -> jdc_core::FxHashSet<String> {
    OBSCURE.with(|m| {
        let mut out = jdc_core::FxHashSet::default();
        let Ok(st) = m.try_borrow() else { return out };
        let Some(st) = st.as_ref() else { return out };
        for simple in st.map.values() {
            out.insert(simple.clone());
        }
        for internal in &st.recorded {
            if let Some(tail) = internal.rsplit(['/', '$']).next() {
                if !tail.is_empty() {
                    out.insert(tail.to_string());
                }
            }
        }
        out
    })
}

/// Take the recorded expression-level refs and clear the state.
pub(crate) fn take_recorded_and_clear() -> jdc_core::FxHashSet<String> {
    OBSCURE.with(|m| {
        m.borrow_mut()
            .take()
            .map(|st| st.recorded)
            .unwrap_or_default()
    })
}

// ---- cross-package access widening ----------------------------------
// ART tolerates cross-package access to non-public classes/members that
// R8/d8 preserved; Java source cannot express it ("是在不可访问的类或
// 接口中定义的", "k在kotlinx.coroutines中不是公共的", "unknownFields 在
// l6 中是 protected"). The census scan records every cross-package
// reference; the installer keeps the non-public targets and the
// declaration renders widen them to public.

struct WidenState {
    classes: jdc_core::FxHashSet<String>,
    methods: jdc_core::FxHashMap<String, jdc_core::FxHashSet<(String, String)>>,
    fields: jdc_core::FxHashMap<String, jdc_core::FxHashSet<String>>,
}

static WIDEN: std::sync::OnceLock<WidenState> = std::sync::OnceLock::new();

pub(crate) fn install_access_widening(pool: &crate::DexPool) {
    let t0 = std::time::Instant::now();
    let raw = crate::refscan::access_widening_scan(pool.dexes());
    let mut classes: jdc_core::FxHashSet<String> = jdc_core::FxHashSet::default();
    for c in &raw.classes {
        if let Some(pc) = pool.get_if_materialized(c) {
            if pc.access & crate::access::ACC_PUBLIC == 0 {
                classes.insert(c.clone());
            }
        }
    }
    let mut methods: jdc_core::FxHashMap<String, jdc_core::FxHashSet<(String, String)>> =
        jdc_core::FxHashMap::default();
    for (owner, set) in &raw.methods {
        let Some(pc) = pool.get_if_materialized(owner) else {
            continue;
        };
        let is_enum = pc.access & crate::access::ACC_ENUM != 0;
        let mut keep: jdc_core::FxHashSet<(String, String)> =
            jdc_core::FxHashSet::default();
        for (name, desc) in set {
            // Enum ctors are source-level private-only; clinit is never
            // referenced. Widening either is a javac "modifier not
            // allowed here".
            if name == "<clinit>" || (is_enum && name == "<init>") {
                continue;
            }
            let nonpub = pc
                .all_methods()
                .any(|m| &*m.name == name.as_str() && &*m.desc == desc.as_str()
                    && m.access & crate::access::ACC_PUBLIC == 0);
            if nonpub {
                keep.insert((name.clone(), desc.clone()));
            }
        }
        if !keep.is_empty() {
            methods.insert(owner.clone(), keep);
        }
    }
    // Override-closure DOWN the hierarchy: a widened (public) super
    // method whose subclass override stays protected/package is
    // "无法覆盖...更低的访问权限" (lark +823). Every transitive
    // subclass declaring the same (name, desc) non-public widens too.
    {
        let mut subs: jdc_core::FxHashMap<String, Vec<String>> =
            jdc_core::FxHashMap::default();
        for name in &pool.order {
            let Some(pc) = pool.get_if_materialized(name) else {
                continue;
            };
            if let Some(sup) = &pc.super_name {
                subs.entry(sup.clone()).or_default().push(name.clone());
            }
            for i in &pc.interfaces {
                subs.entry(i.clone()).or_default().push(name.clone());
            }
        }
        let seeds: Vec<(String, String, String)> = methods
            .iter()
            .flat_map(|(o, set)| {
                set.iter().map(|(n, d)| (o.clone(), n.clone(), d.clone()))
            })
            .collect();
        for (owner, mname, mdesc) in seeds {
            let mut queue: Vec<String> = match subs.get(&owner) {
                Some(v) => v.clone(),
                None => continue,
            };
            let mut seen: jdc_core::FxHashSet<String> = jdc_core::FxHashSet::default();
            while let Some(c) = queue.pop() {
                if !seen.insert(c.clone()) {
                    continue;
                }
                let Some(pc) = pool.get_if_materialized(&c) else {
                    continue;
                };
                let declares_nonpub = pc.all_methods().any(|m| {
                    &*m.name == mname.as_str()
                        && &*m.desc == mdesc.as_str()
                        && m.access & crate::access::ACC_PUBLIC == 0
                });
                if declares_nonpub {
                    methods
                        .entry(c.clone())
                        .or_default()
                        .insert((mname.clone(), mdesc.clone()));
                }
                if let Some(v) = subs.get(&c) {
                    queue.extend(v.iter().cloned());
                }
            }
        }
    }
    let mut fields: jdc_core::FxHashMap<String, jdc_core::FxHashSet<String>> =
        jdc_core::FxHashMap::default();
    for (owner, set) in &raw.fields {
        let Some(pc) = pool.get_if_materialized(owner) else {
            continue;
        };
        let keep: jdc_core::FxHashSet<String> = set
            .iter()
            .filter(|name| {
                pc.static_fields
                    .iter()
                    .chain(pc.instance_fields.iter())
                    .any(|f| f.name == name.as_str()
                        && f.access & crate::access::ACC_PUBLIC == 0)
            })
            .cloned()
            .collect();
        if !keep.is_empty() {
            fields.insert(owner.clone(), keep);
        }
    }
    if std::env::var("DDC_STATS").is_ok() {
        eprintln!(
            "[renames] access widening: classes={} method-owners={} field-owners={} scan={:?}",
            classes.len(),
            methods.len(),
            fields.len(),
            t0.elapsed()
        );
    }
    let _ = WIDEN.set(WidenState { classes, methods, fields });
}

#[inline]
pub(crate) fn widen_class(internal: &str) -> bool {
    WIDEN.get().is_some_and(|w| w.classes.contains(internal))
}

#[inline]
pub(crate) fn widen_method(owner: &str, name: &str, desc: &str) -> bool {
    WIDEN.get().is_some_and(|w| {
        w.methods
            .get(owner)
            .is_some_and(|s| s.contains(&(name.to_string(), desc.to_string())))
    })
}

#[inline]
pub(crate) fn widen_field(owner: &str, name: &str) -> bool {
    WIDEN.get().is_some_and(|w| {
        w.fields.get(owner).is_some_and(|s| s.contains(name))
    })
}

/// Render-time lookup: the metadata map first; otherwise an
/// expression-level ref whose first segment equals the root class's
/// simple name — record it (its import emits at assembly) and render
/// the simple name NOW.
pub(crate) fn obscured_render_pub(internal: &str) -> Option<String> {
    OBSCURE.with(|m| {
        let mut st = m.borrow_mut();
        let st = st.as_mut()?;
        if let Some(simple) = st.map.get(internal) {
            return Some(simple.clone());
        }
        let first = internal.split('/').next().unwrap_or("");
        let own = st.class.rsplit('/').next().unwrap_or("");
        if first == own && internal.split('/').count() >= 2 {
            // A NESTED internal's in-scope simple name is its `$` tail
            // (`x/a$b` imports/renders as `b`) — the slash-tail left the
            // `$` in the render (`t2$a` flat against a nested emission).
            let simple = internal
                .rsplit(['/', '$'])
                .next()
                .unwrap_or("")
                .to_string();
            if !simple.is_empty() && !st.blocked.contains(&simple) {
                st.recorded.insert(internal.to_string());
                return Some(simple);
            }
        }
        None
    })
}

/// The pre-scan: internal names referenced by the class's metadata
/// (supertypes, field types, method descriptors) whose first segment
/// equals the class's own simple name and which exist in the pool —
/// these get imports and simple-name renders.
fn compute_obscured_renders(pool: &DexPool, class: &PoolClass) -> jdc_core::FxHashMap<String, String> {
    let own_simple = class.name.rsplit('/').next().unwrap_or("");
    if own_simple.is_empty() {
        return jdc_core::FxHashMap::default();
    }
    let mut refs: jdc_core::FxHashSet<String> = jdc_core::FxHashSet::default();
    for sup in class.interfaces.iter() {
        refs.insert(sup.clone());
    }
    if let Some(sup) = &class.super_name {
        refs.insert(sup.clone());
    }
    for f in class.static_fields.iter().chain(class.instance_fields.iter()) {
        if let JavaType::Object(n) = crate::desc_type(&f.desc) {
            refs.insert(n.to_string());
        }
    }
    for m in class.all_methods() {
        if let Some(d) = m.parsed_desc() {
            for a in d.args.iter() {
                if let JavaType::Object(n) = a {
                    refs.insert(n.to_string());
                }
            }
            if let JavaType::Object(n) = &d.ret {
                refs.insert(n.to_string());
            }
        }
    }
    let mut out: jdc_core::FxHashMap<String, String> = jdc_core::FxHashMap::default();
    for r in refs {
        // First segment == the class's own simple name: the qualified
        // render would be obscured. The name must exist in the pool for
        // the import to resolve. The KEY stays internal (renders look
        // refs up by internal name), but the import line and the
        // simple name use the RENAMED display — a collision-renamed
        // class renders under its display name and an import of the
        // internal name does not resolve.
        let first = r.split('/').next().unwrap_or("");
        if first == own_simple && r.split('/').count() >= 2 && pool.get(&r).is_some() {
            // Own-family refs (the class itself, its nested members)
            // render through member scope — an import would be redundant
            // (self-import) or shadow a same-package sibling named like
            // the tail (`import t.t2.a` hijacks every bare `a` that
            // meant sibling class t.a — lark t/t2).
            if r == class.name || r.starts_with(&format!("{}$", class.name)) {
                continue;
            }
            let display = crate::apply_class_rename(&r);
            let simple = display
                .rsplit(['/', '$'])
                .next()
                .unwrap_or("")
                .to_string();
            if !simple.is_empty() {
                out.insert(r.clone(), simple);
            }
        }
    }
    out
}

pub fn print_class_name(pool: &DexPool, internal: &str) -> String {
    if let Some(simple) = obscured_render_pub(internal) {
        return sanitize_ref(&simple);
    }
    let cow = crate::apply_class_rename(internal);
    let internal: &str = &cow;
    // Flat EMISSION UNITS: a digit-tail member (anonymous / d8-lambda
    // shape, `Outer$lruCache$1`) is emitted as its own top-level file
    // whose simple name keeps every `$` — the `$` boundaries inside
    // that unit are part of the NAME, not nesting. References must use
    // the flat unit name; the generic loop below dotted the first
    // boundary (`LruCacheKt.lruCache$1`) against the declaration
    // `class LruCacheKt$lruCache$1` — every use of the type failed and
    // javac's attribution for the whole file collapsed (3627 pure-
    // cascade files on weibo).
    if internal.contains('$') && pool.get(internal).is_some() {
        if let Some(root) = emission_root(pool, internal) {
            if root.contains('$') {
                let below = &internal[root.len()..];
                if below.is_empty() {
                    // The unit itself: the flat name IS the reference.
                    return sanitize_ref(&internal.replace('/', "."));
                }
                let segs: Vec<&str> = below[1..].split('$').collect();
                if segs.iter().any(|s| s.is_empty()) {
                    // R8 tails like `ThreadMsg$$$` cannot be dotted at
                    // all — keep the whole name flat.
                    return sanitize_ref(&internal.replace('/', "."));
                }
                let mut out = root.replace('/', ".");
                for s in segs {
                    out.push('.');
                    out.push_str(s);
                }
                return sanitize_ref(&out);
            }
        }
    }
    let mut out = String::new();
    // `known` must test the ACCUMULATED internal prefix, not the bare
    // inter-`$` segment: the per-segment shape checked `pool.get("a")`
    // for the second level of `s5/o$a$b`, missed, and rendered the
    // undeclarable reference `s5.o.a$b` (找不到符号) for every nested-
    // nested type.
    let mut off = 0usize;
    loop {
        let rest = &internal[off..];
        match rest.find('$') {
            Some(i) => {
                let seg = &rest[..i];
                let prefix = &internal[..off + i];
                let known = pool.get(prefix).is_some()
                    || jdc_core::rename::is_renamed_display(prefix)
                    // The FULL name is not a pool class: this `$` cannot
                    // be a literal name (pool literal classes — an app's
                    // own `View$OnUnhandledKeyEventListener` — keep their
                    // `$` here AND at their declaration), so it can only
                    // be an external framework nesting boundary
                    // (`View$OnClickListener` → `.OnClickListener`).
                    || !pool.get(internal).is_some();
                // The `$` may only become a nesting dot when the tail
                // segment STARTS a Java identifier: R8's desugared-
                // library names carry `$` inside PACKAGE paths
                // (`j$/util/...` dotted into `j..util`) and suffixes
                // like `Collection$-EL` or anonymous `RequestId$1`
                // cannot be dotted under any reading.
                let tail_ok = rest[i + 1..]
                    .chars()
                    .next()
                    .is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
                out.push_str(&seg.replace('/', "."));
                out.push_str(if known && tail_ok { "." } else { "$" });
                off += i + 1;
            }
            None => {
                out.push_str(&rest.replace('/', "."));
                return sanitize_ref(&out);
            }
        }
    }
}

/// The top-level EMISSION UNIT ancestor of a pool class: walking the
/// `$`-chain upward (outer_of), the first ancestor that is itself
/// emitted as its own compilation unit — a digit-tail rest marks a
/// flat boundary (see top_level_classes). Clean members render inline
/// in their outer, so only the unit's own name may carry `$`.
fn emission_root(pool: &DexPool, internal: &str) -> Option<String> {
    let mut cur = internal.to_string();
    loop {
        let outer = pool.outer_of(&cur)?;
        let rest = cur
            .strip_prefix(outer)
            .and_then(|t| t.strip_prefix('$'))
            .unwrap_or("");
        if !crate::clean_member_tail(rest) {
            return Some(cur);
        }
        cur = outer.to_string();
    }
}

/// Class-file names may contain characters Java source identifiers
/// cannot (`Collection$-EL`); the deterministic mapping matches the
/// declaration sites (java_ident).
/// Every `.`-segment of a fully-qualified name must start a Java
/// identifier: obfuscators emit `package do;` and `..badge.new..` paths.
pub(crate) fn sanitize_fq(dotted: &str) -> String {
    dotted
        .split('.')
        .map(|seg| {
            if is_java_keyword_name(seg)
                || is_restricted_type_name(seg)
                || seg.chars().next().is_some_and(|c| c.is_ascii_digit())
            {
                format!("_{seg}")
            } else if seg
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
            {
                seg.to_string()
            } else {
                // Non-ASCII single chars (rimet nests classes named `ˆ`
                // / `ァ`) map to a lone `_` — reserved since Java 9.
                let mapped: String = seg
                    .chars()
                    .map(|c| {
                        if c.is_ascii_alphanumeric() || c == '_' || c == '$' {
                            c
                        } else {
                            '_'
                        }
                    })
                    .collect();
                if mapped == "_" {
                    "__".to_string()
                } else {
                    mapped
                }
            }
        })
        .collect::<Vec<_>>()
        .join(".")
}

/// Restricted contextual TYPE names — legal as member/local names
/// (rt.jar compiles `var` locals), illegal in class declarations and
/// type references. Consulted only on CLASS-name paths.
pub(crate) fn is_restricted_type_name(s: &str) -> bool {
    matches!(s, "var" | "yield" | "record" | "sealed" | "permits")
}

fn is_java_keyword_name(s: &str) -> bool {
    matches!(
        s,
        "abstract"
            | "assert"
            | "boolean"
            | "break"
            | "byte"
            | "case"
            | "catch"
            | "char"
            | "class"
            | "const"
            | "continue"
            | "default"
            | "do"
            | "double"
            | "else"
            | "enum"
            | "extends"
            | "final"
            | "finally"
            | "float"
            | "for"
            | "goto"
            | "if"
            | "implements"
            | "import"
            | "instanceof"
            | "int"
            | "interface"
            | "long"
            | "native"
            | "new"
            | "package"
            | "private"
            | "protected"
            | "public"
            | "return"
            | "short"
            | "static"
            | "strictfp"
            | "super"
            | "switch"
            | "synchronized"
            | "this"
            | "throw"
            | "throws"
            | "transient"
            | "try"
            | "void"
            | "volatile"
            | "while"
            | "true"
            | "false"
            | "null"
            // `_` is a reserved identifier since Java 9 (Alipay's
            // instant-run fields are named `_`) — the `_<name>` mapping
            // turns it into `__`, matching jdc-core's call sites.
            | "_"
    )
}

fn sanitize_ref(name: &str) -> String {
    sanitize_fq(name)
}

/// Dotted source form of an internal name.
pub fn dotted_pool(pool: &DexPool, internal: &str) -> String {
    print_class_name(pool, internal)
}

/// Unused import silencer.
#[allow(dead_code)]
fn _unused(_: &dyn Fn(&JavaType) -> jdc_core::types::GenericType) {
    let _ = java_type_to_generic;
}