uniffi-bindgen-java 0.4.2

a java bindings generator for uniffi rust
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
use anyhow::{Context, Result};
use askama::Template;
use core::fmt::Debug;
use heck::{ToLowerCamelCase, ToShoutySnakeCase, ToUpperCamelCase};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{
    borrow::Borrow,
    cell::RefCell,
    collections::{HashMap, HashSet},
};
use uniffi_bindgen::{interface::*, to_askama_error};

mod callback_interface;
mod compounds;
mod custom;
mod enum_;
mod miscellany;
mod object;
mod primitives;
mod record;
mod variant;

/// Insert a JSpecify `@Nullable` TYPE_USE annotation at the correct position
/// in a (possibly fully-qualified) Java type name.
///
/// Per JLS §9.7.4, TYPE_USE annotations must appear directly before the simple
/// name of the type, not before the package/enclosing-class qualifier:
///   - `java.lang.String`          → `java.lang.@Nullable String`
///   - `UniffiCleaner.Cleanable`   → `UniffiCleaner.@Nullable Cleanable`
///   - `String`                    → `@Nullable String`
///   - `java.util.Map<K, V>`       → `java.util.@Nullable Map<K, V>`
pub(crate) fn nullable_type_label(type_label: &str) -> String {
    const ANNOTATION: &str = "@org.jspecify.annotations.Nullable ";
    // Only look at the portion before any generic '<' to find the right '.'
    let before_generics = type_label.find('<').unwrap_or(type_label.len());
    let prefix = &type_label[..before_generics];
    if let Some(dot_pos) = prefix.rfind('.') {
        format!(
            "{}{}{}",
            &type_label[..dot_pos + 1],
            ANNOTATION,
            &type_label[dot_pos + 1..]
        )
    } else {
        format!("{}{}", ANNOTATION, type_label)
    }
}

pub fn potentially_add_external_package(
    config: &Config,
    ci: &ComponentInterface,
    type_name: &str,
    display_name: String,
) -> String {
    match ci.get_type(type_name) {
        Some(typ) => {
            if ci.is_external(&typ) {
                format!(
                    "{}.{}",
                    config.external_type_package_name(typ.module_path().unwrap(), &display_name),
                    display_name
                )
            } else {
                display_name
            }
        }
        None => display_name,
    }
}

trait CodeType: Debug {
    /// The language specific label used to reference this type. This will be used in
    /// method signatures and property declarations.
    fn type_label(&self, ci: &ComponentInterface, config: &Config) -> String;

    /// The primitive type label if this type is a primitive (int, long, boolean, etc.).
    /// Returns None for non-primitive types.
    fn type_label_primitive(&self) -> Option<String> {
        None
    }

    /// A representation of this type label that can be used as part of another
    /// identifier. e.g. `read_foo()`, or `FooInternals`.
    ///
    /// This is especially useful when creating specialized objects or methods to deal
    /// with this type only.
    fn canonical_name(&self) -> String;

    /// Instance of the FfiConverter
    ///
    /// This is the object that contains the lower, write, lift, and read methods for this type.
    /// Depending on the binding this will either be a singleton or a class with static methods.
    ///
    /// This is the newer way of handling these methods and replaces the lower, write, lift, and
    /// read CodeType methods.
    fn ffi_converter_name(&self) -> String {
        format!("FfiConverter{}", self.canonical_name())
    }

    /// Name of the FfiConverter
    ///
    /// This is the object that contains the lower, write, lift, and read methods for this type.
    /// Depending on the binding this will either be a singleton or a class with static methods.
    ///
    /// This is the newer way of handling these methods and replaces the lower, write, lift, and
    /// read CodeType methods.
    fn ffi_converter_instance(&self, _config: &Config, _ci: &ComponentInterface) -> String {
        format!("{}.INSTANCE", self.ffi_converter_name())
    }

    /// Function to run at startup
    fn initialization_fn(&self) -> Option<String> {
        None
    }
}

// taken from https://docs.oracle.com/javase/specs/ section 3.9
static KEYWORDS: Lazy<HashSet<String>> = Lazy::new(|| {
    let kwlist = vec![
        "abstract",
        "continue",
        "for",
        "new",
        "switch",
        "assert",
        "default",
        "if",
        "package",
        "synchronized",
        "boolean",
        "do",
        "goto",
        "private",
        "this",
        "break",
        "double",
        "implements",
        "protected",
        "throw",
        "byte",
        "else",
        "import",
        "public",
        "throws",
        "case",
        "enum",
        "instanceof",
        "return",
        "transient",
        "catch",
        "extends",
        "int",
        "short",
        "try",
        "char",
        "final",
        "interface",
        "static",
        "void",
        "class",
        "finally",
        "long",
        "strictfp",
        "volatile",
        "const",
        "float",
        "native",
        "super",
        "while",
        "_",
    ];
    HashSet::from_iter(kwlist.into_iter().map(|s| s.to_string()))
});

// config options to customize the generated Java.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Config {
    pub(super) package_name: Option<String>,
    pub(super) cdylib_name: Option<String>,
    generate_immutable_records: Option<bool>,
    #[serde(default)]
    omit_checksums: bool,
    #[serde(default)]
    custom_types: HashMap<String, CustomTypeConfig>,
    #[serde(default)]
    pub(super) external_packages: HashMap<String, String>,
    #[serde(default)]
    android: bool,
    #[serde(default)]
    nullness_annotations: bool,
    /// Renames for types, fields, methods, variants, and arguments.
    /// Uses dot notation: "OldRecord" = "NewRecord", "OldRecord.field" = "new_field"
    #[serde(default)]
    pub(super) rename: toml::Table,
}

impl Config {
    pub fn omit_checksums(&self) -> bool {
        self.omit_checksums
    }

    /// Whether to generate PanamaPort imports for Android compatibility.
    /// When true, post-processes generated code to replace `java.lang.foreign.*` with
    /// PanamaPort's `com.v7878.foreign.*` and `java.lang.invoke.VarHandle` with
    /// `com.v7878.invoke.VarHandle`.
    pub fn android(&self) -> bool {
        self.android
    }

    /// Whether to generate JSpecify `@NullMarked` and `@Nullable` annotations.
    pub fn nullness_annotations(&self) -> bool {
        self.nullness_annotations
    }
}

impl Config {
    pub fn package_name(&self) -> String {
        if let Some(package_name) = &self.package_name {
            package_name.clone()
        } else {
            "uniffi".into()
        }
    }

    pub fn cdylib_name(&self) -> String {
        if let Some(cdylib_name) = &self.cdylib_name {
            cdylib_name.clone()
        } else {
            "uniffi".into()
        }
    }

    /// Whether to generate immutable records (`record` instead of `class`)
    pub fn generate_immutable_records(&self) -> bool {
        self.generate_immutable_records.unwrap_or(false)
    }

    // Get the package name for an external type
    pub fn external_type_package_name(&self, module_path: &str, namespace: &str) -> String {
        // config overrides are keyed by the crate name, default fallback is the namespace.
        let crate_name = module_path.split("::").next().unwrap();
        match self.external_packages.get(crate_name) {
            Some(name) => name.clone(),
            // unreachable in library mode - all deps are in our config with correct namespace.
            None => format!("uniffi.{namespace}"),
        }
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CustomTypeConfig {
    imports: Option<Vec<String>>,
    type_name: Option<String>,
    into_custom: String, // backcompat alias for lift
    lift: String,
    from_custom: String, // backcompat alias for lower
    lower: String,
}

// functions replace literal "{}" in strings with a specified value.
impl CustomTypeConfig {
    fn lift(&self, name: &str) -> String {
        let converter = if self.lift.is_empty() {
            &self.into_custom
        } else {
            &self.lift
        };
        converter.replace("{}", name)
    }
    fn lower(&self, name: &str) -> String {
        let converter = if self.lower.is_empty() {
            &self.from_custom
        } else {
            &self.lower
        };
        converter.replace("{}", name)
    }
}

// Generate Java bindings for the given ComponentInterface, as a string.
pub fn generate_bindings(config: &Config, ci: &ComponentInterface) -> Result<String> {
    let output = JavaWrapper::new(config.clone(), ci)
        .render()
        .context("failed to render java bindings")?;

    if config.android() {
        // PanamaPort provides the FFM API under a different package prefix.
        // The API surface is identical so a global string replacement is sufficient.
        // `java.lang.invoke.MethodHandle/MethodHandles/MethodType` are available on
        // Android API 26+ and are intentionally left unchanged.
        Ok(output
            .replace("java.lang.foreign.", "com.v7878.foreign.")
            .replace("java.lang.invoke.VarHandle", "com.v7878.invoke.VarHandle"))
    } else {
        Ok(output)
    }
}

#[derive(Template)]
#[template(syntax = "java", escape = "none", path = "wrapper.java")]
pub struct JavaWrapper<'a> {
    config: Config,
    ci: &'a ComponentInterface,
    type_helper_code: String,
}

impl<'a> JavaWrapper<'a> {
    pub fn new(config: Config, ci: &'a ComponentInterface) -> Self {
        let type_renderer = TypeRenderer::new(&config, ci);
        let type_helper_code = type_renderer.render().unwrap();
        Self {
            config,
            ci,
            type_helper_code,
        }
    }

    pub fn initialization_fns(&self) -> Vec<String> {
        self.ci
            .iter_local_types()
            .map(|t| JavaCodeOracle.find(t))
            .filter_map(|ct| ct.initialization_fn())
            .collect()
    }

    /// Get the namespace class name, avoiding collisions with type names.
    /// If the namespace name (after camelCase conversion) collides with any type,
    /// append "Lib" suffix repeatedly until there's no collision.
    pub fn namespace_class_name(&self) -> String {
        let base_name = JavaCodeOracle.class_name(self.ci, self.ci.namespace());

        // Collect all type names for collision checking
        let type_names: HashSet<String> = self
            .ci
            .iter_local_types()
            .map(|t| JavaCodeOracle.find(t).type_label(self.ci, &self.config))
            .collect();

        // Keep adding "Lib" suffix until there's no collision
        let mut candidate = base_name;
        while type_names.contains(&candidate) {
            candidate = format!("{}Lib", candidate);
        }
        candidate
    }
}

/// Renders Java helper code for all types
///
/// This template is a bit different than others in that it stores internal state from the render
/// process.  Make sure to only call `render()` once.
#[derive(Template)]
#[template(syntax = "java", escape = "none", path = "Types.java")]
pub struct TypeRenderer<'a> {
    config: &'a Config,
    ci: &'a ComponentInterface,
    // Track included modules for the `include_once()` macro
    include_once_names: RefCell<HashSet<String>>,
}

impl<'a> TypeRenderer<'a> {
    fn new(config: &'a Config, ci: &'a ComponentInterface) -> Self {
        Self {
            config,
            ci,
            include_once_names: RefCell::new(HashSet::new()),
        }
    }

    // The following methods are used by the `Types.java` macros.

    // Helper for the including a template, but only once.
    //
    // The first time this is called with a name it will return true, indicating that we should
    // include the template.  Subsequent calls will return false.
    fn include_once_check(&self, name: &str) -> bool {
        self.include_once_names
            .borrow_mut()
            .insert(name.to_string())
    }

    // Get the package name for an external type (used by ExternalTypeTemplate.java)
    fn external_type_package_name(&self, module_path: &str, namespace: &str) -> String {
        self.config
            .external_type_package_name(module_path, namespace)
    }
}

fn fixup_keyword(name: String) -> String {
    if KEYWORDS.contains(&name) {
        format!("_{name}")
    } else {
        name
    }
}

#[derive(Clone)]
pub struct JavaCodeOracle;

impl JavaCodeOracle {
    fn find(&self, type_: &Type) -> Box<dyn CodeType> {
        type_.clone().as_type().as_codetype()
    }

    /// Get the idiomatic Java rendering of a class name (for enums, records, errors, etc).
    fn class_name(&self, ci: &ComponentInterface, nm: &str) -> String {
        let name = nm.to_string().to_upper_camel_case();
        // fixup errors.
        fixup_keyword(if ci.is_name_used_as_error(nm) {
            self.convert_error_suffix(&name)
        } else {
            name
        })
    }

    fn convert_error_suffix(&self, nm: &str) -> String {
        match nm.strip_suffix("Error") {
            Some(stripped) if !stripped.is_empty() => format!("{stripped}Exception"),
            _ => nm.to_string(),
        }
    }

    /// Get the idiomatic Java rendering of a function name.
    fn fn_name(&self, nm: &str) -> String {
        fixup_keyword(nm.to_string().to_lower_camel_case())
    }

    /// Get the idiomatic Java rendering of a variable name.
    pub fn var_name(&self, nm: &str) -> String {
        fixup_keyword(self.var_name_raw(nm))
    }

    /// `var_name` without the reserved word alteration.  Useful for struct field names.
    pub fn var_name_raw(&self, nm: &str) -> String {
        nm.to_string().to_lower_camel_case()
    }

    /// Get the idiomatic setter name for a variable.
    pub fn setter(&self, nm: &str) -> String {
        format!("set{}", fixup_keyword(nm.to_string().to_upper_camel_case()))
    }

    /// Get the idiomatic Java rendering of an individual enum variant.
    fn enum_variant_name(&self, nm: &str) -> String {
        nm.to_string().to_shouty_snake_case()
    }

    /// Get the idiomatic Java rendering of an FFI callback function name
    fn ffi_callback_name(&self, nm: &str) -> String {
        format!("Uniffi{}", nm.to_upper_camel_case())
    }

    /// Get the idiomatic Java rendering of an FFI struct name
    fn ffi_struct_name(&self, nm: &str) -> String {
        format!("Uniffi{}", nm.to_upper_camel_case())
    }

    /// FFI type label for use in method signatures and MethodHandle wrapper methods.
    /// In FFM, primitives stay as primitives, everything else is MemorySegment
    /// (except Handle/RustArcPtr which are long).
    fn ffi_type_label(
        &self,
        ffi_type: &FfiType,
        _config: &Config,
        _ci: &ComponentInterface,
    ) -> String {
        match ffi_type {
            // Note that unsigned values in Java don't have true native support. Signed primitives
            // can contain unsigned values and there are methods like `Integer.compareUnsigned`
            // that respect the unsigned value, but knowledge outside the type system is required.
            // TODO(java): improve callers knowledge of what contains an unsigned value
            FfiType::Int8 | FfiType::UInt8 => "byte".to_string(),
            FfiType::Int16 | FfiType::UInt16 => "short".to_string(),
            FfiType::Int32 | FfiType::UInt32 => "int".to_string(),
            FfiType::Int64 | FfiType::UInt64 => "long".to_string(),
            FfiType::Float32 => "float".to_string(),
            FfiType::Float64 => "double".to_string(),
            FfiType::Handle => "long".to_string(),
            // In FFM, all struct types, buffers, pointers, and callbacks are MemorySegment
            FfiType::RustBuffer(_)
            | FfiType::RustCallStatus
            | FfiType::ForeignBytes
            | FfiType::Callback(_)
            | FfiType::Struct(_)
            | FfiType::VoidPointer
            | FfiType::Reference(_)
            | FfiType::MutReference(_) => "java.lang.foreign.MemorySegment".to_string(),
        }
    }

    /// FFI type label using boxed types for use in generic contexts (e.g. FfiConverter<T, Long>).
    /// Java generics don't accept primitives.
    fn ffi_type_label_boxed(&self, ffi_type: &FfiType) -> String {
        match ffi_type {
            FfiType::Int8 | FfiType::UInt8 => "java.lang.Byte".to_string(),
            FfiType::Int16 | FfiType::UInt16 => "java.lang.Short".to_string(),
            FfiType::Int32 | FfiType::UInt32 => "java.lang.Integer".to_string(),
            FfiType::Int64 | FfiType::UInt64 => "java.lang.Long".to_string(),
            FfiType::Float32 => "java.lang.Float".to_string(),
            FfiType::Float64 => "java.lang.Double".to_string(),
            FfiType::Handle => "java.lang.Long".to_string(),
            _ => "java.lang.foreign.MemorySegment".to_string(),
        }
    }

    /// Maps FfiType to ValueLayout constant for FunctionDescriptor construction
    fn ffi_value_layout(&self, ffi_type: &FfiType) -> String {
        match ffi_type {
            FfiType::Int8 | FfiType::UInt8 => "java.lang.foreign.ValueLayout.JAVA_BYTE".to_string(),
            FfiType::Int16 | FfiType::UInt16 => {
                "java.lang.foreign.ValueLayout.JAVA_SHORT".to_string()
            }
            FfiType::Int32 | FfiType::UInt32 => {
                "java.lang.foreign.ValueLayout.JAVA_INT".to_string()
            }
            FfiType::Int64 | FfiType::UInt64 => {
                "java.lang.foreign.ValueLayout.JAVA_LONG".to_string()
            }
            FfiType::Float32 => "java.lang.foreign.ValueLayout.JAVA_FLOAT".to_string(),
            FfiType::Float64 => "java.lang.foreign.ValueLayout.JAVA_DOUBLE".to_string(),
            FfiType::Handle => "java.lang.foreign.ValueLayout.JAVA_LONG".to_string(),
            FfiType::RustBuffer(_) => "RustBuffer.LAYOUT".to_string(),
            // RustCallStatus is passed as a pointer in the C ABI
            FfiType::RustCallStatus => "java.lang.foreign.ValueLayout.ADDRESS".to_string(),
            FfiType::ForeignBytes => "ForeignBytes.LAYOUT".to_string(),
            FfiType::Struct(name) => format!("{}.LAYOUT", self.ffi_struct_name(name)),
            FfiType::Callback(_)
            | FfiType::VoidPointer
            | FfiType::Reference(_)
            | FfiType::MutReference(_) => "java.lang.foreign.ValueLayout.ADDRESS".to_string(),
        }
    }

    /// Returns the alignment requirement (in bytes) for an FFI type
    fn ffi_type_alignment(&self, ffi_type: &FfiType) -> usize {
        match ffi_type {
            FfiType::Int8 | FfiType::UInt8 => 1,
            FfiType::Int16 | FfiType::UInt16 => 2,
            FfiType::Int32 | FfiType::UInt32 | FfiType::Float32 => 4,
            FfiType::Int64 | FfiType::UInt64 | FfiType::Float64 | FfiType::Handle => 8,
            // Pointers are pointer-aligned (8 bytes on 64-bit)
            FfiType::Callback(_)
            | FfiType::VoidPointer
            | FfiType::Reference(_)
            | FfiType::MutReference(_) => 8,
            // Structs: alignment is the max alignment of their fields.
            // RustBuffer has JAVA_LONG (8) as first field → 8-byte aligned
            // RustCallStatus starts with JAVA_BYTE but contains RustBuffer → 8-byte aligned
            // ForeignBytes starts with JAVA_INT → but contains ADDRESS → 8-byte aligned
            FfiType::RustBuffer(_)
            | FfiType::RustCallStatus
            | FfiType::ForeignBytes
            | FfiType::Struct(_) => 8,
        }
    }

    /// Returns the size (in bytes) for an FFI type
    fn ffi_type_size(&self, ffi_type: &FfiType) -> usize {
        match ffi_type {
            FfiType::Int8 | FfiType::UInt8 => 1,
            FfiType::Int16 | FfiType::UInt16 => 2,
            FfiType::Int32 | FfiType::UInt32 | FfiType::Float32 => 4,
            FfiType::Int64 | FfiType::UInt64 | FfiType::Float64 | FfiType::Handle => 8,
            FfiType::Callback(_)
            | FfiType::VoidPointer
            | FfiType::Reference(_)
            | FfiType::MutReference(_) => 8,
            // Struct sizes: these are hardcoded based on their layouts
            FfiType::RustBuffer(_) => 24,  // long + long + pointer
            FfiType::RustCallStatus => 32, // byte + 7 pad + RustBuffer(24)
            FfiType::ForeignBytes => 16,   // int + 4 pad + pointer
            FfiType::Struct(_) => 0,       // unknown at codegen time, handled by LAYOUT
        }
    }

    /// Maps FfiType to layout for use as a struct field in StructLayout definitions.
    /// Unlike ffi_value_layout (for FunctionDescriptors), embedded structs use their
    /// full LAYOUT here rather than ADDRESS.
    fn ffi_struct_field_layout(&self, ffi_type: &FfiType) -> String {
        match ffi_type {
            FfiType::RustBuffer(_) => "RustBuffer.LAYOUT".to_string(),
            FfiType::RustCallStatus => "UniffiRustCallStatus.LAYOUT".to_string(),
            FfiType::ForeignBytes => "ForeignBytes.LAYOUT".to_string(),
            FfiType::Struct(name) => format!("{}.LAYOUT", self.ffi_struct_name(name)),
            _ => self.ffi_value_layout(ffi_type),
        }
    }

    /// Maps FfiType to UNALIGNED ValueLayout for struct field access
    fn ffi_value_layout_unaligned(&self, ffi_type: &FfiType) -> String {
        match ffi_type {
            FfiType::Int8 | FfiType::UInt8 => "java.lang.foreign.ValueLayout.JAVA_BYTE".to_string(), // byte has no alignment
            FfiType::Int16 | FfiType::UInt16 => {
                "java.lang.foreign.ValueLayout.JAVA_SHORT_UNALIGNED".to_string()
            }
            FfiType::Int32 | FfiType::UInt32 => {
                "java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED".to_string()
            }
            FfiType::Int64 | FfiType::UInt64 => {
                "java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED".to_string()
            }
            FfiType::Float32 => "java.lang.foreign.ValueLayout.JAVA_FLOAT_UNALIGNED".to_string(),
            FfiType::Float64 => "java.lang.foreign.ValueLayout.JAVA_DOUBLE_UNALIGNED".to_string(),
            FfiType::Handle => "java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED".to_string(),
            FfiType::Callback(_)
            | FfiType::VoidPointer
            | FfiType::Reference(_)
            | FfiType::MutReference(_) => {
                "java.lang.foreign.ValueLayout.ADDRESS_UNALIGNED".to_string()
            }
            // Structs use slice-based access, not ValueLayout access
            _ => self.ffi_value_layout(ffi_type),
        }
    }

    /// Cast prefix needed for invokeExact() return values
    fn ffi_invoke_exact_cast(&self, ffi_type: &FfiType) -> String {
        match ffi_type {
            FfiType::Int8 | FfiType::UInt8 => "(byte) ".to_string(),
            FfiType::Int16 | FfiType::UInt16 => "(short) ".to_string(),
            FfiType::Int32 | FfiType::UInt32 => "(int) ".to_string(),
            FfiType::Int64 | FfiType::UInt64 => "(long) ".to_string(),
            FfiType::Float32 => "(float) ".to_string(),
            FfiType::Float64 => "(double) ".to_string(),
            FfiType::Handle => "(long) ".to_string(),
            FfiType::RustBuffer(_)
            | FfiType::RustCallStatus
            | FfiType::ForeignBytes
            | FfiType::Callback(_)
            | FfiType::Struct(_)
            | FfiType::VoidPointer
            | FfiType::Reference(_)
            | FfiType::MutReference(_) => "(java.lang.foreign.MemorySegment) ".to_string(),
        }
    }

    /// Returns true if the FFI type is a struct that requires a SegmentAllocator
    /// as the first argument to invokeExact() for downcall handles
    fn ffi_type_is_struct(&self, ffi_type: &FfiType) -> bool {
        // RustCallStatus is passed as a pointer, not by value, so it doesn't need SegmentAllocator
        matches!(
            ffi_type,
            FfiType::RustBuffer(_) | FfiType::ForeignBytes | FfiType::Struct(_)
        )
    }

    /// Returns true if this FFI type is an embedded struct inside another struct
    /// (uses slice-based access rather than primitive get/set)
    fn ffi_type_is_embedded_struct(&self, ffi_type: &FfiType) -> bool {
        matches!(
            ffi_type,
            FfiType::RustBuffer(_)
                | FfiType::RustCallStatus
                | FfiType::ForeignBytes
                | FfiType::Struct(_)
        )
    }

    /// Get the struct class name for an FfiType::Struct
    fn ffi_struct_type_name(&self, ffi_type: &FfiType) -> String {
        match ffi_type {
            FfiType::Struct(name) => self.ffi_struct_name(name),
            FfiType::RustBuffer(_) => "RustBuffer".to_string(),
            FfiType::RustCallStatus => "UniffiRustCallStatus".to_string(),
            FfiType::ForeignBytes => "ForeignBytes".to_string(),
            _ => panic!("ffi_struct_type_name called on non-struct type: {ffi_type:?}"),
        }
    }

    /// Get the name of the interface and class name for an object.
    ///
    /// If we support callback interfaces, the interface name is the object name, and the class name is derived from that.
    /// Otherwise, the class name is the object name and the interface name is derived from that.
    ///
    /// This split determines what types `FfiConverter.lower()` inputs.  If we support callback
    /// interfaces, `lower` must lower anything that implements the interface.  If not, then lower
    /// only lowers the concrete class.
    fn object_names(&self, ci: &ComponentInterface, obj: &Object) -> (String, String) {
        let class_name = self.class_name(ci, obj.name());
        if obj.has_callback_interface() {
            let impl_name = format!("{class_name}Impl");
            (class_name, impl_name)
        } else {
            (format!("{class_name}Interface"), class_name)
        }
    }
}

trait AsCodeType {
    fn as_codetype(&self) -> Box<dyn CodeType>;
}

// Workaround for the possibility of upstream additions of AsType breaking compilation
// Downside to this is new types need to be manually added
impl AsCodeType for Type {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        // Map `Type` instances to a `Box<dyn CodeType>` for that type.
        //
        // There is a companion match in `templates/Types.java` which performs a similar function for the
        // template code.
        //
        //   - When adding additional types here, make sure to also add a match arm to the `Types.java` template.
        //   - To keep things manageable, let's try to limit ourselves to these 2 mega-matches
        match self.as_type() {
            Type::UInt8 | Type::Int8 => Box::new(primitives::Int8CodeType),
            Type::UInt16 | Type::Int16 => Box::new(primitives::Int16CodeType),
            Type::UInt32 | Type::Int32 => Box::new(primitives::Int32CodeType),
            Type::UInt64 | Type::Int64 => Box::new(primitives::Int64CodeType),
            Type::Float32 => Box::new(primitives::Float32CodeType),
            Type::Float64 => Box::new(primitives::Float64CodeType),
            Type::Boolean => Box::new(primitives::BooleanCodeType),
            Type::String => Box::new(primitives::StringCodeType),
            Type::Bytes => Box::new(primitives::BytesCodeType),

            Type::Timestamp => Box::new(miscellany::TimestampCodeType),
            Type::Duration => Box::new(miscellany::DurationCodeType),

            Type::Enum { name, .. } => Box::new(enum_::EnumCodeType::new(name.clone())),
            Type::Object { name, imp, .. } => {
                Box::new(object::ObjectCodeType::new(name.clone(), imp))
            }
            Type::Record { name, .. } => Box::new(record::RecordCodeType::new(name.clone())),
            Type::CallbackInterface { name, .. } => Box::new(
                callback_interface::CallbackInterfaceCodeType::new(name.clone()),
            ),
            Type::Optional { inner_type } => {
                Box::new(compounds::OptionalCodeType::new((*inner_type).clone()))
            }
            Type::Sequence { inner_type } => match inner_type.as_ref() {
                Type::Int16 | Type::UInt16 => Box::new(compounds::Int16ArrayCodeType),
                Type::Int32 | Type::UInt32 => Box::new(compounds::Int32ArrayCodeType),
                Type::Int64 | Type::UInt64 => Box::new(compounds::Int64ArrayCodeType),
                Type::Float32 => Box::new(compounds::Float32ArrayCodeType),
                Type::Float64 => Box::new(compounds::Float64ArrayCodeType),
                Type::Boolean => Box::new(compounds::BooleanArrayCodeType),
                // Int8/UInt8 sequences still use SequenceCodeType; the separate Bytes type handles byte[]
                _ => Box::new(compounds::SequenceCodeType::new((*inner_type).clone())),
            },
            Type::Map {
                key_type,
                value_type,
            } => Box::new(compounds::MapCodeType::new(
                (*key_type).clone(),
                (*value_type).clone(),
            )),
            Type::Custom { name, .. } => Box::new(custom::CustomCodeType::new(name.clone())),
        }
    }
}
impl AsCodeType for &'_ Type {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        (*self).as_codetype()
    }
}
impl AsCodeType for &&'_ Type {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        (**self).as_codetype()
    }
}
impl AsCodeType for &'_ Field {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}
impl AsCodeType for &'_ uniffi_bindgen::interface::Enum {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}
impl AsCodeType for &'_ uniffi_bindgen::interface::Object {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}
impl AsCodeType for &'_ Box<uniffi_meta::Type> {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}
impl AsCodeType for &'_ Argument {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}
impl AsCodeType for &'_ uniffi_bindgen::interface::Record {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}
impl AsCodeType for &'_ uniffi_bindgen::interface::CallbackInterface {
    fn as_codetype(&self) -> Box<dyn CodeType> {
        self.as_type().as_codetype()
    }
}

mod filters {
    #![allow(unused_variables)]
    use super::*;
    use uniffi_meta::AsType;

    // Askama 0.14 passes a Values parameter to all filters. We use `_v` to accept but ignore it.

    pub(super) fn ffi_type(
        type_: &impl AsType,
        _v: &dyn askama::Values,
    ) -> Result<FfiType, askama::Error> {
        Ok(type_.as_type().into())
    }

    pub(super) fn type_name(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        config: &Config,
    ) -> Result<String, askama::Error> {
        Ok(as_ct.as_codetype().type_label(ci, config))
    }

    /// Generate a fully qualified type name including the package.
    /// This is needed for enum variant fields to avoid naming collisions
    /// when a variant field type has the same name as the enum itself.
    pub(super) fn qualified_type_name<T>(
        as_type: &T,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        config: &Config,
    ) -> Result<String, askama::Error>
    where
        T: AsCodeType + AsType,
    {
        fully_qualified_type_label(&as_type.as_type(), ci, config)
            .map_err(|e| askama::Error::Custom(e.into()))
    }

    fn fully_qualified_type_label(
        ty: &Type,
        ci: &ComponentInterface,
        config: &Config,
    ) -> anyhow::Result<String> {
        match ty {
            Type::Optional { inner_type } => {
                let inner = fully_qualified_type_label(inner_type, ci, config)?;
                if config.nullness_annotations() {
                    Ok(nullable_type_label(&inner))
                } else {
                    Ok(inner)
                }
            }
            Type::Sequence { inner_type } => match inner_type.as_ref() {
                Type::Int16 | Type::UInt16 => Ok("short[]".to_string()),
                Type::Int32 | Type::UInt32 => Ok("int[]".to_string()),
                Type::Int64 | Type::UInt64 => Ok("long[]".to_string()),
                Type::Float32 => Ok("float[]".to_string()),
                Type::Float64 => Ok("double[]".to_string()),
                Type::Boolean => Ok("boolean[]".to_string()),
                _ => Ok(format!(
                    "java.util.List<{}>",
                    fully_qualified_type_label(inner_type, ci, config)?
                )),
            },
            Type::Map {
                key_type,
                value_type,
            } => Ok(format!(
                "java.util.Map<{}, {}>",
                fully_qualified_type_label(key_type, ci, config)?,
                fully_qualified_type_label(value_type, ci, config)?
            )),
            Type::Enum { .. }
            | Type::Record { .. }
            | Type::Object { .. }
            | Type::CallbackInterface { .. }
            | Type::Custom { .. } => {
                let class_name = ty
                    .name()
                    .map(|nm| JavaCodeOracle.class_name(ci, nm))
                    .ok_or_else(|| anyhow::anyhow!("type {:?} has no name", ty))?;
                let package_name = package_for_type(ty, ci, config)?;
                Ok(format!("{}.{}", package_name, class_name))
            }
            _ => Ok(JavaCodeOracle.find(ty).type_label(ci, config)),
        }
    }

    fn package_for_type(
        ty: &Type,
        ci: &ComponentInterface,
        config: &Config,
    ) -> anyhow::Result<String> {
        if ci.is_external(ty) {
            let module_path = ty
                .module_path()
                .ok_or_else(|| anyhow::anyhow!("external type {:?} missing module path", ty))?;
            let namespace = ci.namespace_for_module_path(module_path)?;
            Ok(config.external_type_package_name(module_path, namespace))
        } else {
            Ok(config.package_name())
        }
    }

    /// Returns the [`ComponentInterface`] that owns `module_path`.
    ///
    /// If `module_path` belongs to the current crate, returns `ci` directly.
    /// Otherwise looks up an external component interface by the full module
    /// path first, then by the crate name (first `::` segment).
    fn component_interface_for_module_path<'a>(
        ci: &'a ComponentInterface,
        module_path: &str,
    ) -> Option<&'a ComponentInterface> {
        let crate_name = module_path.split("::").next().unwrap_or(module_path);
        if crate_name == ci.crate_name() {
            Some(ci)
        } else {
            ci.find_component_interface(module_path)
                .or_else(|| ci.find_component_interface(crate_name))
        }
    }

    pub(super) fn canonical_name(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(as_ct.as_codetype().canonical_name())
    }

    /// Check if a type is external (from another crate)
    pub(super) fn is_external(
        as_type: &impl AsType,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<bool, askama::Error> {
        Ok(ci.is_external(&as_type.as_type()))
    }

    pub(super) fn ffi_converter_instance(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(as_ct.as_codetype().ffi_converter_instance(config, ci))
    }

    pub(super) fn ffi_converter_name(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(as_ct.as_codetype().ffi_converter_name())
    }

    pub(super) fn lower_fn(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(format!(
            "{}.lower",
            as_ct.as_codetype().ffi_converter_instance(config, ci)
        ))
    }

    pub(super) fn allocation_size_fn(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(format!(
            "{}.allocationSize",
            as_ct.as_codetype().ffi_converter_instance(config, ci)
        ))
    }

    pub(super) fn write_fn(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(format!(
            "{}.write",
            as_ct.as_codetype().ffi_converter_instance(config, ci)
        ))
    }

    pub(super) fn lift_fn(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(format!(
            "{}.lift",
            as_ct.as_codetype().ffi_converter_instance(config, ci)
        ))
    }

    pub(super) fn read_fn(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(format!(
            "{}.read",
            as_ct.as_codetype().ffi_converter_instance(config, ci)
        ))
    }

    // Get the idiomatic Java rendering of an integer.
    fn int_literal(t: &Option<Type>, base10: String) -> Result<String, askama::Error> {
        if let Some(t) = t {
            match t {
                // Byte and Short need explicit casts in Java
                Type::Int8 | Type::UInt8 => Ok(format!("(byte){}", base10)),
                Type::Int16 | Type::UInt16 => Ok(format!("(short){}", base10)),
                Type::Int32 | Type::UInt32 => Ok(base10),
                Type::Int64 | Type::UInt64 => Ok(base10),
                _ => Err(to_askama_error("Only ints are supported.")),
            }
        } else {
            Err(to_askama_error("Enum hasn't defined a repr"))
        }
    }

    // Get the idiomatic Java rendering of an individual enum variant's discriminant
    pub fn variant_discr_literal(
        e: &Enum,
        _v: &dyn askama::Values,
        index: &usize,
    ) -> Result<String, askama::Error> {
        let literal = e.variant_discr(*index).expect("invalid index");
        match literal {
            // Java doesn't convert between signed and unsigned by default
            // so we'll need to make sure we define the type as appropriately
            Literal::UInt(v, _, _) => int_literal(e.variant_discr_type(), v.to_string()),
            Literal::Int(v, _, _) => int_literal(e.variant_discr_type(), v.to_string()),
            _ => Err(to_askama_error("Only ints are supported.")),
        }
    }

    /// FFI type name (primitive for scalars, MemorySegment for everything else)
    pub fn ffi_type_name(
        type_: &FfiType,
        _v: &dyn askama::Values,
        config: &Config,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_type_label(type_, config, ci))
    }

    /// Returns the primitive call suffix (e.g. "Long", "Int") for primitive-specialized
    /// uniffiRustCall variants. Returns empty string for types where the high-level Java
    /// primitive doesn't match the FFI primitive (e.g. Boolean→byte) or non-primitive types.
    pub fn primitive_call_suffix(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        // Only bypass FfiConverter when the Java primitive matches the FFI primitive.
        // Boolean is excluded because it's `boolean` in Java but `byte` at the FFI level.
        Ok(
            match as_ct.as_codetype().type_label_primitive().as_deref() {
                Some("byte") => "Byte",
                Some("short") => "Short",
                Some("int") => "Int",
                Some("long") => "Long",
                Some("float") => "Float",
                Some("double") => "Double",
                _ => "",
            }
            .to_string(),
        )
    }

    /// Returns true if the argument's FFI type is a primitive where the Java type matches
    /// the FFI type directly (no conversion needed). Used to skip lower_fn for primitive args.
    /// Excludes boolean (Java `boolean` vs FFI `byte`).
    pub fn has_primitive_ffi_type(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
    ) -> Result<bool, askama::Error> {
        Ok(matches!(
            as_ct.as_codetype().type_label_primitive().as_deref(),
            Some("byte" | "short" | "int" | "long" | "float" | "double")
        ))
    }

    /// Maps FfiType to ValueLayout constant for FunctionDescriptor
    pub fn ffi_value_layout(
        type_: &FfiType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_value_layout(type_))
    }

    /// Generate the full structLayout body for an FfiStruct, with computed padding
    pub fn ffi_struct_layout_body(
        ffi_struct: &uniffi_bindgen::interface::FfiStruct,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        let mut parts = Vec::new();
        let mut offset: usize = 0;
        for field in ffi_struct.fields() {
            let field_type = field.type_();
            let alignment = JavaCodeOracle.ffi_type_alignment(&field_type);
            // Insert padding if needed for alignment
            let padding = if !offset.is_multiple_of(alignment) {
                alignment - (offset % alignment)
            } else {
                0
            };
            if padding > 0 {
                parts.push(format!(
                    "java.lang.foreign.MemoryLayout.paddingLayout({})",
                    padding
                ));
                offset += padding;
            }
            let layout = JavaCodeOracle.ffi_struct_field_layout(&field_type);
            let field_name = JavaCodeOracle.var_name_raw(field.name());
            parts.push(format!("{}.withName(\"{}\")", layout, field_name));
            // Advance offset
            let size = JavaCodeOracle.ffi_type_size(&field_type);
            if size > 0 {
                offset += size;
            } else {
                // Unknown size (e.g., user-defined FfiStruct) — can't compute further padding
                // but alignment was already handled
                offset = 0; // reset; further padding may be wrong but this is rare
            }
        }
        Ok(parts.join(",\n        "))
    }

    /// Maps FfiType to UNALIGNED ValueLayout for struct field access
    pub fn ffi_value_layout_unaligned(
        type_: &FfiType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_value_layout_unaligned(type_))
    }

    /// Cast prefix for invokeExact() return values
    pub fn ffi_invoke_exact_cast(
        type_: &FfiType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_invoke_exact_cast(type_))
    }

    /// Returns true if the FFI return type is a struct needing SegmentAllocator
    pub fn ffi_type_is_struct(
        type_: &FfiType,
        _v: &dyn askama::Values,
    ) -> Result<bool, askama::Error> {
        Ok(JavaCodeOracle.ffi_type_is_struct(type_))
    }

    /// Returns true if this is an embedded struct (slice-based access in struct fields)
    pub fn ffi_type_is_embedded_struct(
        type_: &FfiType,
        _v: &dyn askama::Values,
    ) -> Result<bool, askama::Error> {
        Ok(JavaCodeOracle.ffi_type_is_embedded_struct(type_))
    }

    /// Get the struct class name for an FFI struct type
    pub fn ffi_struct_type_name(
        type_: &FfiType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_struct_type_name(type_))
    }

    /// FFI type name using boxed types for generic contexts (accepts high-level Type)
    pub fn ffi_type_name_boxed(
        type_: &impl AsType,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        let ffi_type: FfiType = type_.as_type().into();
        Ok(JavaCodeOracle.ffi_type_label_boxed(&ffi_type))
    }

    /// Get the interface name for a trait implementation (for external trait interfaces).
    pub fn trait_interface_name(
        trait_ty: &Type,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        let Some(module_path) = trait_ty.module_path() else {
            return Err(to_askama_error(&format!(
                "Invalid trait_type: {trait_ty:?}"
            )));
        };
        let Some(ci_look) = component_interface_for_module_path(ci, module_path) else {
            return Err(to_askama_error(&format!(
                "no interface with module_path: {}",
                module_path
            )));
        };

        let (obj_name, has_callback_interface) = match trait_ty {
            Type::Object { name, .. } => {
                let Some(obj) = ci_look.get_object_definition(name) else {
                    return Err(to_askama_error(&format!(
                        "trait interface not found: {}",
                        name
                    )));
                };
                (name, obj.has_callback_interface())
            }
            Type::CallbackInterface { name, .. } => (name, true),
            _ => {
                return Err(to_askama_error(&format!(
                    "Invalid trait_type: {trait_ty:?}"
                )));
            }
        };

        let class_name = JavaCodeOracle.class_name(ci_look, obj_name);
        if has_callback_interface {
            Ok(class_name)
        } else {
            Ok(format!("{}Interface", class_name))
        }
    }

    /// Get the idiomatic Java rendering of a class name from a string.
    pub fn class_name<S: AsRef<str>>(
        nm: S,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.class_name(ci, nm.as_ref()))
    }

    /// Get the idiomatic Java rendering of a class name from a Type.
    pub fn class_name_from_type(
        as_type: &impl AsType,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        let type_ = as_type.as_type();
        let name = match &type_ {
            Type::Enum { name, .. } => name,
            Type::Object { name, .. } => name,
            Type::Record { name, .. } => name,
            Type::Custom { name, .. } => name,
            Type::CallbackInterface { name, .. } => name,
            _ => {
                return Err(to_askama_error(&format!(
                    "class_name_from_type: unsupported type {:?}",
                    type_
                )));
            }
        };
        Ok(JavaCodeOracle.class_name(ci, name))
    }

    /// Get the idiomatic Java rendering of a function name.
    pub fn fn_name<S: AsRef<str>>(nm: S, _v: &dyn askama::Values) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.fn_name(nm.as_ref()))
    }

    /// Get the idiomatic Java rendering of a variable name.
    pub fn var_name<S: AsRef<str>>(
        nm: S,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.var_name(nm.as_ref()))
    }

    /// Get the idiomatic Java rendering of a variable name, without altering reserved words.
    pub fn var_name_raw<S: AsRef<str>>(
        nm: S,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.var_name_raw(nm.as_ref()))
    }

    /// Get the idiomatic Java setter method name.
    pub fn setter<S: AsRef<str>>(nm: S, _v: &dyn askama::Values) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.setter(nm.as_ref()))
    }

    /// Get a String representing the name used for an individual enum variant.
    pub fn variant_name(
        variant: &Variant,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.enum_variant_name(variant.name()))
    }

    pub fn error_variant_name(
        variant: &Variant,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        let name = variant.name().to_string().to_upper_camel_case();
        Ok(JavaCodeOracle.convert_error_suffix(&name))
    }

    /// Get the idiomatic Java rendering of an FFI callback function name
    pub fn ffi_callback_name<S: AsRef<str>>(
        nm: S,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_callback_name(nm.as_ref()))
    }

    /// Get the idiomatic Java rendering of an FFI struct name
    pub fn ffi_struct_name<S: AsRef<str>>(
        nm: S,
        _v: &dyn askama::Values,
    ) -> Result<String, askama::Error> {
        Ok(JavaCodeOracle.ffi_struct_name(nm.as_ref()))
    }

    pub fn object_names(
        obj: &Object,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<(String, String), askama::Error> {
        Ok(JavaCodeOracle.object_names(ci, obj))
    }

    pub fn async_inner_return_type(
        callable: impl Callable,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        config: &Config,
    ) -> Result<String, askama::Error> {
        callable
            .return_type()
            .map_or(Ok("java.lang.Void".to_string()), |t| {
                type_name(t, _v, ci, config)
            })
    }

    pub fn async_return_type(
        callable: impl Callable,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        config: &Config,
    ) -> Result<String, askama::Error> {
        let is_async = callable.is_async();
        let inner_type = async_inner_return_type(callable, _v, ci, config)?;
        if is_async {
            Ok(format!(
                "java.util.concurrent.CompletableFuture<{inner_type}>"
            ))
        } else {
            Ok(inner_type)
        }
    }

    pub fn async_poll(
        callable: impl Callable,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        let ffi_func = callable.ffi_rust_future_poll(ci);
        Ok(format!(
            "(future, callback, continuationHandle) -> UniffiLib.{ffi_func}(future, callback, continuationHandle)"
        ))
    }

    pub fn async_complete(
        callable: impl Callable,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        _config: &Config,
    ) -> Result<String, askama::Error> {
        let ffi_func = callable.ffi_rust_future_complete(ci);
        // The complete function returns a RustBuffer for types that use RustBuffer FFI,
        // which is a struct and needs a SegmentAllocator.
        let needs_allocator = callable.return_type().is_some_and(|t| {
            let ffi_type: FfiType = t.into();
            JavaCodeOracle.ffi_type_is_struct(&ffi_type)
        });
        let allocator_arg = if needs_allocator { "_allocator, " } else { "" };
        let call = format!("UniffiLib.{ffi_func}({allocator_arg}future, continuation)");
        Ok(format!("(_allocator, future, continuation) -> {call}"))
    }

    pub fn async_free(
        callable: impl Callable,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
    ) -> Result<String, askama::Error> {
        let ffi_func = callable.ffi_rust_future_free(ci);
        Ok(format!("(future) -> UniffiLib.{ffi_func}(future)"))
    }

    /// Remove the "`" chars we put around function/variable names
    ///
    /// These are used to avoid name clashes with java identifiers, but sometimes you want to
    /// render the name unquoted.  One example is the message property for errors where we want to
    /// display the name for the user.
    pub fn unquote<S: AsRef<str>>(nm: S, _v: &dyn askama::Values) -> Result<String, askama::Error> {
        Ok(nm.as_ref().trim_matches('`').to_string())
    }

    /// Get the idiomatic Java rendering of docstring
    pub fn docstring<S: AsRef<str>>(
        docstring: S,
        _v: &dyn askama::Values,
        spaces: &i32,
    ) -> Result<String, askama::Error> {
        let middle = textwrap::indent(&textwrap::dedent(docstring.as_ref()), " * ");
        let wrapped = format!("/**\n{middle}\n */");

        let spaces = usize::try_from(*spaces).unwrap_or_default();
        Ok(textwrap::indent(&wrapped, &" ".repeat(spaces)))
    }

    /// Returns the type name suitable for use in field declarations, method parameters, and return types.
    /// For non-optional primitives, returns the primitive type (int, long, boolean, etc.).
    /// For optional types and all other types, returns the boxed/object type.
    pub fn type_name_for_field(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        config: &Config,
    ) -> Result<String, askama::Error> {
        // Check if the codetype has a primitive label available
        let codetype = as_ct.as_codetype();
        if let Some(primitive) = codetype.type_label_primitive() {
            return Ok(primitive);
        }
        // Otherwise use the standard boxed type label
        Ok(codetype.type_label(ci, config))
    }

    /// Always returns the boxed type name, for use in generic contexts like CompletableFuture<T>.
    /// This is the same as type_name but with a clearer name for template readability.
    pub fn boxed_type_name(
        as_ct: &impl AsCodeType,
        _v: &dyn askama::Values,
        ci: &ComponentInterface,
        config: &Config,
    ) -> Result<String, askama::Error> {
        Ok(as_ct.as_codetype().type_label(ci, config))
    }

    /// Generates an equality expression for comparing two values of a field's type.
    /// For primitives: returns "left == right"
    /// For objects: returns "java.util.Objects.equals(left, right)"
    pub fn equals_expr<T: AsCodeType, L: std::fmt::Display, R: std::fmt::Display>(
        field: &T,
        _v: &dyn askama::Values,
        left: L,
        right: R,
    ) -> Result<String, askama::Error> {
        // Check if this type has a primitive label (meaning it's a primitive)
        if field.as_codetype().type_label_primitive().is_some() {
            Ok(format!("{} == {}", left, right))
        } else {
            Ok(format!("java.util.Objects.equals({}, {})", left, right))
        }
    }

    /// Generates a hash code expression for a field value.
    /// For primitives: returns "Type.hashCode(value)" (e.g., "java.lang.Integer.hashCode(value)")
    /// For objects: returns "java.util.Objects.hashCode(value)"
    pub fn hash_code_expr<T: AsCodeType + AsType, V: std::fmt::Display>(
        field: &T,
        _v: &dyn askama::Values,
        value: V,
    ) -> Result<String, askama::Error> {
        match field.as_type() {
            Type::Boolean => Ok(format!("java.lang.Boolean.hashCode({})", value)),
            Type::Int8 | Type::UInt8 => Ok(format!("java.lang.Byte.hashCode({})", value)),
            Type::Int16 | Type::UInt16 => Ok(format!("java.lang.Short.hashCode({})", value)),
            Type::Int32 | Type::UInt32 => Ok(format!("java.lang.Integer.hashCode({})", value)),
            Type::Int64 | Type::UInt64 => Ok(format!("java.lang.Long.hashCode({})", value)),
            Type::Float32 => Ok(format!("java.lang.Float.hashCode({})", value)),
            Type::Float64 => Ok(format!("java.lang.Double.hashCode({})", value)),
            _ => Ok(format!("java.util.Objects.hashCode({})", value)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use uniffi_bindgen::interface::ComponentInterface;
    use uniffi_meta::{
        CallbackInterfaceMetadata, EnumMetadata, EnumShape, FieldMetadata, FnMetadata,
        FnParamMetadata, Metadata, MetadataGroup, MethodMetadata, NamespaceMetadata, ObjectImpl,
        ObjectMetadata, ObjectTraitImplMetadata, RecordMetadata, TraitMethodMetadata, Type,
        VariantMetadata,
    };

    #[test]
    fn preserves_error_type_named_error() {
        // create a metadata group with an error enum named only "Error" and a
        // function that always returns that error.
        let mut group = MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "test".to_string(),
                name: "test".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        };
        group.add_item(Metadata::Enum(EnumMetadata {
            module_path: "test".to_string(),
            name: "Error".to_string(),
            shape: EnumShape::Error { flat: true },
            remote: false,
            variants: vec![VariantMetadata {
                name: "Oops".to_string(),
                discr: None,
                fields: vec![],
                docstring: None,
            }],
            discr_type: None,
            non_exhaustive: false,
            docstring: None,
        }));
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "always_fails".to_string(),
            is_async: false,
            inputs: vec![],
            return_type: None,
            throws: Some(Type::Enum {
                module_path: "test".to_string(),
                name: "Error".to_string(),
            }),
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        // show that the generated bindings preserve the name
        // Note: we use java.lang.Exception to avoid collisions with user types
        let relevant_lines = bindings
            .lines()
            .filter(|line| {
                line.contains("class Error extends java.lang.Exception")
                    || line.contains("class Exception extends java.lang.Exception")
                    || line.contains("throws Error")
                    || line.contains("throws Exception")
            })
            .collect::<Vec<_>>()
            .join("\n");

        assert!(
            bindings.contains("public class Error extends java.lang.Exception"),
            "expected generated bindings to preserve the `Error` type name:\n{relevant_lines}"
        );
        assert!(
            bindings.contains("public static void alwaysFails() throws Error"),
            "expected generated bindings to preserve `throws Error`:\n{relevant_lines}"
        );
    }

    #[test]
    fn converts_error_suffix_but_not_bare_error() {
        assert_eq!(
            JavaCodeOracle.convert_error_suffix("ExampleError"),
            "ExampleException"
        );
        assert_eq!(JavaCodeOracle.convert_error_suffix("Error"), "Error");
    }

    fn create_primitive_array_test_group() -> MetadataGroup {
        let mut group = MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "test".to_string(),
                name: "test".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        };

        // Add functions for each primitive array type
        let primitive_types = [
            ("process_floats", Type::Float32),
            ("process_doubles", Type::Float64),
            ("process_shorts", Type::Int16),
            ("process_ints", Type::Int32),
            ("process_longs", Type::Int64),
            ("process_bools", Type::Boolean),
        ];

        for (name, inner_type) in primitive_types {
            group.add_item(Metadata::Func(FnMetadata {
                module_path: "test".to_string(),
                name: name.to_string(),
                is_async: false,
                inputs: vec![FnParamMetadata {
                    name: "data".to_string(),
                    ty: Type::Sequence {
                        inner_type: Box::new(inner_type.clone()),
                    },
                    by_ref: false,
                    optional: false,
                    default: None,
                }],
                return_type: Some(Type::Sequence {
                    inner_type: Box::new(inner_type),
                }),
                throws: None,
                checksum: None,
                docstring: None,
            }));
        }

        group
    }

    #[test]
    fn generates_float32_primitive_array() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("float[]"),
            "expected float[] in generated bindings"
        );
        assert!(
            bindings.contains("FfiConverterFloat32Array"),
            "expected FfiConverterFloat32Array in generated bindings"
        );
        assert!(
            bindings.contains("public static float[] processFloats(float[] data)"),
            "expected processFloats method with float[] signature"
        );
    }

    #[test]
    fn generates_float64_primitive_array() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("double[]"),
            "expected double[] in generated bindings"
        );
        assert!(
            bindings.contains("FfiConverterFloat64Array"),
            "expected FfiConverterFloat64Array in generated bindings"
        );
        assert!(
            bindings.contains("public static double[] processDoubles(double[] data)"),
            "expected processDoubles method with double[] signature"
        );
    }

    #[test]
    fn generates_int32_primitive_array() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("int[]"),
            "expected int[] in generated bindings"
        );
        assert!(
            bindings.contains("FfiConverterInt32Array"),
            "expected FfiConverterInt32Array in generated bindings"
        );
        assert!(
            bindings.contains("public static int[] processInts(int[] data)"),
            "expected processInts method with int[] signature"
        );
    }

    #[test]
    fn generates_int64_primitive_array() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("long[]"),
            "expected long[] in generated bindings"
        );
        assert!(
            bindings.contains("FfiConverterInt64Array"),
            "expected FfiConverterInt64Array in generated bindings"
        );
        assert!(
            bindings.contains("public static long[] processLongs(long[] data)"),
            "expected processLongs method with long[] signature"
        );
    }

    #[test]
    fn generates_int16_primitive_array() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("short[]"),
            "expected short[] in generated bindings"
        );
        assert!(
            bindings.contains("FfiConverterInt16Array"),
            "expected FfiConverterInt16Array in generated bindings"
        );
        assert!(
            bindings.contains("public static short[] processShorts(short[] data)"),
            "expected processShorts method with short[] signature"
        );
    }

    #[test]
    fn generates_boolean_primitive_array() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("boolean[]"),
            "expected boolean[] in generated bindings"
        );
        assert!(
            bindings.contains("FfiConverterBooleanArray"),
            "expected FfiConverterBooleanArray in generated bindings"
        );
        assert!(
            bindings.contains("public static boolean[] processBools(boolean[] data)"),
            "expected processBools method with boolean[] signature"
        );
    }

    #[test]
    fn does_not_generate_list_for_primitive_sequences() {
        let group = create_primitive_array_test_group();
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        // Verify that we don't generate boxed List types for primitives
        assert!(
            !bindings.contains("List<java.lang.Float>"),
            "should not contain List<java.lang.Float>"
        );
        assert!(
            !bindings.contains("List<java.lang.Double>"),
            "should not contain List<java.lang.Double>"
        );
        assert!(
            !bindings.contains("List<java.lang.Integer>"),
            "should not contain List<java.lang.Integer>"
        );
        assert!(
            !bindings.contains("List<java.lang.Long>"),
            "should not contain List<java.lang.Long>"
        );
        assert!(
            !bindings.contains("List<java.lang.Short>"),
            "should not contain List<java.lang.Short>"
        );
        assert!(
            !bindings.contains("List<java.lang.Boolean>"),
            "should not contain List<java.lang.Boolean>"
        );
    }

    #[test]
    fn android_replaces_ffm_package() {
        let mut group = MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "test".to_string(),
                name: "test".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        };
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "noop".to_string(),
            is_async: false,
            inputs: vec![],
            return_type: None,
            throws: None,
            checksum: None,
            docstring: None,
        }));

        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();

        let android_config: Config = toml::from_str("android = true").unwrap();

        let bindings = generate_bindings(&android_config, &ci).unwrap();

        assert!(
            !bindings.contains("java.lang.foreign."),
            "android bindings should not contain java.lang.foreign"
        );
        assert!(
            bindings.contains("com.v7878.foreign."),
            "android bindings should contain com.v7878.foreign"
        );
        // MethodHandle/MethodHandles/MethodType should NOT be replaced
        assert!(
            bindings.contains("java.lang.invoke.MethodHandle"),
            "android bindings should preserve java.lang.invoke.MethodHandle"
        );
        // Standard java.lang types should NOT be replaced
        assert!(
            bindings.contains("java.lang.Exception"),
            "android bindings should preserve java.lang.Exception"
        );
    }

    #[test]
    fn trait_impl_with_submodule_path() {
        // Regression test: when a crate has multiple modules, module_path is
        // "crate_name::submodule". component_interface_for_module_path() handles
        // this by checking the local CI's crate_name first before falling back
        // to find_component_interface() for external crates.
        let submodule_path = "mycrate::inner";
        let mut group = MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "mycrate".to_string(),
                name: "mycrate".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        };

        // A trait object defined in a submodule
        group.add_item(Metadata::Object(ObjectMetadata {
            module_path: submodule_path.to_string(),
            name: "MyTrait".to_string(),
            remote: false,
            imp: ObjectImpl::CallbackTrait,
            docstring: None,
        }));

        // A concrete object that implements the trait, also in the submodule
        group.add_item(Metadata::Object(ObjectMetadata {
            module_path: submodule_path.to_string(),
            name: "MyObj".to_string(),
            remote: false,
            imp: ObjectImpl::Struct,
            docstring: None,
        }));

        group.add_item(Metadata::ObjectTraitImpl(ObjectTraitImplMetadata {
            ty: Type::Object {
                module_path: submodule_path.to_string(),
                name: "MyObj".to_string(),
                imp: ObjectImpl::Struct,
            },
            trait_ty: Type::Object {
                module_path: submodule_path.to_string(),
                name: "MyTrait".to_string(),
                imp: ObjectImpl::CallbackTrait,
            },
        }));

        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        // The object should implement the trait interface
        assert!(
            bindings.contains("implements AutoCloseable, MyObjInterface, MyTrait"),
            "MyObj should implement MyTrait via trait_interface_name even with submodule path:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("MyObj") || l.contains("MyTrait"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn local_trait_impls_accept_submodule_module_paths() {
        let mut group = MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "test".to_string(),
                name: "test".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        };
        group.add_item(Metadata::Object(ObjectMetadata {
            module_path: "test".to_string(),
            name: "DefaultMetricsRecorder".to_string(),
            remote: false,
            imp: ObjectImpl::Struct,
            docstring: None,
        }));
        group.add_item(Metadata::CallbackInterface(CallbackInterfaceMetadata {
            module_path: "test::metrics".to_string(),
            name: "MetricsRecorder".to_string(),
            docstring: None,
        }));
        group.add_item(Metadata::ObjectTraitImpl(ObjectTraitImplMetadata {
            ty: Type::Object {
                module_path: "test".to_string(),
                name: "DefaultMetricsRecorder".to_string(),
                imp: ObjectImpl::Struct,
            },
            trait_ty: Type::CallbackInterface {
                module_path: "test::metrics".to_string(),
                name: "MetricsRecorder".to_string(),
            },
        }));

        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();

        let interface_name = super::filters::trait_interface_name(
            &Type::CallbackInterface {
                module_path: "test::metrics".to_string(),
                name: "MetricsRecorder".to_string(),
            },
            &(),
            &ci,
        )
        .unwrap();
        assert_eq!(interface_name, "MetricsRecorder");

        let bindings = generate_bindings(&Config::default(), &ci).unwrap();
        assert!(
            bindings.contains("DefaultMetricsRecorderInterface, MetricsRecorder"),
            "expected local callback trait impls with submodule paths to render successfully:\n{}",
            bindings
                .lines()
                .filter(|line| line.contains("class DefaultMetricsRecorder"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    fn nullness_config() -> Config {
        toml::from_str("nullness_annotations = true").unwrap()
    }

    #[test]
    fn nullable_type_label_fully_qualified() {
        assert_eq!(
            super::nullable_type_label("java.lang.String"),
            "java.lang.@org.jspecify.annotations.Nullable String"
        );
    }

    #[test]
    fn nullable_type_label_unqualified() {
        assert_eq!(
            super::nullable_type_label("String"),
            "@org.jspecify.annotations.Nullable String"
        );
    }

    #[test]
    fn nullable_type_label_nested_class() {
        assert_eq!(
            super::nullable_type_label("UniffiCleaner.Cleanable"),
            "UniffiCleaner.@org.jspecify.annotations.Nullable Cleanable"
        );
    }

    #[test]
    fn nullable_type_label_generic_type() {
        assert_eq!(
            super::nullable_type_label("java.util.Map<java.lang.String, java.lang.Integer>"),
            "java.util.@org.jspecify.annotations.Nullable Map<java.lang.String, java.lang.Integer>"
        );
    }

    #[test]
    fn nullable_type_label_generic_with_nullable_inner() {
        // Simulates Optional<Map<String, Optional<Int32>>>: the inner Optional
        // is already annotated, then the outer Optional annotates the Map itself.
        assert_eq!(
            super::nullable_type_label(
                "java.util.Map<java.lang.String, java.lang.@org.jspecify.annotations.Nullable Integer>"
            ),
            "java.util.@org.jspecify.annotations.Nullable Map<java.lang.String, java.lang.@org.jspecify.annotations.Nullable Integer>"
        );
    }

    #[test]
    fn nullable_type_label_generic_unqualified() {
        assert_eq!(
            super::nullable_type_label("CompletableFuture<java.lang.String>"),
            "@org.jspecify.annotations.Nullable CompletableFuture<java.lang.String>"
        );
    }

    fn test_group() -> MetadataGroup {
        MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "test".to_string(),
                name: "test".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        }
    }

    #[test]
    fn nullness_annotations_disabled_by_default() {
        let mut group = test_group();
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "maybe_string".to_string(),
            is_async: false,
            inputs: vec![FnParamMetadata {
                name: "input".to_string(),
                ty: Type::String,
                by_ref: false,
                optional: false,
                default: None,
            }],
            return_type: Some(Type::Optional {
                inner_type: Box::new(Type::String),
            }),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();
        assert!(
            !bindings.contains("@org.jspecify.annotations.NullMarked"),
            "should not contain @NullMarked when disabled"
        );
        assert!(
            !bindings.contains("@org.jspecify.annotations.Nullable"),
            "should not contain @Nullable when disabled"
        );
    }

    #[test]
    fn nullness_function_with_optional_param_and_return() {
        let mut group = test_group();
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "foo".to_string(),
            is_async: false,
            inputs: vec![
                FnParamMetadata {
                    name: "required".to_string(),
                    ty: Type::String,
                    by_ref: false,
                    optional: false,
                    default: None,
                },
                FnParamMetadata {
                    name: "optional".to_string(),
                    ty: Type::Optional {
                        inner_type: Box::new(Type::String),
                    },
                    by_ref: false,
                    optional: false,
                    default: None,
                },
            ],
            return_type: Some(Type::Optional {
                inner_type: Box::new(Type::Int32),
            }),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings.contains("java.lang.@org.jspecify.annotations.Nullable Integer"),
            "return type should be @Nullable Integer"
        );
        assert!(
            bindings.contains("java.lang.@org.jspecify.annotations.Nullable String optional"),
            "optional param should be @Nullable String"
        );
        // The required param should NOT have @Nullable
        assert!(
            bindings.contains(
                "java.lang.String required, java.lang.@org.jspecify.annotations.Nullable"
            ),
            "required param should not have @Nullable"
        );
    }

    #[test]
    fn nullness_record_with_optional_field() {
        let mut group = test_group();
        group.add_item(Metadata::Record(RecordMetadata {
            module_path: "test".to_string(),
            name: "Person".to_string(),
            remote: false,
            fields: vec![
                FieldMetadata {
                    name: "name".to_string(),
                    ty: Type::String,
                    default: None,
                    docstring: None,
                },
                FieldMetadata {
                    name: "nickname".to_string(),
                    ty: Type::Optional {
                        inner_type: Box::new(Type::String),
                    },
                    default: None,
                    docstring: None,
                },
            ],
            docstring: None,
        }));
        // Need a function to make the record reachable
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "get_person".to_string(),
            is_async: false,
            inputs: vec![],
            return_type: Some(Type::Record {
                module_path: "test".to_string(),
                name: "Person".to_string(),
            }),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings
                .contains("private java.lang.@org.jspecify.annotations.Nullable String nickname"),
            "optional field should have @Nullable:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("nickname"))
                .collect::<Vec<_>>()
                .join("\n")
        );
        // Non-optional field should NOT have @Nullable
        assert!(
            !bindings.contains("java.lang.@org.jspecify.annotations.Nullable String name"),
            "non-optional field should not have @Nullable"
        );
    }

    #[test]
    fn nullness_immutable_record_with_optional_field() {
        let mut group = test_group();
        group.add_item(Metadata::Record(RecordMetadata {
            module_path: "test".to_string(),
            name: "Person".to_string(),
            remote: false,
            fields: vec![
                FieldMetadata {
                    name: "name".to_string(),
                    ty: Type::String,
                    default: None,
                    docstring: None,
                },
                FieldMetadata {
                    name: "nickname".to_string(),
                    ty: Type::Optional {
                        inner_type: Box::new(Type::String),
                    },
                    default: None,
                    docstring: None,
                },
            ],
            docstring: None,
        }));
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "get_person".to_string(),
            is_async: false,
            inputs: vec![],
            return_type: Some(Type::Record {
                module_path: "test".to_string(),
                name: "Person".to_string(),
            }),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let config: Config =
            toml::from_str("nullness_annotations = true\ngenerate_immutable_records = true")
                .unwrap();
        let bindings = generate_bindings(&config, &ci).unwrap();
        assert!(
            bindings.contains("java.lang.@org.jspecify.annotations.Nullable String nickname"),
            "immutable record should have @Nullable on optional component:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("nickname"))
                .collect::<Vec<_>>()
                .join("\n")
        );
        assert!(
            !bindings.contains("java.lang.@org.jspecify.annotations.Nullable String name"),
            "non-optional component should not have @Nullable"
        );
    }

    #[test]
    fn nullness_object_method_with_optional_param() {
        let mut group = test_group();
        group.add_item(Metadata::Object(ObjectMetadata {
            module_path: "test".to_string(),
            name: "MyObj".to_string(),
            remote: false,
            imp: ObjectImpl::Struct,
            docstring: None,
        }));
        group.add_item(Metadata::Method(MethodMetadata {
            module_path: "test".to_string(),
            self_name: "MyObj".to_string(),
            name: "do_thing".to_string(),
            is_async: false,
            inputs: vec![FnParamMetadata {
                name: "input".to_string(),
                ty: Type::Optional {
                    inner_type: Box::new(Type::String),
                },
                by_ref: false,
                optional: false,
                default: None,
            }],
            return_type: None,
            throws: None,
            takes_self_by_arc: false,
            checksum: None,
            docstring: None,
        }));
        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings.contains("java.lang.@org.jspecify.annotations.Nullable String input"),
            "object method param should have @Nullable:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("doThing"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn nullness_object_cleanable_field_annotated() {
        let mut group = test_group();
        group.add_item(Metadata::Object(ObjectMetadata {
            module_path: "test".to_string(),
            name: "MyObj".to_string(),
            remote: false,
            imp: ObjectImpl::Struct,
            docstring: None,
        }));
        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings
                .contains("UniffiCleaner.@org.jspecify.annotations.Nullable Cleanable cleanable"),
            "cleanable field should have @Nullable when annotations enabled:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("cleanable"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn nullness_object_cleanable_field_not_annotated_by_default() {
        let mut group = test_group();
        group.add_item(Metadata::Object(ObjectMetadata {
            module_path: "test".to_string(),
            name: "MyObj".to_string(),
            remote: false,
            imp: ObjectImpl::Struct,
            docstring: None,
        }));
        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();
        let bindings = generate_bindings(&Config::default(), &ci).unwrap();
        assert!(
            bindings.contains("UniffiCleaner.Cleanable cleanable"),
            "cleanable field should not have @Nullable by default"
        );
        assert!(
            !bindings.contains("UniffiCleaner.@org.jspecify.annotations.Nullable Cleanable"),
            "cleanable field should not have @Nullable when annotations disabled"
        );
    }

    #[test]
    fn nullness_enum_variant_with_optional_field() {
        let mut group = test_group();
        group.add_item(Metadata::Enum(EnumMetadata {
            module_path: "test".to_string(),
            name: "MyEnum".to_string(),
            shape: EnumShape::Enum,
            remote: false,
            variants: vec![VariantMetadata {
                name: "WithOptional".to_string(),
                discr: None,
                fields: vec![FieldMetadata {
                    name: "value".to_string(),
                    ty: Type::Optional {
                        inner_type: Box::new(Type::String),
                    },
                    default: None,
                    docstring: None,
                }],
                docstring: None,
            }],
            discr_type: None,
            non_exhaustive: false,
            docstring: None,
        }));
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "get_enum".to_string(),
            is_async: false,
            inputs: vec![],
            return_type: Some(Type::Enum {
                module_path: "test".to_string(),
                name: "MyEnum".to_string(),
            }),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings.contains("java.lang.@org.jspecify.annotations.Nullable String value"),
            "enum variant optional field should have @Nullable on the field declaration:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("value"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn nullness_error_variant_with_optional_field() {
        let mut group = test_group();
        group.add_item(Metadata::Enum(EnumMetadata {
            module_path: "test".to_string(),
            name: "MyError".to_string(),
            shape: EnumShape::Error { flat: false },
            remote: false,
            variants: vec![VariantMetadata {
                name: "BadInput".to_string(),
                discr: None,
                fields: vec![FieldMetadata {
                    name: "detail".to_string(),
                    ty: Type::Optional {
                        inner_type: Box::new(Type::String),
                    },
                    default: None,
                    docstring: None,
                }],
                docstring: None,
            }],
            discr_type: None,
            non_exhaustive: false,
            docstring: None,
        }));
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "do_stuff".to_string(),
            is_async: false,
            inputs: vec![],
            return_type: None,
            throws: Some(Type::Enum {
                module_path: "test".to_string(),
                name: "MyError".to_string(),
            }),
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings.contains("java.lang.@org.jspecify.annotations.Nullable String detail"),
            "error variant optional field should have @Nullable on the field declaration:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("detail"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn nullness_async_function_with_optional_return() {
        let mut group = test_group();
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "fetch".to_string(),
            is_async: true,
            inputs: vec![],
            return_type: Some(Type::Optional {
                inner_type: Box::new(Type::String),
            }),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings
                .contains("CompletableFuture<java.lang.@org.jspecify.annotations.Nullable String>"),
            "async optional return should be CompletableFuture<@Nullable String>:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("fetch"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn nullness_non_optional_types_never_nullable() {
        let mut group = test_group();
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "identity".to_string(),
            is_async: false,
            inputs: vec![
                FnParamMetadata {
                    name: "s".to_string(),
                    ty: Type::String,
                    by_ref: false,
                    optional: false,
                    default: None,
                },
                FnParamMetadata {
                    name: "i".to_string(),
                    ty: Type::Int32,
                    by_ref: false,
                    optional: false,
                    default: None,
                },
                FnParamMetadata {
                    name: "b".to_string(),
                    ty: Type::Boolean,
                    by_ref: false,
                    optional: false,
                    default: None,
                },
            ],
            return_type: Some(Type::String),
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            !bindings.contains("@org.jspecify.annotations.Nullable"),
            "non-optional types should never have @Nullable"
        );
    }

    #[test]
    fn nullness_nested_optional_in_map_value() {
        let mut group = test_group();
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "process_map".to_string(),
            is_async: false,
            inputs: vec![FnParamMetadata {
                name: "data".to_string(),
                ty: Type::Map {
                    key_type: Box::new(Type::String),
                    value_type: Box::new(Type::Optional {
                        inner_type: Box::new(Type::Int32),
                    }),
                },
                by_ref: false,
                optional: false,
                default: None,
            }],
            return_type: None,
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings.contains("java.util.Map<java.lang.String, java.lang.@org.jspecify.annotations.Nullable Integer>"),
            "map with optional value should have @Nullable on value type:\n{}",
            bindings.lines().filter(|l| l.contains("processMap")).collect::<Vec<_>>().join("\n")
        );
    }

    #[test]
    fn nullness_nested_optional_in_list() {
        let mut group = test_group();
        group.add_item(Metadata::Func(FnMetadata {
            module_path: "test".to_string(),
            name: "process_list".to_string(),
            is_async: false,
            inputs: vec![FnParamMetadata {
                name: "data".to_string(),
                ty: Type::Sequence {
                    inner_type: Box::new(Type::Optional {
                        inner_type: Box::new(Type::String),
                    }),
                },
                by_ref: false,
                optional: false,
                default: None,
            }],
            return_type: None,
            throws: None,
            checksum: None,
            docstring: None,
        }));
        let ci = ComponentInterface::from_metadata(group).unwrap();
        let bindings = generate_bindings(&nullness_config(), &ci).unwrap();
        assert!(
            bindings
                .contains("java.util.List<java.lang.@org.jspecify.annotations.Nullable String>"),
            "list with optional element should have @Nullable on element type:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("processList"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }

    #[test]
    fn callback_interface_helpers_use_class_style_names() {
        let mut group = MetadataGroup {
            namespace: NamespaceMetadata {
                crate_name: "test".to_string(),
                name: "test".to_string(),
            },
            namespace_docstring: None,
            items: Default::default(),
        };
        group.add_item(Metadata::CallbackInterface(CallbackInterfaceMetadata {
            module_path: "test".to_string(),
            name: "Histogram".to_string(),
            docstring: None,
        }));
        group.add_item(Metadata::TraitMethod(TraitMethodMetadata {
            module_path: "test".to_string(),
            trait_name: "Histogram".to_string(),
            index: 0,
            name: "record".to_string(),
            is_async: false,
            inputs: vec![FnParamMetadata {
                name: "value".to_string(),
                ty: Type::Float64,
                by_ref: false,
                optional: false,
                default: None,
            }],
            return_type: None,
            throws: None,
            takes_self_by_arc: false,
            checksum: None,
            docstring: None,
        }));

        let mut ci = ComponentInterface::from_metadata(group).unwrap();
        ci.derive_ffi_funcs().unwrap();

        let bindings = generate_bindings(&Config::default(), &ci).unwrap();

        assert!(
            bindings.contains("public void record(double value);"),
            "expected callback interface API to preserve the Rust method name:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("double value"))
                .collect::<Vec<_>>()
                .join("\n")
        );
        assert!(
            bindings.contains("public static final class RecordCallback implements UniffiCallbackInterfaceHistogramMethod0.Fn"),
            "expected callback helper class to use a Java class-style name:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("implements UniffiCallbackInterfaceHistogramMethod0.Fn"))
                .collect::<Vec<_>>()
                .join("\n")
        );
        assert!(
            bindings.contains("RecordCallback.INSTANCE"),
            "expected generated callback helper references to use the renamed helper class:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains(".INSTANCE"))
                .collect::<Vec<_>>()
                .join("\n")
        );
        assert!(
            !bindings.contains("public static class record implements"),
            "unexpected lowercase helper class leaked into generated bindings:\n{}",
            bindings
                .lines()
                .filter(|l| l.contains("class record"))
                .collect::<Vec<_>>()
                .join("\n")
        );
    }
}