prebindgen-jni 0.5.0

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

use kotlin_codegen::KtType;
use prebindgen_registry::{Building, Conversions, Crossing, RegistryBuilder};

use super::*;

/// The `#[allow(...)]` carried by every generated converter `fn`.
///
/// Generated converters are uniform templates, not hand-written idiomatic Rust,
/// so beyond the name / unused suppressions this allows the clippy lints those
/// templates inherently trip — none of which flag a real issue in generated
/// code, and which are only avoidable by contorting the emitted code:
/// * `needless_question_mark` — a range-checked input's `Ok(try_from(..)?)`;
/// * `let_and_return` — a multi-stage conversion fold's trailing `let`;
/// * `nonminimal_bool` / `eq_op` — a representation-domain guard whose bounds
///   are the scalar's min/max (`true &&`) or which has no exclusions (`!(false)`).
fn generated_converter_attr() -> syn::Attribute {
    syn::parse_quote!(#[allow(
        non_snake_case,
        unused_mut,
        unused_variables,
        unused_braces,
        // A representation-agnostic converter says the same thing for every
        // spelling, so the plain spelling gets the degenerate form of it: a
        // reflexive `.into()`, a deref that is a no-op, parens around a value
        // that needed none. Suppressing per-shape would mean asking which
        // spelling this is, which is the guessing #270 removed.
        unused_parens,
        dead_code,
        clippy::useless_conversion,
        clippy::needless_question_mark,
        clippy::let_and_return,
        clippy::nonminimal_bool,
        clippy::eq_op
    )])
}

// ──────────────────────────────────────────────────────────────────────
// Inherent helpers — wrapper builders (used by both Prebindgen impl
// and consuming-crate wrapper exts like ZenohJniExt).
// ──────────────────────────────────────────────────────────────────────

impl Declarations {
    /// Build the standard JNI input-converter `fn` for a Rust type this
    /// **adapter composed**. Body assumes in-scope `env: &mut JNIEnv` and
    /// `v: &<wire>` (or `v: <wire>` for raw-pointer wires); produces a value of
    /// `rust`. Returned function has its name already set per the JNI plugin's
    /// naming convention.
    ///
    /// There are three composed types: `impl Fn(..)` for a callback, `String`
    /// for the `str` terminal (which yields an owned value the call site
    /// borrows), and `Vec<T>` for the `&[T]` parameter, likewise. None is a
    /// borrow, which is why this spells its input verbatim — there is no `&_`
    /// to splice `'env` into. [`Self::build_input_fn_of`] is the door that
    /// annotates, and it does so off `TypeKind::Ref`, where
    /// `annotate_borrow_with_lifetime` matched a `syn::Type::Reference` and
    /// rebuilt it.
    ///
    /// `exc` ties the body convention to the `Result`'s Rust error type:
    /// * `None` → signature `Result<rust, __JniErr>` and the body is
    ///   wrapped `Ok(<body>)`; `?` inside propagates the framework error.
    /// * `Some(E)` → signature `Result<rust, E>` and the body is emitted
    ///   as-is — `<body>` already evaluates to that `Result`, so no `Ok`
    ///   wrap. `E` is the raw error type peeled from a `Result<T, E>`.
    pub(crate) fn build_input_fn_composed(
        &self,
        rust: &syn::Type,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
    ) -> syn::ItemFn {
        let spelled = rust.to_token_stream();
        self.build_input_fn_parts(&spelled, &spelled, wire, body, exc)
    }

    /// [`Self::build_input_fn`] for a caller holding the **reading** of the Rust
    /// type this converter yields — the terminals and the transparent bridges.
    ///
    /// The borrow annotation is the only thing it does differently:
    /// [`annotate_borrow_with_lifetime`] matched a `syn::Type::Reference` with
    /// no lifetime, and the model states both facts on
    /// [`TypeKind::Ref`](prebindgen_registry::flat::TypeKind::Ref), so this spells
    /// `&'env [mut] <inner>` off the classification. Everything else the
    /// signature needs is the spelling, which is the reading's own tokens.
    pub(crate) fn build_input_fn_of(
        &self,
        rust: &prebindgen_registry::flat::TypeRef,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
        emit: &prebindgen_registry::Emit,
    ) -> syn::ItemFn {
        let spelled = emit.spell(rust);
        let rust_with_lifetime = match rust.kind() {
            prebindgen_registry::flat::TypeKind::Ref {
                lifetime: None,
                mutable,
                inner,
            } => {
                let inner = emit.spell(inner);
                let m = if *mutable { quote!(mut) } else { quote!() };
                quote!(&'env #m #inner)
            }
            _ => spelled.clone(),
        };
        self.build_input_fn_parts(&spelled, &rust_with_lifetime, wire, body, exc)
    }

    pub(crate) fn build_input_fn_parts(
        &self,
        rust: &TokenStream,
        rust_with_lifetime: &TokenStream,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
    ) -> syn::ItemFn {
        let name = input_name(rust, wire);
        let wire_with_lifetime = annotate_jobject_with_lifetime(wire, "v");
        let err_type = exc.cloned().unwrap_or_else(default_err_type);
        let ret_body = body_for_exc(body, exc);
        let gen_allow = generated_converter_attr();
        if matches!(wire, syn::Type::Ptr(_)) {
            syn::parse_quote!(
                #gen_allow
                pub(crate) unsafe fn #name<'env>(env: &mut jni::JNIEnv<'env>, v: #wire) -> ::core::result::Result<#rust_with_lifetime, #err_type> {
                    #ret_body
                }
            )
        } else {
            syn::parse_quote!(
                #gen_allow
                pub(crate) unsafe fn #name<'env, 'v>(env: &mut jni::JNIEnv<'env>, v: &#wire_with_lifetime) -> ::core::result::Result<#rust_with_lifetime, #err_type> {
                    #ret_body
                }
            )
        }
    }

    /// Build the standard JNI output-converter `fn`. Body assumes in-scope
    /// `env: &mut JNIEnv` and `v: <rust>` (by value — handles like
    /// `Subscriber<()>` aren't `Clone`, so callers move into the converter).
    ///
    /// `exc` — see [`Self::build_input_fn`]; same body↔exception coupling,
    /// output side.
    pub(crate) fn build_output_fn(
        &self,
        rust: &syn::Type,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
    ) -> syn::ItemFn {
        self.build_output_fn_parts(&rust.to_token_stream(), wire, body, exc)
    }

    /// [`Self::build_output_fn`] off the **reading**. The output signature takes
    /// the value by move and needs no lifetime splice, so this is the spelling
    /// and nothing else.
    pub(crate) fn build_output_fn_of(
        &self,
        rust: &prebindgen_registry::flat::TypeRef,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
        emit: &prebindgen_registry::Emit,
    ) -> syn::ItemFn {
        self.build_output_fn_parts(&emit.spell(rust), wire, body, exc)
    }

    pub(crate) fn build_output_fn_parts(
        &self,
        rust: &TokenStream,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
    ) -> syn::ItemFn {
        let name = output_name(rust, wire);
        let wire_with_lifetime = annotate_jobject_with_lifetime(wire, "a");
        let err_type = exc.cloned().unwrap_or_else(default_err_type);
        let ret_body = body_for_exc(body, exc);
        let gen_allow = generated_converter_attr();
        syn::parse_quote!(
            #gen_allow
            pub(crate) unsafe fn #name<'a>(env: &mut jni::JNIEnv<'a>, v: #rust) -> ::core::result::Result<#wire_with_lifetime, #err_type> {
                #ret_body
            }
        )
    }

    /// Borrowed string-slice output converter (`&str → jstring`, a single
    /// copy — the dual of the `str` input arm). Shared by two resolver arms so
    /// they emit the SAME-named fn (write.rs dedups by `sig.ident`):
    /// * the rank-1 `&str` arm — the converter actually used for a reference
    ///   accessor leaf (`f(&T) -> &str`, output expansion);
    /// * the rank-0 `str` arm — resolves the unsized `str` reached as the sub
    ///   of `&str` (so required-propagation doesn't flag `str` unresolved).
    ///
    /// Surfaces as Kotlin `String`. Built from a normalized (lifetime-free)
    /// `&str` so both arms produce an identical [`output_name`].
    fn str_ref_output(&self) -> ConverterImpl<KotlinMeta> {
        let outer_ty: syn::Type = syn::parse_quote!(&str);
        let wire: syn::Type = syn::parse_quote!(jni::objects::JString);
        let body: syn::Expr = syn::parse_quote!({
            env.new_string(v).map_err(|e| {
                <__JniErr as ::core::convert::From<String>>::from(format!("encode_str: {}", e))
            })?
        });
        let kotlin_name =
            self.override_kotlin_name(&TypeKey::from_type(&outer_ty), Some(KtType::string()));
        let niches = default_niches_for_wire(&wire);
        ConverterImpl {
            subs: vec![],
            pre_stages: vec![],
            function: self.build_output_fn(&outer_ty, &wire, &body, None),
            destination: wire,
            niches,
            metadata: self.framework_meta(kotlin_name),
        }
    }

    /// `Cow<[u8]>` output converter (any lifetime form) — see the call site
    /// in [`Self::output_terminal`]. `None` when `ty` isn't a
    /// `Cow<…, [u8]>` path.
    fn cow_bytes_output(
        &self,
        ty: &prebindgen_registry::flat::TypeRef,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // `TypeKind::Cow` is the form, and its `inner` is the argument — where
        // this walked a path's last segment, compared the ident to the NAME
        // `"Cow"`, and scanned the angle-bracketed arguments for a `[u8]`.
        let prebindgen_registry::flat::TypeKind::Cow { inner, .. } = ty.kind() else {
            return None;
        };
        if inner.key().as_str() != "[u8]" {
            return None;
        }
        // The generated fn's param type must be resolvable without imports —
        // normalize whatever path form the accessor wrote to the full one.
        let norm_ty: syn::Type = syn::parse_quote!(::std::borrow::Cow<'_, [u8]>);
        let wire: syn::Type = syn::parse_quote!(jni::objects::JByteArray);
        let body: syn::Expr = syn::parse_quote!({
            env.byte_array_from_slice(&v).map_err(|e| {
                <__JniErr as ::core::convert::From<String>>::from(format!(
                    "encode_byte_array: {}",
                    e
                ))
            })?
        });
        let kotlin_name = self.override_kotlin_name(&ty.key(), Some(KtType::byte_array()));
        let niches = default_niches_for_wire(&wire);
        Some(ConverterImpl {
            subs: vec![],
            pre_stages: vec![],
            function: self.build_output_fn(&norm_ty, &wire, &body, None),
            destination: wire,
            niches,
            metadata: self.framework_meta(kotlin_name),
        })
    }

    /// Universal "opaque Box-handle as `jlong`" pair — input side.
    ///
    /// Use for any Rust type whose lifecycle is owned by the Java side:
    /// Java holds the raw `Box<T>` pointer as a `Long` and calls Rust
    /// passing the pointer. The converter handles both parameter
    /// shapes, the decision is taken in `on_function` from the
    /// parameter's syntax:
    ///
    /// **`&T` sites (borrow)**: `OwnedObject::from_raw` stores the
    /// pointer without taking ownership of the `Box`; `Deref<Target
    /// = T>` exposes `&*ptr` so the generated call site can borrow it
    /// as `&T`. The wrapper has no `Drop` — nothing is freed, the
    /// heap allocation stays with Java. The Java side must take the
    /// pointer out of its `NativeHandle.withPtr` (read lock) so the
    /// borrow is sequenced against any concurrent consume / close.
    ///
    /// **`T` sites (consume, by-value)**: the call-site emitter
    /// bypasses `OwnedObject` and inlines `*Box::from_raw(ptr)` —
    /// infallible. The Java side must take the pointer out of its
    /// `NativeHandle.consume` (write lock + atomic null) before
    /// invoking this entry point; that write lock drains concurrent
    /// borrows and the atomic-null ensures the same Long cannot be
    /// passed twice. No `T: Clone` bound (Box requires nothing of T),
    /// so non-Clone handles (`Publisher<'a>`, `Subscriber<()>`) can
    /// consume.
    ///
    /// **Convention** (single rule for both input and output):
    /// * Wire: `jni::sys::jlong` — the same width JNI hands across
    ///   the boundary on every platform (`*mut T` would mismatch
    ///   on 32-bit, where ptr size is 4 but jlong is 8).
    /// * Output: `Box::into_raw(Box::new(v)) as i64` — leak the heap
    ///   allocation to Java; sole owner is whoever later calls
    ///   `Box::from_raw` on the same pointer.
    /// * Input: `OwnedObject::from_raw(*v as *const T)` (borrow only),
    ///   after rejecting null and tag-bit-set values — bit 0 is the
    ///   Kotlin-side closed tag (see `NativeHandle`), so an odd `jlong`
    ///   is a handle that was closed after the wrapper's pre-lock guard;
    ///   it must never be dereferenced.
    /// * Niche: `0i64` / `*v == 0` — `Box::into_raw` never returns 0,
    ///   so `Option<T>` automatically synthesises `0` = `None`,
    ///   matching the legacy "null pointer" ABI for nullable handles.
    ///   A *tagged* (closed-but-present) value is an error, not `None`.
    pub fn opaque_handle_input(
        &self,
        reading: &prebindgen_registry::flat::TypeRef,
        emit: &prebindgen_registry::Emit,
    ) -> ConverterImpl<KotlinMeta> {
        let wire: syn::Type = syn::parse_quote!(jni::sys::jlong);
        let ty = emit.spell(reading);
        let name = input_name(&ty, &wire);
        let gen_allow = generated_converter_attr();
        let function: syn::ItemFn = syn::parse_quote!(
            #gen_allow
            pub(crate) unsafe fn #name<'env, 'v>(
                env: &mut jni::JNIEnv<'env>,
                v: &jni::sys::jlong,
            ) -> ::core::result::Result<OwnedObject<#ty>, __JniErr> {
                // Null or tag-bit-set (closed handle raced past the Kotlin
                // pre-lock guard) — reject before any dereference.
                if *v == 0 || (*v & 1) == 1 {
                    return ::core::result::Result::Err(
                        <__JniErr as ::core::convert::From<String>>::from(
                            "Operation on a closed native handle.".to_string(),
                        ),
                    );
                }
                Ok(unsafe { OwnedObject::from_raw(*v as *const #ty) })
            }
        );
        ConverterImpl {
            subs: vec![],
            function,
            destination: wire,
            pre_stages: vec![],
            niches: Niches::one(syn::parse_quote!(0i64), syn::parse_quote!(*v == 0)),
            // Opaque handles' value-context Kotlin name stays `"Long"`
            // (the jlong wire mention); the *typed* Kotlin rendering is
            // derived from `handle` below. The wrapper's `?` path surfaces
            // an `OwnedObject::from_raw` failure as the framework
            // `JniBindingError`, so the throws fields point at the
            // framework exception.
            metadata: self.opaque_leaf_meta(reading.key()),
        }
    }

    /// Leaf metadata for an opaque handle: value-context name `"Long"`
    /// plus the [`Projection`] that folds outward through wrappers (owned,
    /// [`FoldStrategy::Base`]). The single seam where a Rust type is
    /// first marked a closeable native handle.
    fn opaque_leaf_meta(&self, key: TypeKey) -> KotlinMeta {
        KotlinMeta {
            projection: Some(Projection {
                leaf_key: key,
                owned: true,
                strategy: FoldStrategy::Base,
                kind: ProjectionKind::Handle,
                niche_sentinels: Vec::new(),
            }),
            ..self.framework_meta(Some(KtType::cls("Long")))
        }
    }

    /// Leaf metadata for Rust `u64`: the JNI value-context stays `Long`, while
    /// projection-aware Kotlin emitters surface `ULong` and insert the
    /// bit-preserving `toLong()` / `toULong()` bridge.
    fn unsigned64_leaf_meta(&self) -> KotlinMeta {
        KotlinMeta {
            projection: Some(Projection {
                leaf_key: TypeKey::parse("u64").expect("builtin type key"),
                owned: false,
                strategy: FoldStrategy::Base,
                kind: ProjectionKind::Unsigned64,
                niche_sentinels: Vec::new(),
            }),
            ..self.framework_meta(Some(KtType::long()))
        }
    }

    /// If the user pinned a Kotlin name for `outer_ty` via
    /// [`Self::data_class`] (or it's an opaque-handle entry that
    /// kept its FQN in `kotlin_name`), use that name; otherwise leave
    /// the auto-derived `inherited` value untouched. Lets handler arms
    /// inherit by default but yield to an explicit user pin when one
    /// exists — same precedence the legacy `KotlinTypeMap.lookup`
    /// fallback chain had.
    pub(crate) fn override_kotlin_name(
        &self,
        key: &TypeKey,
        inherited: Option<KtType>,
    ) -> Option<KtType> {
        if let Some(cfg) = self.types.get(key) {
            // Opaque-handle entries keep their typed FQN in
            // `name_spec` for FQN-consumers, but the value-context
            // name is `"Long"` (set on the rank-0 handler's metadata).
            // Don't let that FQN leak into a wrapper's metadata.
            if !cfg.is_opaque() {
                if let Some(spec) = &cfg.name_spec {
                    return Some(KtType::cls(self.fqn_of(spec)));
                }
            }
        }
        inherited
    }

    /// Canonical input-converter name for `(rust, wire)` — exposed
    /// for plugin wrapper exts that build `ConverterImpl::function`
    /// manually with a non-standard return type (e.g.
    /// `impl Into<…>` parameters that can't be expressed via
    /// `input_wrapper_shape`'s fixed signature shape).
    pub fn input_converter_name(&self, rust: &syn::Type, wire: &syn::Type) -> syn::Ident {
        input_name(&rust.to_token_stream(), wire)
    }

    /// Symmetric to [`Self::input_converter_name`].
    pub fn output_converter_name(&self, rust: &syn::Type, wire: &syn::Type) -> syn::Ident {
        output_name(&rust.to_token_stream(), wire)
    }

    fn emitted_source_type_names(
        &self,
        registry: &Registry<KotlinMeta>,
    ) -> std::collections::HashMap<String, syn::Path> {
        let mut names = std::collections::HashMap::new();
        let mut add = |key: &TypeKey| {
            if let Some(short) = rust_short_name_opt(key) {
                // Per-item origin when the type has an indexed
                // `#[prebindgen]` item; else the default module (a declared
                // type re-exported by the primary source, or a deliberately
                // unmarked type like a convert!-only newtype).
                // Parsed, not constructed: a short name is whatever the source
                // wrote, and `Ident::new` PANICS on a raw one (`r#type`)
                // rather than erroring. Pre-existing; found by the raw-name
                // regression added for the sum encoder's twin of this bug.
                let Ok(ident) = syn::parse_str::<syn::Ident>(&short) else {
                    return;
                };
                let module = registry
                    .origin_module(&ident)
                    .unwrap_or_else(|| self.default_module(registry));
                names.insert(short, module);
            }
        };
        for key in self.types.keys() {
            add(key);
        }
        // Rust-side-only boundary types are absent from the type table but
        // still appear in emitted signatures (e.g. the `E` of a peeled
        // `Result<T, E>`), so they need the same qualification.
        for (key, _) in self.rust_side_only_types().collect::<Vec<_>>() {
            add(&key);
        }
        // `convert!`-declared types likewise have no type-table entry but
        // appear in emitted converter signatures.
        for decl in &self.convert_decls {
            add(decl.key());
        }
        names
    }

    /// Walk `item` and prefix every bare single-segment type reference
    /// matching a [`Self::emitted_source_type_names`] name with that name's
    /// origin module. Applied once per emitted item at write
    /// time via [`Prebindgen::post_process_item`] so converter bodies,
    /// type ascriptions, and casts all stay in sync without each emit
    /// site having to remember to qualify.
    fn qualify_item(&self, item: &mut syn::Item, registry: &Registry<KotlinMeta>) {
        let source_names = self.emitted_source_type_names(registry);
        // Names reachable from an array LENGTH (`[u8; MAX]`, `[u8; Holder::N]`).
        //
        // Registry-wide, NOT the declared-surface `source_names`: a length's
        // owner is a compile-time namespace, not a boundary type. Requiring it
        // to be declared would force an otherwise-unused Kotlin class into
        // existence just to make the generated Rust compile, and would be
        // asymmetric with consts, which qualify whether or not JniGenBuilder declared
        // them.
        // EVERY named item the registry indexes. A length is an arbitrary const
        // expression, so it can name a const, the type owning an associated
        // const, or a `const fn` — and enumerating item KINDS here missed one
        // of those three twice, so the enumeration lives in core
        // (`named_item_idents`) where a new kind is added once.
        //
        // The NAME SET is independent of origin stamps and the VALUE falls back
        // to the default module: an origin-less hand-built stream holds elements
        // whose location carries no crate name, and those still need qualifying
        // (core documents `crate` as their module).
        let length_names: std::collections::HashMap<String, syn::Path> = registry
            .named_item_idents()
            .map(|ident| {
                let module = registry
                    .origin_module(ident)
                    .unwrap_or_else(|| self.default_module(registry));
                (ident.to_string(), module)
            })
            .collect();
        let mut visitor = QualifyEmittedTypes {
            source_names: &source_names,
            length_names: &length_names,
        };
        syn::visit_mut::VisitMut::visit_item_mut(&mut visitor, item);
    }

    /// Output side of [`Self::opaque_handle_input`] — see that method's
    /// docs for the full convention.
    pub fn opaque_handle_output(
        &self,
        reading: &prebindgen_registry::flat::TypeRef,
        emit: &prebindgen_registry::Emit,
    ) -> ConverterImpl<KotlinMeta> {
        let wire: syn::Type = syn::parse_quote!(jni::sys::jlong);
        let body: syn::Expr =
            syn::parse_quote!(std::boxed::Box::into_raw(std::boxed::Box::new(v)) as i64);
        ConverterImpl {
            subs: vec![],
            function: self.build_output_fn_of(reading, &wire, &body, None, emit),
            destination: wire,
            pre_stages: vec![],
            niches: Niches::one(syn::parse_quote!(0i64), syn::parse_quote!(*v == 0)),
            // Opaque handles' value-context name `"Long"` + folded
            // `Projection` — see [`Self::opaque_handle_input`] /
            // [`Self::opaque_leaf_meta`]. Framework throws because the
            // wrapper's emitted match-arm still has a `JniBindingError`
            // branch reachable via the chain.
            metadata: self.opaque_leaf_meta(reading.key()),
        }
    }
}

/// The single `signal_error` free function: the one error channel every
/// generated extern uses. Instead of throwing a JVM exception, it invokes
/// the per-call Kotlin `ErrorSink.onError(message: String)` callback with the
/// error's `Display` string. The caller's wrapper installs a default sink
/// that captures the message and rethrows it as a Kotlin exception after the
/// native call returns (so SDK `try/catch` keeps working), but a caller may
/// pass any sink and do anything else. This is the seed of the unified
/// callback return-channel: a later step can add an `onValue(...)` leg so
/// success values flow through the same sink.
///
/// `err` is generic over `Display`, so both the framework `__JniErr`
/// (`JniBindingError`, a `String` wrapper) and a domain `Result<T, E>`'s `E`
/// funnel through one function with no per-type routing.
/// The pending-exception guard shared by both signal helpers: if a JVM
/// exception is already pending (a Java upcall threw during a converter), let
/// it propagate untouched — do NOT invoke an error callback over it, and do
/// not clear/describe it (that would swallow the real exception). The extern
/// returns its sentinel and the pending exception surfaces when control
/// returns to the JVM.
pub(crate) fn build_signal_binding_error_item() -> syn::Item {
    syn::parse_quote!(
        #[allow(non_snake_case, dead_code)]
        pub(crate) fn signal_binding_error(
            env: &mut jni::JNIEnv,
            sink: &jni::objects::JObject,
            mid: &::prebindgen_jni_runtime::CachedIfaceMethod,
            fqn: &str,
            descr: &str,
            je: &str,
        ) {
            if env.exception_check().unwrap_or(false) {
                return;
            }
            // The binding message crosses as the single `String` param of the
            // `JniErrorHandler.run` (`mid`/`fqn`/`descr` are the per-extern
            // cached interface method).
            let __je: jni::objects::JObject = match env.new_string(je) {
                Ok(s) => s.into(),
                Err(e) => {
                    tracing::error!("signal_binding_error: new_string failed: {}", e);
                    return;
                }
            };
            let __args = [jni::sys::jvalue { l: __je.as_raw() }];
            // On failure leave any pending exception in place (don't describe/
            // clear it) so it propagates rather than being swallowed.
            if let Err(e) = mid.call_object(env, fqn, "run", descr, sink, &__args) {
                tracing::error!("signal_binding_error: error-callback invoke failed: {}", e);
            }
        }
    )
}

/// Invoke a fallible function's typed **domain** error handler
/// (`<Src>Handler.run(ze…)`) with the pre-encoded decomposed-error leaves.
/// There is no leading `je` and no defaults — this is called ONLY on `Err(E)`.
pub(crate) fn build_signal_domain_error_item() -> syn::Item {
    syn::parse_quote!(
        #[allow(non_snake_case, dead_code)]
        pub(crate) fn signal_domain_error(
            env: &mut jni::JNIEnv,
            sink: &jni::objects::JObject,
            mid: &::prebindgen_jni_runtime::CachedIfaceMethod,
            fqn: &str,
            descr: &str,
            ze: &[jni::sys::jvalue],
        ) {
            if env.exception_check().unwrap_or(false) {
                return;
            }
            // On failure leave any pending exception in place (don't describe/
            // clear it) so it propagates rather than being swallowed.
            if let Err(e) = mid.call_object(env, fqn, "run", descr, sink, ze) {
                tracing::error!("signal_domain_error: error-callback invoke failed: {}", e);
            }
        }
    )
}

/// One `#[no_mangle] extern "C"` destructor per opaque handle — the Rust
/// counterpart to the `public fun free() = free {
/// freePtr<suffix>(it) }` / `private external fun freePtr<suffix>` pair
/// emitted by [`render_typed_handle_source`] — so the framework owns *both*
/// halves of the destructor for every typed-handle class. Each body is the
/// uniform `drop(Box::from_raw(ptr as *mut T))`; the inner `T`'s own `Drop`
/// runs (e.g. `Publisher` network-undeclare) with no special casing.
///
/// The symbol follows the documented scheme
/// `Java_<package_underscores>_<class_short>_<mangled-freePtr>`,
/// where `class_short` is the last segment of the typed-handle FQN
/// (`TypeConfig::kotlin_name`) and the `freePtr` name passes through
/// the package/class-aware method hook — exact symmetry with the Kotlin
/// `external fun <mangled-freePtr>` declaration in
/// [`render_typed_handle_source`]. `ext.types` is a `HashMap`, so the
/// items are sorted by symbol to keep generated output deterministic.
///
/// Emission is gated on the resolved `registry`: a destructor is only
/// emitted for an opaque handle whose type a scanned `#[prebindgen]` fn
/// actually references (as input or output). This mirrors converter
/// emission and keeps feature-gated handles (e.g. `zenoh-ext`-only types
/// whose declare/undeclare fns are `#[cfg]`'d out of the scan) from
/// producing destructors that reference types not in scope.
pub(crate) fn build_handle_destructor_items(
    ext: &Declarations,
    registry: &Registry<KotlinMeta>,
    emit: &prebindgen_registry::Emit,
) -> Vec<syn::Item> {
    let mut named: Vec<(String, syn::Item)> = Vec::new();
    for (key, cfg) in &ext.types {
        if !cfg.is_opaque() {
            continue;
        }
        // Skip handles the (feature-aware) scan never references — their
        // type may not be in scope in the generated module. Keyed directly:
        // this used to spell the key into tokens purely so `reading_of` could
        // re-key them, twice (#291).
        let Some(reading) = registry.reading(key) else {
            continue;
        };
        if registry.input_entry(&reading).is_none() && registry.output_entry(&reading).is_none() {
            continue;
        }
        let ty = emit.spell(&reading);
        let class_fqn = cfg
            .name_spec
            .as_ref()
            .map(|s| ext.fqn_of(s))
            .unwrap_or_else(|| {
                panic!(
                    "build_handle_destructor_items: opaque handle `{}` has no \
                     name spec to derive a destructor symbol from",
                    key.as_str()
                )
            });
        let class_short = class_fqn.rsplit('.').next().unwrap_or(&class_fqn);
        let class_package = class_fqn.rsplit_once('.').map(|(pkg, _)| pkg).unwrap_or("");
        let free_ptr = ext.mangle_method(class_package, class_short, "freePtr");
        let symbol = super::symbol::native_symbol(class_package, class_short, &free_ptr);
        let ident = syn::Ident::new(&symbol, Span::call_site());
        // Bit 0 of the jlong is the Kotlin-side closed tag, so every handle
        // type must leave it free: `Box` pointers to `T` are `align_of::<T>()`
        // aligned, hence the compile-time floor of 2. Spelled as an `if` +
        // `panic!` (not `assert!`) so the type reference is real AST — the
        // `qualify_item` pass does not descend into macro token streams.
        let item: syn::Item = syn::parse_quote!(
            const _: () = {
                if ::core::mem::align_of::<#ty>() < 2 {
                    panic!(
                        "opaque handle types must have alignment >= 2 (bit 0 is the closed tag)"
                    );
                }
            };
        );
        named.push((format!("{symbol}__align_assert"), item));
        let item: syn::Item = syn::parse_quote!(
            #[no_mangle]
            #[allow(non_snake_case, unused_variables)]
            pub(crate) unsafe extern "C" fn #ident(
                _env: jni::JNIEnv,
                _class: jni::objects::JClass,
                ptr: jni::sys::jlong,
            ) {
                if ptr != 0 && (ptr & 1) == 0 {
                    drop(Box::from_raw(ptr as *mut #ty));
                }
            }
        );
        named.push((symbol, item));
    }
    named.sort_by(|a, b| a.0.cmp(&b.0));
    named.into_iter().map(|(_, item)| item).collect()
}

/// Which built-in wrapper a converter is being built for — **the model's
/// answer, not a guess from the spelling**.
///
/// This used to be a `&syn::Type` wildcard pattern (`Option<_>`, `& mut _`)
/// rebuilt from the type's tokens and compared as a *string*. That made the
/// dispatch depend on how Rust happened to spell the type: `Box<Option<T>>`
/// reconstructed as `Box<_>`, matched no pattern, and got no converter at all
/// (#270) — even though the model classifies it `Optional` and says so.
///
/// So the shape comes from [`TypeKind`](prebindgen_registry::flat::TypeKind) and
/// the spelling comes from `spell()`, which is the same split the rest of
/// the pipeline follows: classify off `kind`, spell with `spell()`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum WrapperShape {
    /// `Ref` — a borrow of its inner.
    Borrow { mutable: bool },
    /// `Optional` whose inner is a `Ref` — the deep handle-borrow form, tried
    /// before [`Self::Optional`].
    OptionRef { mutable: bool },
    /// `Sequence` — a run of its element.
    Sequence,
    /// `Optional` — its inner, or absent.
    Optional,
}

/// What generated Rust can do with one wrapper the model
/// [erases](prebindgen_registry::flat::TRANSPARENT_WRAPPERS).
///
/// Erasure and reconstruction are different questions, and only the first is the
/// model's. `Box<T>` *is* `T` to every destination language — but undoing it in
/// Rust is `*b`, undoing a `Cow` is `into_owned()`, and undoing an `Rc` is not
/// possible at all. There is no trait spanning those, so the operations live
/// here, one row per wrapper, instead of as a special case per converter.
///
/// **Adding a wrapper is adding a row.** Put its name in
/// `TRANSPARENT_WRAPPERS` (the model decides what it erases) and a row here
/// (the adapter decides what it can rebuild); `every_erased_wrapper_has_ops`
/// fails if the two disagree, so a wrapper cannot become transparent without
/// this file having an answer for it.
struct WrapperOps {
    /// Its last path segment, as `TRANSPARENT_WRAPPERS` spells it.
    name: &'static str,
    /// Move the inner value **out**. `None` when the representation does not
    /// permit it — a `Cow` payload cannot be moved through `Deref` (`E0507`),
    /// and neither can an `Rc`'s.
    ///
    /// Emitted **unparenthesized**: every consumer splices the result into a
    /// `let` initializer, where a wrapping paren is `unused_parens` — and
    /// generated code runs through the consumer's own lints, where that is a
    /// denial. A consumer that splices into a tighter position (a method
    /// receiver, a field base) parenthesizes at its own site.
    read: Option<fn(TokenStream) -> TokenStream>,
    /// Build it **from** the inner value. `None` when not supported.
    build: Option<fn(TokenStream) -> TokenStream>,
}

/// The operations table. One row per wrapper the model erases.
const WRAPPER_OPS: &[WrapperOps] = &[
    WrapperOps {
        name: "Box",
        // `*b` moves out of a box, and `Box::new` puts it back.
        read: Some(|e| quote!(*#e)),
        build: Some(|e| quote!(::std::boxed::Box::new(#e))),
    },
    WrapperOps {
        name: "Cow",
        // Reading would be `into_owned()`, which needs `B: ToOwned` — not
        // implied by anything the model knows about the payload. Refused until
        // something needs it; that is one row, not a redesign.
        read: None,
        // Building is a DIFFERENT question, and it is refused for a different
        // reason — the two `None`s here are not one fact repeated.
        //
        // `Cow::Owned(v)` is well-typed: an input rebuild owns its value, and
        // `Cow<'_, [T]>` takes a `Vec<T>` while `Cow<'_, str>` takes a
        // `String`. So this is not "cannot", it is **should not**. A source
        // spells `Cow` to accept borrowed data without copying; a binding that
        // can only ever hand it `Owned` pays that copy on every call and
        // silently removes the borrow path — and the callee can see the
        // difference (`matches!(c, Cow::Borrowed(_))`), so it is observable
        // rather than merely wasteful.
        //
        // **Deliberate, not deferred.** If a binding decides the copy is
        // acceptable for its own source, this is one line —
        // `Some(|e| quote!(::std::borrow::Cow::Owned(#e)))` — and nothing else
        // moves. The refusal is here so that decision is made on purpose.
        build: None,
    },
];

fn wrapper_ops(name: &str) -> Option<&'static WrapperOps> {
    WRAPPER_OPS.iter().find(|w| w.name == name)
}

/// What a wrapper arm's converter **yields**.
///
/// Two cases, and the code always had both — one of them just had no name:
///
/// * `Reading` — the crossing's own type, as the model classified it. Every
///   arm but one gets this.
/// * `Composed` — a shape this adapter built. The `&[T]` parameter is the only
///   instance: it decodes to an owned `Vec<T>` the call site borrows, and #280
///   leaves `api::lang` no way to mint that reading. A composed shape carries
///   no erased wrappers by construction — it *is* the canonical form.
///
/// The distinction is what the `canonical` argument used to stand in for. A
/// spelling was compared token-for-token against a reconstructed
/// `Option<t1>` / `Vec<t1>` / `&t1` to find out what wrappers sat over it —
/// which is [`TypeRef::erased_wrappers`], asked of the classification instead
/// of re-derived from tokens.
pub(crate) enum Produced<'a> {
    Reading(&'a prebindgen_registry::flat::TypeRef),
    /// Boxed: a `syn::Type` dwarfs the borrow beside it, and the composed case
    /// is the rare one (a single arm).
    Composed(Box<syn::Type>),
}

impl Produced<'_> {
    /// The chain of wrappers standing between what this yields and the
    /// canonical shape its `kind` names, outermost first — empty when the
    /// source already wrote the canonical form.
    ///
    /// `None` when a wrapper has no [`WrapperOps`], so no converter claims it.
    fn bridge_layers(&self) -> Option<Vec<&'static WrapperOps>> {
        match self {
            Produced::Reading(r) => r.erased_wrappers().into_iter().map(wrapper_ops).collect(),
            Produced::Composed(_) => Some(Vec::new()),
        }
    }

    /// True when what this yields IS the canonical shape — no wrapper to
    /// bridge. The arms that hand back an inner converter verbatim require it:
    /// there is no value in hand to wrap or unwrap.
    fn is_canonical(&self) -> bool {
        self.bridge_layers().is_some_and(|l| l.is_empty())
    }

    /// The tokens generated Rust spells for this type.
    fn spell(&self, emit: &prebindgen_registry::Emit) -> TokenStream {
        match self {
            Produced::Reading(r) => emit.spell(r),
            Produced::Composed(t) => t.to_token_stream(),
        }
    }

    /// This type's identity.
    fn key(&self) -> TypeKey {
        match self {
            Produced::Reading(r) => r.key(),
            Produced::Composed(t) => TypeKey::from_type(t),
        }
    }

    /// The borrow this yields, when the source wrote one — the reading's own
    /// `TypeKind::Ref`. A composed shape is never a borrow.
    fn borrow(&self) -> Option<(&prebindgen_registry::flat::TypeRef, bool)> {
        match self {
            Produced::Reading(r) => match r.kind() {
                prebindgen_registry::flat::TypeKind::Ref { mutable, inner, .. } => {
                    Some((inner, *mutable))
                }
                _ => None,
            },
            Produced::Composed(_) => None,
        }
    }
}

impl Declarations {
    /// [`Self::build_input_fn`] over either case.
    fn build_input_fn_produced(
        &self,
        produced: &Produced<'_>,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
        emit: &prebindgen_registry::Emit,
    ) -> syn::ItemFn {
        match produced {
            Produced::Reading(r) => self.build_input_fn_of(r, wire, body, exc, emit),
            Produced::Composed(t) => self.build_input_fn_composed(t, wire, body, exc),
        }
    }

    /// [`Self::build_output_fn`] over either case.
    fn build_output_fn_produced(
        &self,
        produced: &Produced<'_>,
        wire: &syn::Type,
        body: &syn::Expr,
        exc: Option<&syn::Type>,
        emit: &prebindgen_registry::Emit,
    ) -> syn::ItemFn {
        match produced {
            Produced::Reading(r) => self.build_output_fn_of(r, wire, body, exc, emit),
            Produced::Composed(t) => self.build_output_fn(t, wire, body, exc),
        }
    }
}

/// Read the converter's `v` as the canonical shape, undoing each layer
/// outside-in. `None` when any layer cannot be read through — the crossing then
/// stays **unresolved**, naming the type, rather than resolving and emitting
/// Rust the consumer cannot build (#270 review).
fn read_as_canonical(produced: &Produced<'_>) -> Option<TokenStream> {
    let layers = produced.bridge_layers()?;
    let mut e = quote!(v);
    for w in layers {
        e = (w.read?)(e);
    }
    Some(e)
}

/// Build the spelling from a canonical value — the input-side peer, applying
/// each layer inside-out.
fn build_from_canonical(produced: &Produced<'_>, value: TokenStream) -> Option<TokenStream> {
    let layers = produced.bridge_layers()?;
    let mut e = value;
    for w in layers.into_iter().rev() {
        e = (w.build?)(e);
    }
    Some(e)
}

/// Move a value the **source** produced out of the transparent wrappers its
/// spelling adds over its classification, so an emitter that binds it holds the
/// canonical shape — `Box<Option<T>>` → `(*e)`, an unwrapped spelling → `e`
/// unchanged.
///
/// The counterpart of [`bind_as_option`](super::emit::bind_as_option) for an
/// **owned** position. A type-ascribed `let` is a coercion site and serves any
/// representation, but coercion applies to *references*: a value whose payload
/// downstream moves has to be moved out of the wrapper instead, which is what
/// [`WrapperOps::read`] does and what only some wrappers permit.
///
/// `None` when a layer cannot be read through (`Cow`, whose payload cannot be
/// moved out by `Deref`) — the caller then has an unrepresentable crossing to
/// report, and must not emit the match anyway.
///
/// **This answers for one layer's spelling.** It undoes the wrappers standing
/// over `ty`'s own classification; a wrapper *inside* — the `Box` of
/// `Option<Box<Vec<T>>>` — belongs to the inner reading and is that layer's
/// question, per [`TypeRef::erased_wrappers`](prebindgen_registry::flat::TypeRef::erased_wrappers).
pub(crate) fn read_through_erased_wrappers(
    ty: &prebindgen_registry::flat::TypeRef,
    e: TokenStream,
) -> Option<TokenStream> {
    let mut out = e;
    // Outermost first, which is the order they have to come off in.
    for name in ty.erased_wrappers() {
        out = (wrapper_ops(name)?.read?)(out);
    }
    Some(out)
}

/// Put back the transparent wrappers a **rebuild** dropped, so a value the
/// emitter constructed from the classification has the type the source spelled
/// — `Box<Option<S>>` ← `Box::new(v)`, an unwrapped spelling ← `v` unchanged.
///
/// The input-side dual of [`read_through_erased_wrappers`], and the reason both
/// live here rather than at the sites that need them: the specialized input
/// lowerings do not *decode* their parameter, they **rebuild** it — a literal
/// `S { .. }`, an `Option::Some(v)`, a `Vec<T>` pushed element by element — and
/// a rebuild from the classification alone produces the *stripped* type. Handing
/// that to a parameter spelled `Box<..>` is an `E0308` in the generated crate,
/// which is why this is one rule in one place instead of three selection sites
/// each remembering it.
///
/// Applied **innermost-out**, the reverse of reading: the value in hand is the
/// canonical shape, and each layer wraps what the previous one produced.
///
/// `None` when any layer has no [`WrapperOps::build`] — `Cow`, by policy rather
/// than by impossibility; see its row. A caller that gets `None` has a crossing
/// it cannot serve and must decline or report it, never emit the bare value.
///
/// **This answers for one layer's spelling.** It restores the wrappers standing
/// over `ty`'s own classification; a wrapper *inside* — the `Box` of
/// `Option<Box<S>>` — belongs to the inner reading, is applied when that layer
/// is built, and is invisible here. An erasure sits **outside** the layer it
/// wraps, so a rebuild collects wrappers as it descends and applies them as it
/// comes back out.
pub(crate) fn build_through_erased_wrappers(
    ty: &prebindgen_registry::flat::TypeRef,
    value: TokenStream,
) -> Option<TokenStream> {
    build_through_wrappers(&ty.erased_wrappers(), value)
}

/// [`build_through_erased_wrappers`] over a wrapper list already taken off a
/// reading — for a plan that recorded *what to put back* rather than keeping the
/// whole `TypeRef` to ask again.
///
/// The list is the only part of the reading a rebuild uses, and it is two
/// pointers instead of a `TypeRef`'s ~264 bytes. That matters because these
/// plans live in `InputKind`, whose size every variant pays.
pub(crate) fn build_through_wrappers(
    names: &[&'static str],
    value: TokenStream,
) -> Option<TokenStream> {
    let mut out = value;
    for name in names.iter().rev() {
        out = (wrapper_ops(name)?.build?)(out);
    }
    Some(out)
}

/// Per-shape **input** wrapper converter builders (`&`/`Option<&>`/`Vec`/
/// `Option`). Each returns `Some(ConverterImpl)` only for the [`WrapperShape`]
/// it claims; [`Declarations::input_wrapper_shape`] chains them in priority
/// order. The shapes are disjoint — except the two `Optional` sub-cases
/// (direct-handle-by-value vs general), which share one and so live together in
/// [`Declarations::input_option`] to keep their original fall-through.
///
/// Each takes `produced`: the Rust type the converter's function **yields**.
/// Normally that is the crossing's own spelling, so a `Box<Option<T>>` crossing
/// produces a `Box<Option<T>>` rather than silently declaring `Option<T>` and
/// mismatching its call site. The one deliberate exception is a `&[T]`
/// parameter, which decodes to an owned `Vec<T>` the call site borrows — see
/// [`Declarations::select_input_type`].
impl Declarations {
    /// `& _` / `& mut _` borrow: share T's resolved converter — `&T`'s entry
    /// points at the same `ItemFn` (the fn returns owned `T`; the call site in
    /// `emit_jni_function_wrapper` adds `&decoded`). Exists so the
    /// wildcard-substitution machinery marks T required transitively from `&T`.
    fn input_borrow(
        &self,
        shape: WrapperShape,
        produced: &Produced<'_>,
        t1: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        let WrapperShape::Borrow { .. } = shape else {
            return None;
        };
        // This converter does NOT produce the spelled type: it hands back the
        // inner type's own entry, and the call site adds the `&`. So there is no
        // value in hand to unwrap a representation from, and a wrapped spelling
        // — `Box<&T>` — must not resolve here (it would pass an owned `T` where
        // `Box<&T>` is expected).
        if !produced.is_canonical() {
            return None;
        }
        let inner = registry.input_entry(t1)?;
        let outer_ty = produced.key();
        // `&T` / `&mut T` are Kotlin-side no-ops — inherit the inner
        // type's name, unless the user pinned an explicit override
        // on the outer form itself (rare but legal).
        let kotlin_name = self.override_kotlin_name(&outer_ty, inner.metadata.kotlin_name.clone());
        // The outer form shares T's converter function verbatim, so it
        // inherits T's throws behaviour. A borrowed handle (mut or not) is
        // still opaque (param classification needs to see it), but the holder
        // doesn't own it — mark `owned: false` so `close()` emission skips it.
        let projection = inner
            .metadata
            .projection
            .clone()
            .map(|h| Projection { owned: false, ..h });
        Some(ConverterImpl {
            subs: vec![],
            destination: inner.destination.clone(),
            function: inner.function.clone(),
            pre_stages: vec![],
            niches: inner.niches.clone(),
            metadata: KotlinMeta {
                kotlin_name,
                value_rust_type: None,
                projection,
            },
        })
    }

    /// `Option<&T>` / `Option<&mut T>` for opaque T: returns
    /// `Option<OwnedObject<T>>` (the call site `.as_deref()` coerces back).
    /// `None` for non-opaque inners — the resolver then offers `Option<_>`
    /// over `&T` and the general handler takes it.
    fn input_option_ref(
        &self,
        shape: WrapperShape,
        produced: &Produced<'_>,
        t1: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // `t1`'s spelling, for the parts that ask spelling questions — the
        // canonical form a produced spelling is compared against, and the
        // type ascriptions the generated body writes. Everything else takes
        // the READING itself (#284).
        let t1_ty = emit.spell(t1);
        let WrapperShape::OptionRef { .. } = shape else {
            return None;
        };
        // Produces `Option<OwnedObject<T>>`, which the call site adapts with
        // `.as_deref()` — again not the spelled type, so a wrapped spelling has
        // nothing to bridge and must not resolve. See `input_borrow`.
        if !produced.is_canonical() {
            return None;
        }
        let inner = registry.input_entry(t1)?;
        if !inner.metadata.is_direct_handle() {
            // Non-opaque: let the general `Option<_>` handler take it.
            return None;
        }
        let inner_wire = inner.destination.clone();
        let inner_conv = inner.function.sig.ident.clone();
        let outer_ty = produced.key();
        let outer_spelled = produced.spell(emit);
        let name = input_name(&outer_spelled, &inner_wire);
        let gen_allow = generated_converter_attr();
        let function: syn::ItemFn = syn::parse_quote!(
            #gen_allow
            pub(crate) unsafe fn #name<'env, 'v>(
                env: &mut jni::JNIEnv<'env>,
                v: &#inner_wire,
            ) -> ::core::result::Result<Option<OwnedObject<#t1_ty>>, __JniErr> {
                Ok({
                    if *v == 0 { None } else { Some(#inner_conv(env, v)?) }
                })
            }
        );
        let kotlin_name = self.override_kotlin_name(&outer_ty, inner.metadata.kotlin_name.clone());
        let projection = inner.metadata.projection.clone().map(|h| Projection {
            owned: false,
            // `Option<&Handle>` always rides the inner's `*v == 0` niche
            // (body is `if *v == 0 { None } else { ... }` above), so
            // null is the `0i64` sentinel — never JVM boxed.
            strategy: FoldStrategy::Optional(NullableKind::Niche, Box::new(h.strategy)),
            ..h
        });
        Some(ConverterImpl {
            subs: vec![],
            pre_stages: vec![],
            function,
            destination: inner_wire,
            niches: Niches::empty(),
            metadata: KotlinMeta {
                kotlin_name,
                value_rust_type: None,
                projection,
            },
        })
    }

    /// `Vec<T>` (input side): wire is `JObject` carrying a Java
    /// `List<InnerWire>`; iterate, decode each element via the inner converter,
    /// collect into a `Vec`. (`Vec<u8>` is special-cased at rank-0.)
    fn input_vec(
        &self,
        shape: WrapperShape,
        produced: &Produced<'_>,
        t1: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // `t1`'s spelling, for the parts that ask spelling questions — the
        // canonical form a produced spelling is compared against, and the
        // type ascriptions the generated body writes. Everything else takes
        // the READING itself (#284).
        let t1_ty = emit.spell(t1);
        if shape != WrapperShape::Sequence {
            return None;
        }
        let inner = registry.input_entry(t1)?;
        reject_vec_of_handle(&inner.metadata.projection, t1);
        let inner_wire = inner.destination.clone();
        if !is_jobject_shaped_wire(&inner_wire) {
            return None;
        }
        // The element's COMPLETE wire -> Rust chain: a `convert!` element
        // (`Label` -> `String`) reaches its value through the rust-side stages,
        // not through the wire-facing converter alone.
        let inner_conv = crate::jni::emit::composed_inner_input(inner, quote::quote!(&__elem_wire));
        let outer_ty = produced.key();
        // Bridgeable first — see `box_layers_to`.
        let build = build_from_canonical(produced, quote::quote!(__out))?;
        let wire: syn::Type = syn::parse_quote!(jni::objects::JObject);
        let body: syn::Expr = syn::parse_quote!({
            let __list = jni::objects::JList::from_env(env, v)
                .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("Vec<_>: list-from-env: {}", e)))?;
            let mut __it = __list.iter(env)
                .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("Vec<_>: list-iter: {}", e)))?;
            let mut __out: Vec<#t1_ty> = Vec::new();
            while let Some(__obj) = __it.next(env)
                .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("Vec<_>: list-next: {}", e)))?
            {
                let __elem_wire: #inner_wire = __obj.into();
                let __elem: #t1_ty = #inner_conv;
                __out.push(__elem);
            }
            #build
        });
        let inner_kotlin = inner.metadata.kotlin_name.clone()?;
        let kotlin_name = self.override_kotlin_name(
            &outer_ty,
            // `List` is auto-imported in Kotlin (default imports).
            Some(KtType::generic("List", [inner_kotlin])),
        );
        Some(ConverterImpl {
            subs: vec![],
            pre_stages: vec![],
            function: self.build_input_fn_produced(produced, &wire, &body, None, emit),
            destination: wire,
            niches: Niches::empty(),
            metadata: KotlinMeta {
                kotlin_name,
                value_rust_type: None,
                projection: None,
            },
        })
    }

    /// `Option<T>`: first the direct-opaque-handle by-value consume (wire
    /// `jlong`, `0` = `None`, `Box` reconstructed and `T` moved out), then —
    /// when the inner isn't a direct handle — the general nullable fold. The
    /// two share the `Option<_>` pattern, so they stay in one method to keep
    /// the original sequential fall-through.
    fn input_option(
        &self,
        shape: WrapperShape,
        produced: &Produced<'_>,
        t1: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // `t1`'s spelling, for the parts that ask spelling questions — the
        // canonical form a produced spelling is compared against, and the
        // type ascriptions the generated body writes. Everything else takes
        // the READING itself (#284).
        let t1_ty = emit.spell(t1);
        if shape == WrapperShape::Optional {
            let inner = registry.input_entry(t1)?;
            if inner.metadata.is_direct_handle() {
                let inner_wire = inner.destination.clone();
                let outer_ty = produced.key();
                let outer_spelled = produced.spell(emit);
                let build = build_from_canonical(produced, quote::quote!(__v))?;
                let name = input_name(&outer_spelled, &inner_wire);
                let gen_allow = generated_converter_attr();
                let function: syn::ItemFn = syn::parse_quote!(
                    #gen_allow
                    pub(crate) unsafe fn #name<'env, 'v>(
                        env: &mut jni::JNIEnv<'env>,
                        v: &#inner_wire,
                    ) -> ::core::result::Result<#outer_spelled, __JniErr> {
                        Ok({
                            let __v: ::core::option::Option<#t1_ty> = if *v == 0 {
                                None
                            } else if (*v & 1) == 1 {
                                // Tagged (closed) handle raced past the Kotlin
                                // pre-lock guard — present-but-closed is an
                                // error, absent is None.
                                return ::core::result::Result::Err(
                                    <__JniErr as ::core::convert::From<String>>::from(
                                        "Operation on a closed native handle.".to_string(),
                                    ),
                                );
                            } else {
                                Some(*std::boxed::Box::from_raw(*v as *mut #t1_ty))
                            };
                            #build
                        })
                    }
                );
                let kotlin_name =
                    self.override_kotlin_name(&outer_ty, inner.metadata.kotlin_name.clone());
                let projection = inner.metadata.projection.clone().map(|h| Projection {
                    owned: true,
                    // Rides the inner's `*v == 0` niche, so the wire stays
                    // `jlong` and `None` is the `0` sentinel (never JVM boxed).
                    strategy: FoldStrategy::Optional(NullableKind::Niche, Box::new(h.strategy)),
                    ..h
                });
                return Some(ConverterImpl {
                    subs: vec![],
                    pre_stages: vec![],
                    function,
                    destination: inner_wire,
                    niches: Niches::empty(),
                    metadata: KotlinMeta {
                        kotlin_name,
                        value_rust_type: None,
                        projection,
                    },
                });
            }
            // Non-opaque inner: fall through to the general Option handler.
        }
        if shape == WrapperShape::Optional {
            let outer_ty = produced.key();
            let build = build_from_canonical(produced, quote::quote!(__v))?;
            let (wire, inner_body, niches) = option_input(t1, registry, emit)?;
            // `option_input` yields the canonical `Option<T>`; the converter
            // yields the spelling.
            let body: syn::Expr = syn::parse_quote!({
                let __v: ::core::option::Option<#t1_ty> = #inner_body;
                #build
            });
            // Inherit the inner's name; user pins on `Option<T>` win.
            // The nullability marker (`?`) is added by the use site.
            let inherited = registry
                .input_entry(t1)
                .and_then(|e| e.metadata.kotlin_name.clone());
            let kotlin_name = self.override_kotlin_name(&outer_ty, inherited);
            // Fold a Nullable layer over the inner projection (if any). The
            // kind mirrors which path `option_input` took: when it consumed
            // an inner niche, the wire stays identical to the inner's
            // destination and `None` is the niche slot sentinel; the boxed
            // fallback widens the wire to `JObject`.
            let nullable_kind = nullable_kind_for(&wire, t1, registry);
            let projection = registry
                .input_entry(t1)
                .and_then(|e| e.metadata.projection.clone())
                .map(|h| Projection {
                    strategy: FoldStrategy::Optional(nullable_kind, Box::new(h.strategy)),
                    ..h
                });
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_input_fn_produced(produced, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata: KotlinMeta {
                    projection,
                    ..self.framework_meta(kotlin_name)
                },
            });
        }
        None
    }

    /// True when `elem` crosses the boundary as a **single leaf** the foreign
    /// side can reassemble from one wire value — a declared opaque handle
    /// (→ a `jlong` pointer), the `String` builtin (→ `JString`), or the `u64`
    /// scalar projection (→ a raw `jlong` the folder wraps into `ULong`).
    /// Multi-field `data_class` elements (whose output is a `fromParts`
    /// object) and enums are excluded. Drives
    /// [`Self::leaf_vec_fold_elements`].
    ///
    /// Classified from the adapter's declared [`TypeConfig`] table (and the
    /// `String` builtin), not the resolver's output converters — this runs
    /// **before** type resolution, exactly like [`Self::value_struct_decons`].
    fn is_leaf_vec_element(&self, elem: &prebindgen_registry::flat::TypeRef) -> bool {
        match self.types.get(&elem.key()) {
            // A declared opaque handle crosses as a single `jlong` (pointer)
            // leaf that the Kotlin folder wraps into its typed handle class.
            // Enums and multi-field data classes are not leaf-folded — data
            // classes go through `value_struct_decons`.
            Some(cfg) => cfg.is_opaque(),
            // Undeclared: `String` is JObject-shaped; `u64` is the built-in
            // scalar projection whose raw jlong leaf the Kotlin folder wraps
            // into `ULong`. Other primitive collections retain their existing
            // unsupported status (`Vec<u8>` is the rank-0 ByteArray special).
            None => {
                matches!(elem.kind(), prebindgen_registry::flat::TypeKind::String)
                    || elem.key().as_str() == "u64"
            }
        }
    }
}

// ──────────────────────────────────────────────────────────────────────
// Prebindgen impl
// ──────────────────────────────────────────────────────────────────────

impl JniGenBuilder {
    /// State this binding into `registry`: what it exports, what crosses, and
    /// what it defines itself.
    ///
    /// **Push, not pull.** The registry does not call back to ask — the build
    /// script calls this, and the registry stays a passive recorder. That is
    /// what makes "a converter reads a half-built registry" unrepresentable
    /// rather than merely avoided.
    ///
    /// Order-independent, and idempotent apart from the local-fn collision
    /// check: every method it calls records rather than derives.
    /// State this binding into `registry`, then resolve it.
    ///
    /// The pair is always used together, and the generator is what knows both
    /// halves — so it drives, and the registry never calls back. Becomes the
    /// body of `generate(..)` once emission moves here too (#251 phase E).
    /// Read the source, resolve every crossing, and hand back the binding.
    ///
    /// Runs the whole pipeline a build script used to run by hand: parse the
    /// declared sources into a model, describe this binding over it, answer
    /// each crossing in dependency order, and check the set is complete. A
    /// `Flat` and a `Registry` exist inside — they are simply not this caller's
    /// problem.
    pub fn build(self) -> Result<JniGen, prebindgen_registry::WriteRustError> {
        let flat = self
            .sources
            .clone()
            .build()
            .map_err(prebindgen_registry::ScanError::from)?;
        let registry = prebindgen_registry::Registry::builder(flat)?;
        self.build_with(registry)
    }

    /// [`Self::build`] over a registry that was described elsewhere.
    ///
    /// The seam tests use to feed synthetic items without a source directory;
    /// `build` is this with the model read from [`Self::source`].
    ///
    /// **This is the phase change.** The declarations are taken out of the
    /// builder here and never put back: everything below runs against
    /// `&decls`, and what it produces is stored in a [`JniGen`], which has no
    /// route to a `JniGenBuilder` at all.
    pub(crate) fn build_with(
        self,
        registry: prebindgen_registry::RegistryBuilder<KotlinMeta>,
    ) -> Result<JniGen, prebindgen_registry::WriteRustError> {
        let decls = self.decls;
        let registry = decls
            .declare_into(registry)?
            .validate_with(&decls)?
            .convert_with(|crossing, built, emit| decls.convert_crossing(crossing, built, emit))?
            .build()?;
        // Post-resolve invariants, run once here so the writers are pure reads
        // and a `JniGen` is valid by construction.
        decls
            .validate_resolved(&registry)
            .map_err(|message| prebindgen_registry::ScanError::AdapterInvariant { message })?;
        Ok(JniGen { decls, registry })
    }
}

impl Declarations {
    /// Build the conversion for one crossing, against what is already built.
    ///
    /// `None` is *cannot*, never *not yet*: `crossings` hands them out
    /// inner-first, so everything this could compose from is already in `built`.
    fn convert_crossing(
        &self,
        crossing: &Crossing,
        built: &Building<'_, KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        let (dir, key) = crossing;
        // The reading the scan already took for this crossing, fetched by the
        // key the crossing IS. This used to go `key -> to_type() -> reading`,
        // and its own comment called that "the same door, one layer out" as the
        // round trip #263 removed from `api/core`. The door is now keyed, so
        // there is no spelling to rebuild (#284).
        let reading = built.reading(key)?;
        match dir {
            Direction::Input => self.select_input_type(&reading, built, emit).or_else(|| {
                // `impl Fn(args)` that nothing else claimed. Callback args cross
                // in the OPPOSITE direction, which is why their required-ness
                // rides `immediate_edges` rather than this converter's `subs`.
                // The arguments are `TypeRef`s on the classification, so nothing
                // is re-extracted from the signature's syntax.
                let prebindgen_registry::flat::TypeKind::Callback { args } =
                    reading.unwrapped().kind()
                else {
                    return None;
                };
                self.dispatch_fn_input(args, built, emit)
            }),
            Direction::Output => self.select_output_type(&reading, built, emit),
        }
    }

    pub fn declare_into(
        &self,
        mut registry: RegistryBuilder<KotlinMeta>,
    ) -> Result<RegistryBuilder<KotlinMeta>, prebindgen_registry::ScanError> {
        // Binding-local fns first: they become model, and everything below may
        // name one.
        for (item_fn, origin) in self.collect_local_functions() {
            registry = registry.local_function(item_fn, origin)?;
        }

        for ident in self.declared_functions() {
            registry = registry.export(&ident);
        }
        for ident in self.helper_functions() {
            registry = registry.reference(&ident);
        }
        // JniGenBuilder HAS a const mechanism, so const emission is declared-only even
        // when nothing is declared.
        registry = registry.declares_consts();
        for ident in self.declared_consts().into_iter().flatten() {
            registry = registry.export_const(&ident);
        }
        for ty in self.declared_types().into_values() {
            registry = registry.export_type(ty);
        }
        for ident in self.accessor_functions() {
            registry = registry.accessor(&ident);
        }
        for (ident, receiver) in self.method_receivers() {
            registry = registry.method_receiver(&ident, receiver);
        }

        // An expression constant's value type has no captured item to scan.
        for ty in self.required_output_types() {
            registry = registry.cross(Direction::Output, &ty);
        }
        // The other-side type of every `convert!` conversion, in the
        // conversion's direction: an input fn's parameter type needs its own
        // input converter for the composed body to chain through; an output
        // fn's return type needs the output twin.
        let mut convert_edges: Vec<(Crossing, Crossing)> = Vec::new();
        for decl in &self.convert_decls {
            if let Some(ty) = self.convert_target(decl.key(), &registry, Direction::Input) {
                registry = registry.cross(Direction::Input, &ty);
                // The target's conversion chains through this one, and nothing
                // about the target type says so.
                convert_edges.push((
                    (Direction::Input, decl.key().clone()),
                    (Direction::Input, TypeKey::from_type(&ty)),
                ));
            }
            if let Some(ty) = self.convert_target(decl.key(), &registry, Direction::Output) {
                registry = registry.cross(Direction::Output, &ty);
                convert_edges.push((
                    (Direction::Output, decl.key().clone()),
                    (Direction::Output, TypeKey::from_type(&ty)),
                ));
            }
        }
        for (from, on) in convert_edges {
            registry = registry.depends(from, on);
        }
        // How composites cross in pieces. Every one of these reads only the
        // model, which is what lets them be stated here rather than asked for
        // mid-resolve.
        let decompositions = prebindgen_registry::Decompositions {
            expansions: Some(self.build_expansions()),
            deconstructors: Some(self.build_deconstructors(&registry)),
            value_structs: self.build_value_struct_decons(&registry),
            sums: self.build_sum_decons(&registry),
            leaf_vec_elements: self.build_leaf_vec_fold_elements(&registry),
            replaces: self.boundary_only_types(),
        };
        registry = registry.decompose(decompositions);
        Ok(registry)
    }
}

impl Declarations {
    pub(crate) fn build_value_struct_decons(
        &self,
        registry: &impl Conversions<KotlinMeta>,
    ) -> Vec<prebindgen_registry::unfold::ValueDecon> {
        let mut out = Vec::new();
        for item_struct in registry.flat().types().filter_map(|t| match t {
            prebindgen_registry::flat::Type::Struct(s) => Some(s),
            _ => None,
        }) {
            // The declaration's own reading, and its key off that — neither
            // composed from the ident, which an adapter cannot do anyway.
            let reading = item_struct.type_ref();
            let key = reading.key();
            // A `data_class` is a registered type that is neither an opaque
            // handle nor an enum.
            let is_data_class = matches!(
                self.type_kind(registry, &reading.key()),
                TypeKind::DataStruct { cfg: Some(c), .. } if c.name_spec.is_some()
            );
            if !is_data_class {
                continue;
            }
            if let Some(leaves) =
                crate::jni::synth_value_struct_leaves(self, registry, item_struct, &[], "", 0)
            {
                if !leaves.is_empty() {
                    out.push(prebindgen_registry::unfold::ValueDecon {
                        key,
                        source: reading.clone(),
                        leaves,
                    });
                }
            }
        }
        out
    }

    pub(crate) fn build_sum_decons(
        &self,
        registry: &impl Conversions<KotlinMeta>,
    ) -> Vec<prebindgen_registry::unfold::SumDecon> {
        let mut keys: Vec<&TypeKey> = self.types.keys().collect();
        keys.sort_by(|a, b| a.as_str().cmp(b.as_str()));
        let mut out = Vec::new();
        for key in keys {
            let Some(sum_cfg) = self.types[key].sum() else {
                continue;
            };
            // The `sealed_class!` declaration's own IDENTITY. This runs during
            // the declare phase, where a `reading()` would legitimately answer
            // `None` for a type nothing has interned yet — the declaration is
            // the only thing that can say (#291), and `Origin::key` is what it
            // says it with. This took the declaration's node and ran
            // `bare_path_ident` over it to reach the same ident.
            let Some(ident) = self.types[key].rust_type.key().ident() else {
                continue;
            };
            let Some(prebindgen_registry::flat::Type::Variant(sum)) =
                registry.flat().declared_type(&ident)
            else {
                continue;
            };
            out.push(prebindgen_registry::unfold::SumDecon {
                key: key.clone(),
                // The sum's own reading, which the declaration answers with —
                // `Variant::type_ref` exists for exactly this, and it works in
                // the declare phase where a `reading()` lookup could not.
                source: sum.type_ref().clone(),
                leaves: crate::jni::synth_sum_leaves(self, sum_cfg, sum),
            });
        }
        out
    }

    pub(crate) fn build_leaf_vec_fold_elements(
        &self,
        registry: &impl Conversions<KotlinMeta>,
    ) -> Vec<TypeKey> {
        let mut seen = std::collections::HashSet::new();
        let mut out = Vec::new();
        let mut consider = |bare: &prebindgen_registry::flat::TypeRef| {
            if seen.insert(bare.key()) && self.is_leaf_vec_element(bare) {
                out.push(bare.key());
            }
        };
        for f in registry.flat().functions() {
            // `Vec<T>` / `Option<Vec<T>>` return. The model's `ret` already
            // normalizes an elided return to `()`, so there is no arm for it.
            {
                let after_opt = match f.ret.kind() {
                    prebindgen_registry::flat::TypeKind::Optional(inner) => inner,
                    _ => &f.ret,
                };
                if let prebindgen_registry::flat::TypeKind::Vec(elem) = after_opt.kind() {
                    consider(peel_one_borrow(elem));
                }
            }
            // `impl Fn(&[T])` / `impl Fn([T])` callback arg. Over the model's
            // params, whose readings already say which ones ARE callbacks —
            // walking `sig.inputs` re-extracted that from the bounds.
            for p in &f.params {
                let Some(args) = p.ty.callback_args() else {
                    continue;
                };
                for arg in args {
                    if let prebindgen_registry::flat::TypeKind::Slice(elem) =
                        peel_one_borrow(arg).kind()
                    {
                        consider(peel_one_borrow(elem));
                    }
                }
            }
        }
        out
    }
}

/// One `&` off, and nothing else — the model's own `borrow_target` would also
/// see through a `Box`/`Cow`, which `peel_leading_ref` did not.
fn peel_one_borrow(t: &prebindgen_registry::flat::TypeRef) -> &prebindgen_registry::flat::TypeRef {
    match t.kind() {
        prebindgen_registry::flat::TypeKind::Ref { inner, .. } => inner,
        _ => t,
    }
}

/// The declared **fieldless** enum under `name`, or a panic naming the right
/// declarator when it is a sum.
///
/// `enum_class` crosses the boundary as a bare discriminant and has no room for
/// a payload, which is why this is a hard error rather than a fallthrough. It
/// used to be `assert_only_unit_variants`, running `enum_shape` over a
/// `syn::ItemEnum` to work out which of the two shapes it had — the
/// classification the model makes once, at parse time, and expresses as two
/// different elements.
fn flat_unit_enum<'r>(
    registry: &'r impl Conversions<KotlinMeta>,
    name: &syn::Ident,
    declarator: &str,
) -> Option<&'r prebindgen_registry::flat::Enum> {
    match registry.flat().declared_type(name)? {
        prebindgen_registry::flat::Type::Enum(e) => Some(e),
        prebindgen_registry::flat::Type::Variant(v) => {
            let offender = v
                .alternatives
                .iter()
                .find(|a| !a.is_empty())
                .map(|a| a.name.to_string())
                .unwrap_or_default();
            panic!(
                "`{name}` is a data-carrying enum (variant `{offender}` has fields): declare \
                 it with `sealed_class!({name})`, not `{declarator}!({name})` — \
                 `{declarator}` crosses the boundary as a bare discriminant and has no room \
                 for a payload"
            )
        }
        _ => None,
    }
}

impl Declarations {
    fn dispatch_fn_input(
        &self,
        args: &[prebindgen_registry::flat::TypeRef],
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        let outer_ty = build_fn_type(args, emit);
        let (wire, body) = callback_input(self, args, registry, emit)?;
        let niches = default_niches_for_wire(&wire);
        // `impl Fn(...)` crosses the extern tier as the erased lambda object
        // (`Any`) — same as the unfold builder / error-sink params. The typed
        // wrapper-level lambda signature is computed at render time from the
        // arg types' callback plans, not carried in metadata.
        Some(ConverterImpl {
            subs: vec![],
            pre_stages: vec![],
            function: self.build_input_fn_composed(&outer_ty, &wire, &body, None),
            destination: wire,
            niches,
            metadata: self.framework_meta(Some(KtType::any())),
        })
    }
}

impl Prebindgen for Declarations {
    /// Cross-language extras every JNI converter carries — currently
    /// the Kotlin value-context type name. Filled by the rank-N
    /// handlers at the same point they build the wire/body; the
    /// resolver propagates it into [`prebindgen_registry::TypeEntry::metadata`];
    /// the Kotlin emitter reads it back to drive every wrapper /
    /// typed-handle / `JNIWrappers` signature.
    type Metadata = KotlinMeta;

    // ── Structural type resolution ──────────────────────────────────────
    // Try the terminal categories, then the `Result` peel, then the built-in
    // wrapper shapes — peel
    // `ty`'s outermost layer and dispatch to `{input,output}_wrapper_shape` with
    // the reconstructed canonical pattern. `subs` = the captured inner(s).

    /// Member-shape invariants (N5), checked against registry signatures —
    /// the earliest possible moment. Without this, a receiver-less `.method()`
    /// member would silently emit a method that ignores `this`, and a
    /// wrong-return `.constructor()` a factory of the wrong type.
    fn validate(&self, binding: &Building<'_, Self::Metadata>) -> Result<(), String> {
        // Report what this binding left unclaimed. Here because it is the
        // earliest generator-owned hook that sees the model, and it runs
        // exactly where the binding used to print these itself. Moves into
        // `JniGenBuilder::generate` once that exists (prebindgen#251 phase E).
        prebindgen_registry::warn_unclaimed(binding.flat(), &self.claimed());

        for (key, members) in &self.class_members {
            for m in members {
                // A binding-absent fn already hard-errored in the scan.
                //
                // The ELEMENT, not its item: a signature is a parameter list
                // and a return, both already classified, so neither check
                // below walks `syn::FnArg` / `syn::ReturnType` to re-derive
                // what the model states.
                let Some(func) = binding.flat().function(&m.rust_ident) else {
                    continue;
                };
                match m.kind {
                    MemberKind::Method => {
                        let has_receiver =
                            func.params.iter().any(|p| &peel_receiver_key(&p.ty) == key);
                        if !has_receiver {
                            let took: Vec<String> =
                                func.params.iter().map(|p| p.ty.to_string()).collect();
                            return Err(format!(
                                "class `{}` method `{}`: no parameter of type `{}` — an \
                                 instance method's receiver must appear in the signature \
                                 (took: {})",
                                key.as_str(),
                                m.rust_ident,
                                key.as_str(),
                                if took.is_empty() {
                                    "no parameters".to_string()
                                } else {
                                    took.join(", ")
                                }
                            ));
                        }
                    }
                    MemberKind::Constructor => {
                        // Allowed factory shapes: `Self` and `Result<Self, E>`.
                        // The element normalizes an elided return to `Unit`, so
                        // there is no `ReturnType::Default` arm to write, and
                        // `fallible_parts` is `result_ok_type` asked of the
                        // classification instead of of a path.
                        let core = func.ret.fallible_parts().map_or(&func.ret, |(ok, _)| ok);
                        if &peel_receiver_key(core) != key {
                            return Err(format!(
                                "class `{}` constructor `{}`: must return `{}` or \
                                 `Result<{}, E>` — it returns `{}`",
                                key.as_str(),
                                m.rust_ident,
                                key.as_str(),
                                key.as_str(),
                                func.ret
                            ));
                        }
                    }
                }
            }
        }
        // Three sum positions in a declared signature are wrong. Two have no
        // lowering at all and would otherwise fail as
        // "`E` has no output converter", which names the sum rather than the
        // position — actively misleading, because a sum has no whole-value
        // converter BY DESIGN, so that message sends the reader looking for
        // something that must not exist. Reject them here, where the message
        // can say what is actually unsupported and what to write instead.
        for ident in self.declared_functions() {
            // The ELEMENT, not just its syntax: check (3) below asks its params
            // which are callbacks, which is the model's answer, not the tokens'.
            let Some(func) = binding.flat().function(&ident) else {
                continue;
            };
            // (1) A sum in the `Ok` position of a fallible return. A sum is
            // delivered DECOMPOSED through a builder callback, and the
            // `Result` lane has no builder: a `Result` return deliberately
            // keeps its whole-value converter so a fallible factory still
            // yields a handle (see `unfold::returns_type`).
            // Off the model's return, not the item's `sig.output`: the reading
            // says it is fallible and hands over both sides, where re-reading
            // the signature had to find the `Result` in a path first.
            if let Some((ok, _)) = func.ret.fallible_parts() {
                {
                    let core = crate::util::head_type(ok);
                    if matches!(self.type_kind(binding, &core.key()), TypeKind::Sum) {
                        return Err(format!(
                            "fn `{ident}`: `Result<{}, _>` — a sealed_class value is not \
                             supported in the success position of a fallible return. A sum \
                             crosses decomposed (a tag plus one leaf group per variant) \
                             through a builder callback, which the `Result` lane does not \
                             have — a fallible return keeps its whole-value converter so a \
                             factory can still hand back a handle. Return `{}` directly and \
                             report failure through the error channel, or model the failure \
                             as one of the sum's own variants",
                            ok, ok,
                        ));
                    }
                }
            }
            // (2) A sum in the **error** position of a `Result` with no
            // deconstructor declared for it. Unlike the other two this one
            // RESOLVES — it takes the generic undecomposed-`E` path, where the
            // `Err` is routed to the plain binding-error channel as
            // `e.to_string()`. So the author declares a sealed hierarchy and
            // Kotlin silently receives a `String`, and the generated crate
            // quietly acquires an `E: Display` bound that fails downstream in
            // generated code rather than at the declaration. Emitting
            // something misleading is worse than not emitting: say so here.
            //
            // An error plan can only come from a TYPE-level `expand_return!`
            // (auto-applied to every fn with that `E`); a per-fn
            // `.expand_return(...)` always targets the Output position. So the
            // declaration set is the whole story, and this stays a pre-resolve
            // check.
            if let Some((_, err_ty)) = func.ret.fallible_parts() {
                {
                    let core = crate::util::head_type(err_ty);
                    let declared = self
                        .return_expand_decls
                        .iter()
                        .any(|d| *d.key() == err_ty.key());
                    if !declared && matches!(self.type_kind(binding, &core.key()), TypeKind::Sum) {
                        return Err(format!(
                            "fn `{ident}`: `Result<_, {}>` — `{}` is declared `sealed_class!`, \
                             but nothing decomposes it in the error position, so it would be \
                             delivered as `e.to_string()` on the plain binding-error channel \
                             rather than as the sealed hierarchy (and would silently require \
                             `{}: Display`). Declare `expand_return!({})` with the fields to \
                             deliver, so the error crosses through the typed domain-handler \
                             channel; or, if a text message really is what you want, drop the \
                             `sealed_class!` declaration for this type",
                            // `Result<_, E>` and the `expand_return!` key are
                            // the WHOLE error type: the `Display` bound falls
                            // on it (the generated code calls `__e.to_string()`
                            // on the `Err` value), and the auto-apply matches a
                            // deconstructor by that same whole type. Only the
                            // "is declared `sealed_class!`" clause names the
                            // peeled sum, since that is what carries the
                            // declaration. Identical for a bare `E`; they
                            // diverge once it is wrapped.
                            err_ty,
                            core,
                            err_ty,
                            err_ty,
                        ));
                    }
                }
            }
            // (3) A **slice of sums** delivered to a callback
            // (`impl Fn(&[E])`): the element fold would need the sum's
            // folder-appender singleton, which is emitted per `Vec<E>` RETURN
            // position, so the shape resolves to nothing.
            for p in &func.params {
                let Some(args) = p.ty.callback_args() else {
                    continue;
                };
                for arg in args {
                    // The same walk `build_leaf_vec_fold_elements` makes over a
                    // callback argument, off the same helper: one borrow, a
                    // slice, one borrow off its element.
                    let prebindgen_registry::flat::TypeKind::Slice(elem) =
                        peel_one_borrow(arg).kind()
                    else {
                        continue;
                    };
                    let elem = peel_one_borrow(elem);
                    if matches!(self.type_kind(binding, &elem.key()), TypeKind::Sum) {
                        return Err(format!(
                            "fn `{ident}`: `impl Fn(&[{}])` — a slice of a sealed_class value \
                             is not supported as a callback argument. A sum crosses as a tag \
                             plus one leaf group per variant, and folding a *sequence* of \
                             those into the foreign list needs the element folder a `Vec<{}>` \
                             RETURN provides; declare the callback over one value \
                             (`impl Fn({})`) or return `Vec<{}>` instead",
                            elem, elem, elem, elem,
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    /// The post-resolve validation boundary (issue #90): every bound
    /// function's lowered plan must build, and the split declarations must
    /// be unambiguous, before ANY artifact writer touches disk.
    fn validate_resolved(&self, registry: &Registry<KotlinMeta>) -> Result<(), String> {
        validate_bindings(self, registry)
    }

    /// The other-side type of every `convert!` conversion, in the
    /// conversion's direction: an input fn's parameter type (peeled of `&`)
    /// must have its own **input** converter for the composed rank-0 body to
    /// chain through; an output fn's return type needs the **output** twin.
    /// Signatures are read from the registry (missing fns are reported by
    /// the scan's helper-function warning; the body derivation later
    /// hard-errors with the precise decl).
    /// Emit the `OwnedObject<T>` borrow wrapper used by
    /// [`Self::opaque_handle_input`] into the destination file.
    /// The struct is referenced by an unqualified `OwnedObject` from
    /// the same generated file, so no `use` paths leak into the host
    /// crate's source tree.
    fn prerequisites(
        &self,
        registry: &Registry<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Vec<syn::Item> {
        // `__JniErr` is the **framework** error type alias — always the
        // `JniBindingError` String-wrapper. Built-in converter bodies compose
        // their `?` failures into this type via its `From<String>` impl. A
        // `Result<T, E>` return instead binds its own raw `E`; both funnel to
        // the per-call `signal_error` sink (generic over `Display`).
        let error_type = framework_error_type();
        let alias: syn::Item = syn::parse_quote!(
            #[allow(dead_code)]
            pub(crate) type __JniErr = #error_type;
        );
        let mut items = vec![alias];
        items.extend(owned_object_prerequisite_items());
        // The two error-channel fns the extern bodies call: `signal_binding_error`
        // (binding/system failure → `JniErrorHandler`) and `signal_domain_error`
        // (a fallible fn's `Err(E)` → the typed `<Src>Handler`). Emitted above the
        // converters so wrapper code references them by bare name; the binding
        // crate reaches them as `<include_module>::signal_*` from outside the file.
        items.push(build_signal_binding_error_item());
        items.push(build_signal_domain_error_item());
        let _ = registry;
        // Handle destructors — one `extern "C" freePtr<suffix>` per
        // non-suppressed opaque handle (the Rust half of the typed-handle
        // `free()` pair the Kotlin emitter generates).
        items.extend(build_handle_destructor_items(self, registry, emit));
        // Slice/Vec input helpers — a `…VecNew/Push/Free` trio per flattenable
        // element type a scanned `&[T]`/`Vec<T>` param takes. Kotlin builds the
        // Rust-side `Vec` by pushing each element's decoupled leaves, then passes
        // the handle (see `ParamMode::VecBuild`), avoiding per-element
        // `env.get_field(...)` upcalls on the Rust side.
        items.extend(build_vec_build_helper_items(self, registry, emit));
        // Expression constants — one nullary JNI getter extern per
        // `PackageDecl::constant_expr`, its value the binding-defined
        // expression evaluated with a glob import of every source module (so
        // it composes the source crate's items without qualification). The
        // getter reuses the whole function-wrapper pipeline via the
        // synthetic signature, exactly like a const-backed getter.
        let mut glob_modules = registry.all_source_modules();
        if glob_modules.is_empty() {
            glob_modules.push(self.default_module(registry));
        }
        for decl in self.packages.values().flat_map(|p| &p.constant_exprs) {
            validate_constant_expr(self, &decl.kotlin_name, &decl.ty);
            let getter = const_expr_getter_fn(&decl.kotlin_name, &decl.ty, registry);
            let expr = &decl.expr;
            let callee: syn::Expr = syn::parse_quote!({
                #(
                    #[allow(unused_imports)]
                    use #glob_modules::*;
                )*
                #expr
            });
            let wrapper =
                emit_jni_function_wrapper_with_callee(self, &getter, registry, Some(callee), emit);
            items.push(syn::parse2::<syn::Item>(wrapper).expect(
                "constant_expr: generated getter wrapper is a single item by construction",
            ));
        }
        items
    }

    fn post_process_item(
        &self,
        item: &mut syn::Item,
        registry: &Registry<KotlinMeta>,
        _emit: &prebindgen_registry::Emit,
    ) {
        self.qualify_item(item, registry);
    }

    // ── Item methods ─────────────────────────────────────────────────

    fn on_function(
        &self,
        f: &prebindgen_registry::flat::Function,
        registry: &Registry<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> TokenStream {
        emit_jni_function_wrapper(self, f, registry, emit)
    }

    fn on_struct(
        &self,
        _s: &prebindgen_registry::flat::Struct,
        _registry: &Registry<KotlinMeta>,
        _emit: &prebindgen_registry::Emit,
    ) -> TokenStream {
        // Struct converter bodies are emitted by the resolver via
        // input_terminal / output_terminal below; no separate
        // per-struct item is needed.
        TokenStream::new()
    }

    fn on_variant(
        &self,
        _v: &prebindgen_registry::flat::Variant,
        _registry: &Registry<KotlinMeta>,
        _emit: &prebindgen_registry::Emit,
    ) -> TokenStream {
        TokenStream::new()
    }

    fn on_enum(
        &self,
        _e: &prebindgen_registry::flat::Enum,
        _registry: &Registry<KotlinMeta>,
        _emit: &prebindgen_registry::Emit,
    ) -> TokenStream {
        TokenStream::new()
    }

    /// Declared consts only reach here (undeclared ones are gated out before
    /// emission): re-emit the const as a path-alias
    /// to its source-of-truth (initializer tokens are never copied — they
    /// may reference source-crate internals) AND emit its nullary JNI getter
    /// extern. The getter reuses the whole function-wrapper pipeline (so the
    /// const's type flows through the ordinary output-converter machinery);
    /// only the callee expression differs — a path to the const, not a call.
    fn on_const(
        &self,
        c: &prebindgen_registry::flat::Constant,
        registry: &Registry<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> TokenStream {
        reject_handle_const(self, c);
        let getter = const_getter_fn(c);
        let const_ident = &c.name;
        let source_module = self.fn_module(registry, const_ident);
        let callee: syn::Expr = syn::parse_quote!(#source_module::#const_ident);
        let wrapper =
            emit_jni_function_wrapper_with_callee(self, &getter, registry, Some(callee), emit);
        let alias = emit.const_alias(c, &source_module);
        quote! {
            #alias
            #wrapper
        }
    }
}

/// Structural converter builders — the rank-0 terminal chains and the rank-1
/// wrapper-shape handlers, now inherent helpers called by the structural
/// [`Prebindgen::on_input_type`] / [`Prebindgen::on_output_type`].
impl Declarations {
    // ── Input converters ─────────────────────────────────────────────

    /// Whole-type **input** terminal categories (opaque handle, enum,
    /// `convert!`, `str`, primitive, struct) — depends on nothing, `subs`
    /// empty.
    pub(crate) fn input_terminal(
        &self,
        reading: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // Classify off `kind`, spell with `spell()`: the arms below that ask what
        // a type IS use `reading`, and everything that has to name it in
        // generated Rust uses this.
        // Everything below reads the reading: the identity for a lookup, the
        // spelling for what generated Rust says. Neither needs a node.
        // Structured-config overrides first (opaque handles, then user-
        // registered rank-0 wrappers, then built-ins).
        let key = reading.key();
        if let Some(cfg) = self.types.get(&key) {
            if cfg.is_opaque() {
                return Some(self.opaque_handle_input(reading, emit));
            }
        }
        // Fixed-size array of JNI primitives — dual of the output branch.
        // The `try_into` IS the length check: a JVM array of the wrong size
        // becomes a binding error naming the type, never a panic.
        if let Some(spec) = crate::jni::prim_array::prim_array_of(reading) {
            let body = crate::jni::prim_array::input_body(reading, &spec, emit);
            let wire = spec.wire.clone();
            let kotlin_name = self.override_kotlin_name(&reading.key(), Some(spec.kotlin.clone()));
            let niches = default_niches_for_wire(&wire);
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_input_fn_of(reading, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata: self.framework_meta(kotlin_name),
            });
        }
        // `enum_class`-declared enums: jint wire, `TryFrom<i32>` decode.
        // Registered before the user-wrapper lookup so a stray
        // `input_wrapper` registration on the same key would have to be
        // intentional. The rank-0 enum arm produces a terminal converter
        // (jint → Rust enum) with the configured Kotlin FQN in metadata.
        if let Some(cfg) = self.types.get(&key) {
            if cfg.is_enum_class() {
                if let Some(name) = reading.key().ident() {
                    // The ELEMENT, and the match is the check: a declared
                    // `enum_class!` over a data-carrying enum is a `Variant`,
                    // not an `Enum`, so the shape assertion the two bodies used
                    // to run is the kind the model already decided.
                    if let Some(e) = flat_unit_enum(registry, &name, "enum_class") {
                        let (wire, body) = enum_input_body(self, registry, e);
                        let niches = default_niches_for_wire(&wire);
                        let kotlin_name =
                            cfg.name_spec.as_ref().map(|s| KtType::cls(self.fqn_of(s)));
                        return Some(ConverterImpl {
                            subs: vec![],
                            pre_stages: vec![],
                            function: self.build_input_fn_of(reading, &wire, &body, None, emit),
                            destination: wire,
                            niches,
                            metadata: self.framework_meta(kotlin_name),
                        });
                    }
                }
            }
        }
        if let Some(conv) = self.lookup_input(reading, registry, emit) {
            return Some(conv);
        }
        // `str` is unsized, so converters can't return it directly.
        // Still register a rank-0 entry to satisfy resolution for
        // borrowed `&str` parameters: decode `JString` to owned `String`
        // and let call sites borrow as needed.
        if reading.key().as_str() == "str" {
            let wire: syn::Type = syn::parse_quote!(jni::objects::JString);
            let body: syn::Expr = syn::parse_quote!({
                let s = env.get_string(v).map_err(|e| {
                    <__JniErr as ::core::convert::From<String>>::from(format!(
                        "decode_string: {}",
                        e
                    ))
                })?;
                s.into()
            });
            // The unsized `str` yields an OWNED `String` the call site borrows,
            // so this converter's Rust type is not the reading's spelling.
            let rust_ty: syn::Type = syn::parse_quote!(String);
            let kotlin_name = self.override_kotlin_name(&reading.key(), Some(KtType::string()));
            let niches = default_niches_for_wire(&wire);
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_input_fn_composed(&rust_ty, &wire, &body, None),
                destination: wire,
                niches,
                metadata: self.framework_meta(kotlin_name),
            });
        }
        // Any OWNED string, however Rust spells it — `String`, `Box<String>`,
        // `Cow<'_, str>`. The model classifies each of them `Str`; the spelling is
        // the source's business, and `.into()` constructs it from the decoded
        // `String`. This used to be one hardcoded `TypeKey == "Box < String >"`
        // arm, which is what a spelling-keyed converter table costs: one
        // hand-written case per representation anyone happened to write (#270).
        //
        // `str` is handled above, separately and deliberately: it is unsized,
        // so its converter yields an owned `String` the call site borrows —
        // a different contract, not a different spelling.
        if matches!(
            reading.unwrapped().kind(),
            prebindgen_registry::flat::TypeKind::Str | prebindgen_registry::flat::TypeKind::String
        ) {
            let wire: syn::Type = syn::parse_quote!(jni::objects::JString);
            let body: syn::Expr = syn::parse_quote!({
                let s = env.get_string(v).map_err(|e| {
                    <__JniErr as ::core::convert::From<String>>::from(format!(
                        "decode_string: {}",
                        e
                    ))
                })?;
                // The canonical value, then the spelling.
                ::std::string::String::from(s).into()
            });
            let kotlin_name = self.override_kotlin_name(&reading.key(), Some(KtType::string()));
            let niches = default_niches_for_wire(&wire);
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_input_fn_of(reading, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata: self.framework_meta(kotlin_name),
            });
        }
        if let Some((wire, body)) = primitive_input(&reading.key()) {
            let niches = default_niches_for_wire(&wire);
            let kotlin_name = kotlin_for_wire(&wire);
            let metadata = if reading.key().as_str() == "u64" {
                self.unsigned64_leaf_meta()
            } else {
                self.framework_meta(kotlin_name)
            };
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_input_fn_of(reading, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata,
            });
        }
        if let Some(name) = reading.key().ident() {
            // A `sealed_class` sum reached as a whole `JObject` — a field of a
            // data class, or any position where the parent is already an
            // object. Its own converter, so the parent's generic field branch
            // delegates exactly as it does for a nested data class. (The
            // OUTPUT direction has no counterpart: a sum crosses Rust →
            // Kotlin flattened, always.)
            if self.types.get(&key).is_some_and(|c| c.sum().is_some()) {
                if let Some(prebindgen_registry::flat::Type::Variant(v)) =
                    registry.flat().declared_type(&name)
                {
                    let (wire, body) = sum_input_body(self, v, registry, emit)?;
                    // The wire's own null niche, exactly as a data class gets
                    // — that is what lets `Option<sum>` fold with JVM null as
                    // `None` instead of needing a boxed wrapper.
                    let niches = default_niches_for_wire(&wire);
                    let kotlin_name = self
                        .types
                        .get(&key)
                        .and_then(|c| c.name_spec.as_ref())
                        .map(|s| KtType::cls(self.fqn_of(s)));
                    return Some(ConverterImpl {
                        subs: vec![],
                        pre_stages: vec![],
                        function: self.build_input_fn_of(reading, &wire, &body, None, emit),
                        destination: wire,
                        niches,
                        metadata: self.framework_meta(kotlin_name),
                    });
                }
            }
            if let Some(s) = registry.flat().struct_type(&name) {
                let (wire, body) = struct_input_body(self, s, registry, emit)?;
                let niches = default_niches_for_wire(&wire);
                // Auto-generated struct: the value-context Kotlin name is
                // whatever the user pinned via `data_class`. If
                // they didn't, leave `kotlin_name = None` — emitter
                // surfaces this as a build-time hard error.
                let kotlin_name = self
                    .types
                    .get(&key)
                    .and_then(|c| c.name_spec.as_ref())
                    .map(|s| KtType::cls(self.fqn_of(s)));
                return Some(ConverterImpl {
                    subs: vec![],
                    pre_stages: vec![],
                    function: self.build_input_fn_of(reading, &wire, &body, None, emit),
                    destination: wire,
                    niches,
                    metadata: self.framework_meta(kotlin_name),
                });
            }
            // Bare-ident enum: leave to the consuming crate to override
            // (today's CongestionControl etc. fall here — caller's wrapper
            // ext returns Some in its own input_terminal).
        }
        None
    }

    /// The **outbound** half of [`Self::input_transparent_bridge`], and the same
    /// last resort: a spelling whose only difference from something this adapter
    /// can already convert is the transparent wrappers over it.
    ///
    /// It had no twin, so an erased wrapper resolved inbound and not outbound —
    /// `Box<Priority>` was a parameter this binding could take and a return it
    /// could not give, for a wrapper the model exists to make invisible (#309).
    /// The one arm covers `Box<Handle>`, `Box<enum>` and `Box<DataClass>` alike,
    /// because [`Self::output_terminal`] misses all three the same way: it keys
    /// on the SPELLING, and no config sits under `Box < Priority >`.
    ///
    /// The wrappers come **off** here rather than going on, which is the whole
    /// difference:
    ///
    /// ```text
    /// input : let __inner = <inner>(env, v)?;      build_through_erased_wrappers(__inner)
    /// output: let __inner = read_through(v);       <inner>(env, __inner)
    /// ```
    ///
    /// Everything else is direction-independent — `subs`, `destination`,
    /// `niches`, `metadata` all mean the same thing either way, and inheriting
    /// the inner's metadata is what keeps `Box<Priority>` presenting as the
    /// Kotlin enum class instead of losing it behind the wrapper.
    pub(crate) fn output_transparent_bridge(
        &self,
        reading: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        if reading.erased_wrappers().is_empty() {
            return None;
        }
        // The identity under every wrapper. This spelled the stripped type into
        // a node purely so `reading_of` could key off it and `subs` could key
        // off it again — `stripped_key` is that key, and the model already
        // holds it.
        let stripped = reading.stripped_key();
        // A wrapper over a **borrow** is refused here too, and outbound the
        // reason is its own: a borrow's output route is the clone-into-a-fresh-
        // handle arm, which hands back a wire built from a reference — there is
        // no owned value to read the wrapper off. Inbound the same guard is
        // about `E0106`; the shapes coincide, the reasons do not.
        //
        // Asked of the MODEL: an erasure is transparent, so `Box<&T>` already
        // classifies as `Ref` and nothing here matches a `syn` variant.
        if reading.borrow_target().is_some() {
            return None;
        }
        // It has to be a type this binding already crosses; if it is not, the
        // ordinary "unresolved" diagnostic names it, which is the better error.
        let inner = registry.reading(&stripped)?;
        let entry = registry.output_entry(&inner)?;
        let wire = entry.destination.clone();
        // Take the wrappers off what the caller handed us. `None` is `Cow`'s
        // policy refusal — the crossing then stays unresolved and names the
        // type, rather than resolving and emitting Rust the consumer cannot
        // build.
        let read = read_through_erased_wrappers(reading, quote!(v))?;
        // The inner's COMPLETE chain, stages included: a `convert!` type reaches
        // its wire through them.
        let inner_call = crate::jni::emit::composed_inner_output(entry, quote!(__inner));
        let body: syn::Expr = syn::parse_quote!({
            let __inner = #read;
            #inner_call
        });
        Some(ConverterImpl {
            subs: vec![stripped],
            pre_stages: vec![],
            function: self.build_output_fn_of(reading, &wire, &body, None, emit),
            destination: wire,
            niches: entry.niches.clone(),
            // The surface is the inner type's — a wrapper is invisible to the
            // destination language, which is why the model erases it.
            metadata: entry.metadata.clone(),
        })
    }

    /// **Last resort**: a spelling whose only difference from something this
    /// adapter can already convert is the transparent wrappers over it.
    ///
    /// The layer arms each handle one *classification* layer — `Optional`,
    /// `Sequence`, `Ref` — and bridge a wrapper as part of doing so. What none of
    /// them covers is a wrapper over a **terminal**: `Box<Payload>` classifies as
    /// `Named`, so no layer arm claims it, and `input_terminal` keys on the whole
    /// spelling and finds no `Payload` config under `Box < Payload >`. Before
    /// this it resolved to nothing at all — the crossing was refused for a
    /// wrapper the model exists to make invisible.
    ///
    /// So this delegates to the **stripped** spelling's own converter and puts
    /// the wrappers back on what it produced. The inner type is declared as a
    /// `sub`, exactly as a layer arm declares its inner, so it is required and
    /// resolved through the ordinary machinery rather than being resolved here.
    ///
    /// Deliberately tried **after** every layer arm, so nothing that resolves
    /// today changes route: `Box<Option<T>>` keeps the `Optional` arm (which
    /// bridges via `build_from_canonical`), and only the shapes that previously
    /// reached `None` arrive here.
    pub(crate) fn input_transparent_bridge(
        &self,
        reading: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        if reading.erased_wrappers().is_empty() {
            return None;
        }
        // The identity under every wrapper — by the model's own definition, the
        // key of the type whose lowering yields this `kind`. See the outbound
        // peer for why this is a key rather than a spelling.
        let stripped = reading.stripped_key();
        // A wrapper over a **borrow** is not bridgeable here, and the reason is
        // the converter's own shape rather than the wrapper's: this produces an
        // owned value, and there is nothing for a `Box<&T>` to borrow *from* —
        // the returned reference would have to outlive the call that made it
        // (`E0106` on the generated signature). The borrow arms own that case,
        // and they serve the canonical spelling only.
        //
        // Asked of the MODEL, not of `stripped`: an erasure is transparent, so
        // `Box<&T>` already classifies as `Ref` and `kind` answers this without
        // anything here matching a `syn` variant.
        if reading.borrow_target().is_some() {
            return None;
        }
        // It has to be a type this binding already crosses; if it is not, the
        // ordinary "unresolved" diagnostic names it, which is the better error.
        let inner = registry.reading(&stripped)?;
        let entry = registry.input_entry(&inner)?;
        let wire = entry.destination.clone();
        // Wrap what the inner converter produced. `None` here is `Cow`'s policy
        // refusal — the crossing then stays unresolved and names the type,
        // rather than resolving and emitting Rust the consumer cannot build.
        let built = build_through_erased_wrappers(reading, quote!(__inner))?;
        // The inner's COMPLETE chain, stages included. This called
        // `entry.function` directly and left `pre_stages` empty, which SKIPPED
        // them: a `convert!`-declared type reaches its Rust value through those
        // stages (`jlong -> u64 -> Duration`), so a `Box` over one arrived
        // un-staged. Every other composing arm goes through this helper for
        // exactly that reason (#309).
        let inner_call = crate::jni::emit::composed_inner_input(entry, quote!(v));
        let body: syn::Expr = syn::parse_quote!({
            let __inner = #inner_call;
            #built
        });
        Some(ConverterImpl {
            subs: vec![stripped],
            pre_stages: vec![],
            function: self.build_input_fn_of(reading, &wire, &body, None, emit),
            destination: wire,
            niches: entry.niches.clone(),
            // The surface is the inner type's: a wrapper is invisible to the
            // destination language, which is the whole reason the model erases
            // it. Inheriting rather than recomputing also keeps a projection's
            // Kotlin class from being lost behind the wrapper.
            metadata: entry.metadata.clone(),
        })
    }

    /// **Input** wrapper shape (`pat` = the reconstructed canonical pattern,
    /// `t1` = its captured inner): the built-in `&`/`Option<&>`/`Vec`/`Option`
    /// handlers. The dual of [`Self::output_wrapper_shape`], whose own doc has
    /// said so all along — this had been stranded above a different function
    /// since the transparent bridge was inserted between them (#294), and
    /// adding the outbound bridge moved it onto an OUTPUT converter, where it
    /// read as an outright contradiction.
    pub(crate) fn input_wrapper_shape(
        &self,
        shape: WrapperShape,
        produced: &Produced<'_>,
        t1: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // Disjoint shapes (see [`WrapperShape`]), tried in priority order. The
        // borrow/option-ref/vec shapes are mutually exclusive; the two
        // `Optional` sub-cases share a method.
        self.input_borrow(shape, produced, t1, registry)
            .or_else(|| self.input_option_ref(shape, produced, t1, registry, emit))
            .or_else(|| self.input_vec(shape, produced, t1, registry, emit))
            .or_else(|| self.input_option(shape, produced, t1, registry, emit))
    }

    // ── Output converters ────────────────────────────────────────────

    /// Whole-type **output** terminal categories (the dual of
    /// [`Self::input_terminal`]: opaque handle, enum, user table,
    /// `str`, `Cow<[u8]>`, unit, primitive, struct) — `subs` empty.
    pub(crate) fn output_terminal(
        &self,
        reading: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // Classify off `kind`, spell with `spell()` — see `input_terminal`.
        // Everything below reads the reading: the identity for a lookup, the
        // spelling for what generated Rust says. Neither needs a node.
        // Structured-config overrides first (opaque handles, then built-ins).
        let key = reading.key();
        if let Some(cfg) = self.types.get(&key) {
            if cfg.is_opaque() {
                return Some(self.opaque_handle_output(reading, emit));
            }
        }
        // Fixed-size array of JNI primitives: `[u8; N]` -> `ByteArray`,
        // `[i64; N]` -> `LongArray`, ... Bulk-copied, nothing boxed. See
        // [`prim_array`]; this replaced the raw-memory value blob.
        if let Some(spec) = crate::jni::prim_array::prim_array_of(reading) {
            let body = crate::jni::prim_array::output_body(&spec);
            let wire = spec.wire.clone();
            let kotlin_name = self.override_kotlin_name(&reading.key(), Some(spec.kotlin.clone()));
            let niches = default_niches_for_wire(&wire);
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_output_fn_of(reading, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata: self.framework_meta(kotlin_name),
            });
        }
        // `enum_class`-declared enums: jint wire, `as jni::sys::jint`
        // encode. Symmetric to the input arm above; relies on
        // `#[repr(i32)]` (or any repr that supports the cast) on the
        // declared enum so the discriminant value round-trips identically.
        if let Some(cfg) = self.types.get(&key) {
            if cfg.is_enum_class() {
                if let Some(name) = reading.key().ident() {
                    if let Some(e) = flat_unit_enum(registry, &name, "enum_class") {
                        let (wire, body) = enum_output_body(self, e);
                        let niches = default_niches_for_wire(&wire);
                        let kotlin_name =
                            cfg.name_spec.as_ref().map(|s| KtType::cls(self.fqn_of(s)));
                        return Some(ConverterImpl {
                            subs: vec![],
                            pre_stages: vec![],
                            function: self.build_output_fn_of(reading, &wire, &body, None, emit),
                            destination: wire,
                            niches,
                            metadata: self.framework_meta(kotlin_name),
                        });
                    }
                }
            }
        }
        if let Some(conv) = self.lookup_output(reading, registry, emit) {
            return Some(conv);
        }
        // `str` is unsized, so it has no by-value output converter — but it is
        // reached as the sub of a `&str` reference accessor leaf. Resolve it to
        // the same `&str → jstring` fn the rank-1 `&str` arm uses (deduped by
        // name) so required-propagation doesn't flag it unresolved.
        if reading.key().as_str() == "str" {
            return Some(self.str_ref_output());
        }
        // An owned string in any representation the model erases — `Box<String>`,
        // `Cow<'_, str>`. It classifies each of them `Str`, and the body is
        // representation-agnostic: `&*v` reaches through any of them by `Deref`
        // to something `new_string` accepts (`&String` through a `Box`, `&str`
        // through a `Cow`). Only the *dispatch* was spelling-keyed, as one
        // hardcoded `TypeKey == "Box < String >"` arm (#270).
        //
        // Plain `String` keeps its own earlier arm in `primitive_output`, whose
        // body this matches exactly; this one is reached for the wrapped
        // spellings that arm's key cannot name.
        if matches!(
            reading.unwrapped().kind(),
            prebindgen_registry::flat::TypeKind::Str | prebindgen_registry::flat::TypeKind::String
        ) {
            let wire: syn::Type = syn::parse_quote!(jni::objects::JString);
            let body: syn::Expr = syn::parse_quote!({
                env.new_string(&*v).map_err(|e| {
                    <__JniErr as ::core::convert::From<String>>::from(format!("encode_str: {}", e))
                })?
            });
            let kotlin_name = self.override_kotlin_name(&reading.key(), Some(KtType::string()));
            let niches = default_niches_for_wire(&wire);
            // A `Cow` accessor may spell its own path any way it likes, but the
            // generated fn's param type has to resolve with no imports in the
            // consumer crate — normalize it, exactly as the `Cow<'_, [u8]>` arm
            // below does. Every other spelling here (`String`, `Box<String>`)
            // is already prelude-resolvable, so it keeps its own.
            let function = match reading.kind() {
                prebindgen_registry::flat::TypeKind::Cow { .. } => {
                    let norm: syn::Type = syn::parse_quote!(::std::borrow::Cow<'_, str>);
                    self.build_output_fn(&norm, &wire, &body, None)
                }
                _ => self.build_output_fn_of(reading, &wire, &body, None, emit),
            };
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function,
                destination: wire,
                niches,
                metadata: self.framework_meta(kotlin_name),
            });
        }
        // `Cow<'_, [u8]>` (any lifetime): a borrow-or-owned byte container —
        // one copy into the JVM array straight off the `Deref<[u8]>`, no
        // intermediate owned `Vec` (the zero-copy dual of the `Vec<u8>`
        // output, for accessors like `zenoh::ZBytes::to_bytes()` that borrow
        // when the payload is contiguous). Surfaces as Kotlin `ByteArray`.
        if let Some(conv) = self.cow_bytes_output(reading) {
            return Some(conv);
        }
        // `()` — identity converter so `fn foo()` and `fn foo() -> ()`
        // funnel through the same uniform output path as everything else.
        // Wire is `()`. Body just returns `v`. No Kotlin name — Unit
        // returns are dropped from emitted signatures, so metadata stays
        // empty.
        if matches!(
            reading.unwrapped().kind(),
            prebindgen_registry::flat::TypeKind::Unit
        ) {
            let wire: syn::Type = syn::parse_quote!(());
            let body: syn::Expr = syn::parse_quote!(v);
            return Some(ConverterImpl {
                subs: vec![],
                function: self.build_output_fn_of(reading, &wire, &body, None, emit),
                destination: wire,
                pre_stages: vec![],
                niches: Niches::empty(),
                metadata: KotlinMeta::default(),
            });
        }
        if let Some((wire, body)) = primitive_output(&reading.key()) {
            let niches = default_niches_for_wire(&wire);
            let kotlin_name = kotlin_for_wire(&wire);
            let metadata = if reading.key().as_str() == "u64" {
                self.unsigned64_leaf_meta()
            } else {
                self.framework_meta(kotlin_name)
            };
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_output_fn_of(reading, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata,
            });
        }
        if let Some(name) = reading.key().ident() {
            if let Some(s) = registry.flat().struct_type(&name) {
                let (wire, body) = struct_output_body(self, s, registry)?;
                let niches = default_niches_for_wire(&wire);
                let kotlin_name = self
                    .types
                    .get(&key)
                    .and_then(|c| c.name_spec.as_ref())
                    .map(|s| KtType::cls(self.fqn_of(s)));
                return Some(ConverterImpl {
                    subs: vec![],
                    pre_stages: vec![],
                    function: self.build_output_fn_of(reading, &wire, &body, None, emit),
                    destination: wire,
                    niches,
                    metadata: self.framework_meta(kotlin_name),
                });
            }
        }
        None
    }

    /// **Output** wrapper shape (the dual of [`Self::input_wrapper_shape`]):
    /// the built-in `&Handle`/`&str`/`Option`/`Vec` handlers. An
    /// `Option<&Handle>` resolves via the shallow `Option<_>`.
    pub(crate) fn output_wrapper_shape(
        &self,
        shape: WrapperShape,
        produced: &Produced<'_>,
        t1: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        // `t1`'s spelling, for the parts that ask spelling questions — the
        // canonical form a produced spelling is compared against, and the
        // type ascriptions the generated body writes. Everything else takes
        // the READING itself (#284).
        let t1_ty = emit.spell(t1);
        // Borrowed opaque-handle output (`&T` / `&'static T` where `T` is a
        // declared opaque handle). Canonical zenoh-flat's `z_*` accessors
        // return *borrowed* handles for the C tier's zero-copy borrows, but
        // the JVM keeps its handle past the call — so the only sound lowering
        // is to clone the referent into a fresh owned `Box`-handle (every such
        // zenoh handle type is `Clone`). This mirrors `opaque_handle_output`
        // with a `.clone()`; `Option<&T>` then composes through the `Option`
        // arm below (it looks up this `&T` entry as its inner). Matched
        // structurally so the lifetime variant `&'static _` is covered too.
        if let Some((_, false)) = produced.borrow() {
            if self.types.get(&t1.key()).is_some_and(|c| c.is_opaque()) {
                let wire: syn::Type = syn::parse_quote!(jni::sys::jlong);
                let body: syn::Expr = syn::parse_quote!(std::boxed::Box::into_raw(
                    std::boxed::Box::new(v.clone())
                ) as i64);
                return Some(ConverterImpl {
                    subs: vec![],
                    function: self.build_output_fn_produced(produced, &wire, &body, None, emit),
                    destination: wire,
                    pre_stages: vec![],
                    niches: Niches::one(syn::parse_quote!(0i64), syn::parse_quote!(*v == 0)),
                    metadata: self.opaque_leaf_meta(t1.key()),
                });
            }
        }
        // Borrowed string slice output (`&str` / `&'a str`): the converter used
        // for a zero-copy reference accessor return (`f(&T) -> &str`, output
        // expansion). The single copy into the JVM is `&str → jstring` (no
        // intermediate owned `String`). The unsized `str` sub resolves via the
        // rank-0 arm to the same fn (see [`Self::str_ref_output`]).
        if let Some((_, false)) = produced.borrow() {
            if t1.key().as_str() == "str" {
                return Some(self.str_ref_output());
            }
        }
        // `Result<T, E>` is peeled by the selector, off the model's
        // `TypeKind::Fallible`. Bindings declare the `Err` type via
        // `.throwable()`.
        if shape == WrapperShape::Optional {
            let outer_ty = produced.key();
            let canonical: syn::Type = syn::parse_quote!(Option<#t1_ty>);
            // Bridgeable first: an unsupported representation must not resolve
            // and then emit code the consumer cannot compile.
            let read = read_as_canonical(produced)?;
            let (wire, inner_body, niches) = option_output(t1, registry)?;
            let body: syn::Expr = syn::parse_quote!({
                let v: #canonical = #read;
                #inner_body
            });
            let inherited = registry
                .output_entry(t1)
                .and_then(|e| e.metadata.kotlin_name.clone());
            let kotlin_name = self.override_kotlin_name(&outer_ty, inherited);
            // Fold a Nullable layer over the inner projection (if any). The
            // kind reflects which path `option_output` took (see
            // [`nullable_kind_for`]): niche-fulfilled keeps the inner wire
            // and treats the slot value as `None`; boxed widens to `JObject`
            // and uses JVM null.
            let nullable_kind = nullable_kind_for_output(&wire, t1, registry);
            let projection = registry
                .output_entry(t1)
                .and_then(|e| e.metadata.projection.clone())
                .map(|h| Projection {
                    strategy: FoldStrategy::Optional(nullable_kind, Box::new(h.strategy)),
                    ..h
                });
            // A **non-projection** `Option<T>` return (`Option<String>`,
            // `Option<i64>`, …) surfaces directly as a nullable Kotlin type, so
            // its value-context name carries the `?`. Projection options get the
            // `?` from `handle_kt_type(Nullable …)` at the use site instead,
            // so leave those untouched here.
            let kotlin_name = if projection.is_none() {
                kotlin_name.map(|n| if n.is_nullable() { n } else { n.nullable() })
            } else {
                kotlin_name
            };
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_output_fn_produced(produced, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata: KotlinMeta {
                    projection,
                    ..self.framework_meta(kotlin_name)
                },
            });
        }
        // `Vec<T>` (output side): encode as a `java.util.ArrayList<InnerWire>`.
        // Symmetric to the input handler. `Vec<u8>` is special-cased at
        // rank-0 (primitive_output → JByteArray) so rank-1 never sees it.
        if shape == WrapperShape::Sequence {
            let inner = registry.output_entry(t1)?;
            // `Vec<opaque-handle>` output is delivered by the Kotlin-side leaf
            // fold (`apply_leaf_vec_folds` → typed-handle wrap), so this
            // whole-`ArrayList` converter is bypassed for it. A handle's `jlong`
            // wire isn't JObject-shaped, so it returns `None` below; the
            // fold-covered return is de-required, so the `None` is not an error.
            let inner_wire = inner.destination.clone();
            if !is_jobject_shaped_wire(&inner_wire) {
                return None;
            }
            // The element's COMPLETE Rust -> wire chain (see the input peer).
            let inner_conv = crate::jni::emit::composed_inner_output(inner, quote::quote!(__elem));
            let outer_ty = produced.key();
            let canonical: syn::Type = syn::parse_quote!(Vec<#t1_ty>);
            let read = read_as_canonical(produced)?;
            let wire: syn::Type = syn::parse_quote!(jni::objects::JObject);
            let body: syn::Expr = syn::parse_quote!({
                let v: #canonical = #read;
                let __list_obj = env
                    .new_object("java/util/ArrayList", "()V", &[])
                    .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("Vec<_>: new ArrayList: {}", e)))?;
                let __list = jni::objects::JList::from_env(env, &__list_obj)
                    .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("Vec<_>: list-from-env: {}", e)))?;
                for __elem in v.into_iter() {
                    let __elem_wire = #inner_conv;
                    let __elem_obj: jni::objects::JObject = __elem_wire.into();
                    __list.add(env, &__elem_obj)
                        .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("Vec<_>: list-add: {}", e)))?;
                }
                __list_obj
            });
            let inner_kotlin = inner.metadata.kotlin_name.clone()?;
            let kotlin_name = self.override_kotlin_name(
                &outer_ty,
                // `List` is auto-imported in Kotlin (default imports). When
                // the inner carries a projection, this wire-context name
                // still drives non-projection consumers; projection-aware
                // sites (classify_return, data-class fields) prefer
                // `projection` and render the typed `List<TypedShort>`
                // instead.
                Some(KtType::generic("List", [inner_kotlin])),
            );
            // Fold an Iterable layer over the inner projection (if any), so
            // `Vec<Handle>` carries the full strategy.
            let projection = inner.metadata.projection.clone().map(|h| Projection {
                strategy: FoldStrategy::Iterable(Box::new(h.strategy)),
                ..h
            });
            // The list conversion always builds a fresh non-null `ArrayList`, so
            // `JObject` null is a free niche — lets `Option<Vec<T>>` ride it
            // (None ⇒ null list) instead of needing a boxed wrapper.
            let niches = default_niches_for_wire(&wire);
            return Some(ConverterImpl {
                subs: vec![],
                pre_stages: vec![],
                function: self.build_output_fn_produced(produced, &wire, &body, None, emit),
                destination: wire,
                niches,
                metadata: KotlinMeta {
                    kotlin_name,
                    value_rust_type: None,
                    projection,
                },
            });
        }
        None
    }

    /// `&[T]` borrowed-slice output (used for a **callback argument** that crosses
    /// native→JVM, e.g. `impl Fn(&[Payload])`). The borrowed dual of the `Vec<T>`
    /// output handler above: build a `java.util.ArrayList<InnerWire>` by iterating
    /// the slice **by reference** and cloning each element through its output
    /// converter (`v.iter()` + `Clone::clone` instead of `into_iter()`). Surfaces
    /// as Kotlin `List<T>`. The element must have a JObject-shaped output wire
    /// (struct / String / …) — scalar slices are not handled here.
    pub(crate) fn output_slice(
        &self,
        elem: &prebindgen_registry::flat::TypeRef,
        registry: &impl Conversions<KotlinMeta>,
        emit: &prebindgen_registry::Emit,
    ) -> Option<ConverterImpl<KotlinMeta>> {
        let inner = registry.output_entry(elem)?;
        let elem_key = elem.key();
        // The element as the source spelled it — the slice type this converter
        // yields is re-emitted, never re-derived.
        let elem = emit.spell(elem);
        // A `&[opaque-handle]` callback arg is delivered by the Kotlin-side leaf
        // fold (typed-handle wrap), bypassing this whole-`ArrayList` converter; a
        // handle's `jlong` wire isn't JObject-shaped, so it returns `None` here.
        let inner_wire = inner.destination.clone();
        if !is_jobject_shaped_wire(&inner_wire) {
            return None;
        }
        // The element's COMPLETE Rust -> wire chain (see the `Vec<_>` peer).
        let inner_conv = crate::jni::emit::composed_inner_output(
            inner,
            quote::quote!(::core::clone::Clone::clone(__elem)),
        );
        let outer_ty: syn::Type = syn::parse_quote!(&[#elem]);
        let wire: syn::Type = syn::parse_quote!(jni::objects::JObject);
        let body: syn::Expr = syn::parse_quote!({
            let __list_obj = env
                .new_object("java/util/ArrayList", "()V", &[])
                .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("&[_]: new ArrayList: {}", e)))?;
            let __list = jni::objects::JList::from_env(env, &__list_obj)
                .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("&[_]: list-from-env: {}", e)))?;
            for __elem in v.iter() {
                let __elem_wire = #inner_conv;
                let __elem_obj: jni::objects::JObject = __elem_wire.into();
                __list.add(env, &__elem_obj)
                    .map_err(|e| <__JniErr as ::core::convert::From<String>>::from(format!("&[_]: list-add: {}", e)))?;
            }
            __list_obj
        });
        let inner_kotlin = inner.metadata.kotlin_name.clone()?;
        let kotlin_name = self.override_kotlin_name(
            &TypeKey::from_type(&outer_ty),
            Some(KtType::generic("List", [inner_kotlin])),
        );
        let projection = inner.metadata.projection.clone().map(|h| Projection {
            strategy: FoldStrategy::Iterable(Box::new(h.strategy)),
            ..h
        });
        let niches = default_niches_for_wire(&wire);
        Some(ConverterImpl {
            subs: vec![elem_key],
            pre_stages: vec![],
            function: self.build_output_fn(&outer_ty, &wire, &body, None),
            destination: wire,
            niches,
            metadata: KotlinMeta {
                kotlin_name,
                value_rust_type: None,
                projection,
            },
        })
    }
}

/// The declaration surface, stated once.
///
/// These were trait methods the registry called back into the adapter from
/// inside `resolve`. They are the adapter's own business now, gathered into the
/// one value the registry is constructed from.
impl Declarations {
    /// Union of every `.fun(...)` list across all
    /// [`Self::package`] subpackage contexts. Each entry is a
    /// `#[prebindgen]` fn ident the user explicitly hooked into the
    /// binding; functions not in this set are skipped by the registry's
    /// signature scan and by the per-item emitter.
    pub(crate) fn declared_functions(&self) -> std::collections::HashSet<syn::Ident> {
        let mut out = std::collections::HashSet::new();
        for pkg in self.packages.values() {
            for m in &pkg.functions {
                out.insert(m.rust_ident.clone());
            }
            // Function-backed constants (`constant_fun`) are ordinary
            // declared functions on the Rust/extern side; only their Kotlin
            // surface differs (an eagerly-initialized top-level `val`).
            for m in &pkg.constant_functions {
                out.insert(m.rust_ident.clone());
            }
        }
        // Class members (accessor/method/constructor) are declared via
        // `.accessor`/`.method`/`.constructor` (not `.fun`) but are still real
        // `#[prebindgen]` wrappers: they need a Rust extern + JNINative
        // `external fun` + JSONL inclusion. Only their Kotlin surface differs
        // (an instance method or companion factory instead of a free fn).
        out.extend(
            self.class_members
                .values()
                .flatten()
                .map(|m| m.rust_ident.clone()),
        );
        out
    }
    /// Functions ever referenced as a named `.field(fun!(...))` in any
    /// `expand_return!` decl, type-level or per-fn — see
    /// [`JniGenBuilder::field_accessor_fns`]. Usage-derived, not tied to `.method()`
    /// class-member declarations: a function need not also be exposed as an
    /// instance method to be referenced this way.
    pub(crate) fn accessor_functions(&self) -> std::collections::HashSet<syn::Ident> {
        self.field_accessor_fns()
    }
    /// Methods (`.method`) — their fn ident mapped to the owning class's
    /// `TypeKey`, so input-flattening can skip the receiver parameter.
    pub(crate) fn method_receivers(&self) -> std::collections::HashMap<syn::Ident, TypeKey> {
        self.class_members
            .iter()
            .flat_map(|(key, ms)| {
                ms.iter()
                    .filter(|m| m.kind == MemberKind::Method)
                    .map(move |m| (m.rust_ident.clone(), key.clone()))
            })
            .collect()
    }
    /// Fns acknowledged-but-unbound via [`JniGenBuilder::ignore`] — suppresses
    /// the registry's "skipping undeclared" warning, emits nothing.
    pub(crate) fn ignored_functions(&self) -> std::collections::HashSet<syn::Ident> {
        self.ignored_fns.clone()
    }
    /// Bulk name-family ignores from [`JniGenBuilder::ignore`] +
    /// [`matching`](crate::matching).
    pub(crate) fn ignored_name_predicates(&self) -> Vec<prebindgen_registry::NamePredicate> {
        self.ignored_name_predicates.clone()
    }
    /// Framework-called fns that get no extern of their own: `convert!`
    /// conversion fns (called by generated converter bodies) and fns
    /// referenced only inside boundary decls (`expand_return!` accessors /
    /// `expand_param!` ctors, called by the generated fold/unfold code).
    /// Routing both through the *helper* channel — not the ignore channel —
    /// makes a typo'd `fun!(…)` inside a decl a hard scan error
    /// (`ScanError::DeclaredNotFound`) instead of a stale-ignore
    /// warning.
    /// Declared functions are subtracted: a fn that is also a real
    /// member/package fn keeps its extern. Type requirements come through
    /// [`Self::extra_required_types`], not a signature scan.
    pub(crate) fn helper_functions(&self) -> std::collections::HashSet<syn::Ident> {
        let declared = self.declared_functions();
        self.convert_fns()
            .chain(self.boundary_referenced_fns())
            .filter(|f| !declared.contains(f))
            .collect()
    }
    /// Union of every `.constant(...)` list across all [`Self::package`]
    /// subpackage contexts. `Some` even when empty — [`JniGenBuilder`] HAS a
    /// const declaration mechanism, so const emission is declared-only and
    /// undeclared consts get the skip warning.
    pub(crate) fn declared_consts(&self) -> Option<std::collections::HashSet<syn::Ident>> {
        let mut out = std::collections::HashSet::new();
        for pkg in self.packages.values() {
            for c in &pkg.constants {
                out.insert(c.rust_ident.clone());
            }
        }
        Some(out)
    }
    /// Consts acknowledged-but-unexposed via [`JniGenBuilder::ignore`].
    pub(crate) fn ignored_consts(&self) -> std::collections::HashSet<syn::Ident> {
        self.ignored_const_idents.clone()
    }
    /// The declared value types of every expression constant
    /// (`ConstDecl::expr`) — they have no `#[prebindgen]` item to
    /// scan, so the resolver is told directly to produce their output
    /// converters.
    pub(crate) fn required_output_types(&self) -> Vec<syn::Type> {
        self.packages
            .values()
            .flat_map(|p| p.constant_exprs.iter().map(|e| e.ty.clone()))
            .collect()
    }
    /// Every type registered via one of the **class declarators**
    /// (`ptr_class!` / `enum_class!` / `sealed_class!` / `data_class!`)
    /// — i.e. every entry in the type table, whose only
    /// writer is `JniGenBuilder::register_class`. These are the only structs/enums
    /// the per-item emitter walks, and the scan requires them in BOTH
    /// directions (their converters always resolve both ways). Wrapper
    /// registrations live in their own tables and are deliberately excluded: a
    /// wrapper type is required per **usage** direction, so an output-only
    /// wrapper needs no input twin.
    ///
    /// Each with the spelling its declarator was written with — the scan needs
    /// real tokens to intern a type that is in no table yet (#291).
    pub(crate) fn declared_types(&self) -> std::collections::HashMap<TypeKey, Origin<syn::Type>> {
        self.types
            .iter()
            .map(|(k, c)| (k.clone(), c.rust_type.clone()))
            .collect()
    }
    /// Types acknowledged-but-undeclared via [`JniGenBuilder::ignore`].
    pub(crate) fn ignored_types(&self) -> std::collections::HashSet<TypeKey> {
        self.ignored_class_types.clone()
    }
    /// What this binding claimed, for the unclaimed-item report. A helper is
    /// claimed even though it is never emitted, and a boundary-only type even
    /// though it never crosses whole: both are deliberate, so neither is a
    /// skip worth reporting.
    pub(crate) fn claimed(&self) -> prebindgen_registry::Claimed {
        let mut functions = self.declared_functions();
        functions.extend(self.helper_functions());
        // The report asks what was *claimed*, which is a set of identities —
        // the declarations' spellings are the scan's business, not this one's.
        let mut types: std::collections::HashSet<TypeKey> =
            self.declared_types().into_keys().collect();
        types.extend(self.boundary_only_types().into_keys());
        prebindgen_registry::Claimed {
            functions,
            types,
            consts: self.declared_consts(),
            ignored_functions: self.ignored_functions(),
            ignored_types: self.ignored_types(),
            ignored_consts: self.ignored_consts(),
            ignored_name_predicates: self.ignored_name_predicates(),
        }
    }
    /// **Rust-side-only** types: boundary decls (`expand_param!` /
    /// `expand_return!`) whose type has no class declaration. They never
    /// materialize in Kotlin — only their ingredients (fold) and fields
    /// (unfold / error channel) cross the boundary — so the registry
    /// acknowledges them and drops their direct converter requirements once
    /// the plans are in place.
    pub(crate) fn boundary_only_types(
        &self,
    ) -> std::collections::HashMap<TypeKey, Origin<syn::Type>> {
        // A `sealed_class!`-declared sum has no single wire: it crosses as a
        // tag plus one leaf group per variant, so a direct converter for the
        // value itself is genuinely not needed. Declaring it boundary-only
        // drops that requirement while keeping the type scanned (its payload
        // types register and resolve, which is what the Kotlin surface reads
        // its field types from).
        self.rust_side_only_types()
            .chain(
                self.types
                    .iter()
                    .filter(|(_, c)| c.sum().is_some())
                    .map(|(k, c)| (k.clone(), c.rust_type.clone())),
            )
            .collect()
    }
}

#[cfg(test)]
mod wrapper_ops_tests {
    use super::*;

    /// Every wrapper the model erases has a row here.
    ///
    /// The two lists answer different questions — the model's is "what do I
    /// erase", this file's is "what can I rebuild" — and they are allowed to
    /// disagree about *capability* (`Cow` is erased and cannot be read through).
    /// They are not allowed to disagree about *membership*: a wrapper that
    /// becomes transparent without a row here would be silently unbridgeable
    /// everywhere, which looks exactly like a type the binding got wrong.
    ///
    /// So adding `Rc` is: one entry in `TRANSPARENT_WRAPPERS`, one row in
    /// `WRAPPER_OPS` (`read: None` — an `Rc`'s payload cannot be moved out —
    /// and `build: Some(Rc::new)`). This test is what says so out loud instead
    /// of leaving the second step to be discovered.
    #[test]
    fn every_erased_wrapper_has_ops() {
        let missing: Vec<&str> = prebindgen_registry::flat::TRANSPARENT_WRAPPERS
            .iter()
            .copied()
            .filter(|w| wrapper_ops(w).is_none())
            .collect();
        assert!(
            missing.is_empty(),
            "the model erases {missing:?}, and this adapter has no `WrapperOps` row for them — \
             add one (`read`/`build` may be `None` when the representation does not allow it, \
             which refuses the shape instead of mis-generating it)"
        );
    }

    /// …and nothing here claims a wrapper the model does not erase, which would
    /// be an operation that can never run.
    #[test]
    fn no_ops_for_a_wrapper_the_model_keeps() {
        let stray: Vec<&str> = WRAPPER_OPS
            .iter()
            .map(|w| w.name)
            .filter(|n| !prebindgen_registry::flat::TRANSPARENT_WRAPPERS.contains(n))
            .collect();
        assert!(
            stray.is_empty(),
            "`WRAPPER_OPS` rows for non-erased {stray:?}"
        );
    }

    /// A rebuild puts the wrappers back **innermost-out**, the reverse of the
    /// order a read takes them off.
    ///
    /// Asserted on a `Box<Box<_>>` rather than a single layer, because a single
    /// layer cannot tell the two orders apart — which is exactly how a
    /// composition bug survives. And asserted against `read` on the same type,
    /// so the two are pinned as duals rather than as two independent claims.
    #[test]
    fn a_rebuild_puts_the_wrappers_back_inside_out() {
        let ty = crate::test_util::reading(syn::parse_quote!(Box<Box<Option<String>>>));
        assert_eq!(ty.erased_wrappers(), ["Box", "Box"]);

        let built = build_through_erased_wrappers(&ty, quote!(v)).expect("Box builds");
        assert_eq!(
            built.to_string().replace(' ', ""),
            ":: std :: boxed :: Box :: new (:: std :: boxed :: Box :: new (v))".replace(' ', ""),
        );
        // The dual, on the same type: reading takes them off outermost-first.
        let read = read_through_erased_wrappers(&ty, quote!(v)).expect("Box reads");
        assert_eq!(read.to_string().replace(' ', ""), "**v");

        // The control: nothing erased, so both are the identity and neither
        // test above can be passing on an unconditional wrap.
        let plain = crate::test_util::reading(syn::parse_quote!(Option<String>));
        assert!(plain.erased_wrappers().is_empty());
        for e in [
            build_through_erased_wrappers(&plain, quote!(v)),
            read_through_erased_wrappers(&plain, quote!(v)),
        ] {
            assert_eq!(e.expect("identity").to_string(), "v");
        }
    }

    /// `Cow` declines a rebuild, and the two directions decline for **different
    /// reasons** — which is why the row carries two `None`s rather than one
    /// capability flag.
    ///
    /// Reading is impossible (`E0507`: a `Cow` payload cannot be moved through
    /// `Deref`). Building is *possible* — `Cow::Owned(v)` is well-typed for an
    /// owned payload — and refused on purpose, because a binding that can only
    /// ever hand a `Cow` parameter `Owned` pays a copy per call and removes the
    /// borrow path the source asked for. If that policy is ever revisited, this
    /// test is the thing that has to change with it.
    #[test]
    fn a_cow_declines_a_rebuild_by_policy() {
        let ty = crate::test_util::reading(syn::parse_quote!(Cow<'_, str>));
        assert_eq!(ty.erased_wrappers(), ["Cow"]);
        assert!(build_through_erased_wrappers(&ty, quote!(v)).is_none());
        assert!(read_through_erased_wrappers(&ty, quote!(v)).is_none());

        // A `Cow` under a `Box` declines too: one unbuildable layer refuses the
        // whole chain, rather than the `Box` half quietly succeeding.
        let nested = crate::test_util::reading(syn::parse_quote!(Box<Cow<'_, str>>));
        assert_eq!(nested.erased_wrappers(), ["Box", "Cow"]);
        assert!(build_through_erased_wrappers(&nested, quote!(v)).is_none());
    }
}