rust-asm 0.2.1

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

/// Represents a constant value loadable by the `LDC` (Load Constant) instruction.
///
/// This enum wraps various types of constants that can be stored in the constant pool
/// and pushed onto the operand stack.
#[derive(Debug, Clone)]
pub enum LdcConstant {
    /// A 32-bit integer constant.
    Integer(i32),
    /// A 32-bit floating-point constant.
    Float(f32),
    /// A 64-bit integer constant.
    Long(i64),
    /// A 64-bit floating-point constant.
    Double(f64),
    /// A string literal constant.
    String(String),
    /// A class constant (e.g., `String.class`).
    Class(String),
    /// A method type constant (MethodDescriptor).
    MethodType(String),
    /// A method handle constant.
    MethodHandle {
        reference_kind: u8,
        reference_index: u16,
    },
    /// A dynamic constant (computed via `invokedynamic` bootstrap methods).
    Dynamic,
}

/// A visitor to visit a Java field.
///
/// The methods of this trait must be called in the following order:
/// `visit_end`.
pub trait FieldVisitor {
    /// Visits the end of the field.
    ///
    /// This method, which is the last one to be called, is used to inform the
    /// visitor that all the annotations and attributes of the field have been visited.
    fn visit_end(&mut self) {}
}

pub trait MethodVisitor {
    /// Starts the visit of the method's code.
    fn visit_code(&mut self) {}

    /// Visits a zero-operand instruction.
    ///
    /// # Arguments
    /// * `opcode` - The opcode of the instruction to be visited.
    fn visit_insn(&mut self, _opcode: u8) {}

    /// Visits an instruction with a single int operand.
    fn visit_int_insn(&mut self, _opcode: u8, _operand: i32) {}

    /// Visits a local variable instruction.
    fn visit_var_insn(&mut self, _opcode: u8, _var_index: u16) {}

    /// Visits a type instruction.
    ///
    /// # Arguments
    /// * `opcode` - The opcode of the instruction.
    /// * `type_name` - The internal name of the object or array class.
    fn visit_type_insn(&mut self, _opcode: u8, _type_name: &str) {}

    /// Visits a field instruction.
    ///
    /// # Arguments
    /// * `opcode` - The opcode of the instruction.
    /// * `owner` - The internal name of the field's owner class.
    /// * `name` - The field's name.
    /// * `desc` - The field's descriptor.
    fn visit_field_insn(&mut self, _opcode: u8, _owner: &str, _name: &str, _desc: &str) {}
    fn visit_method_insn(
        &mut self,
        _opcode: u8,
        _owner: &str,
        _name: &str,
        _desc: &str,
        _is_interface: bool,
    ) {
    }
    fn visit_invoke_dynamic_insn(&mut self, _name: &str, _desc: &str) {}
    /// Visits a jump instruction.
    ///
    /// # Arguments
    /// * `opcode` - The opcode of the instruction.
    /// * `target_offset` - The offset of the target instruction relative to the current instruction.
    fn visit_jump_insn(&mut self, _opcode: u8, _target_offset: i32) {}

    /// Visits an `LDC` instruction.
    fn visit_ldc_insn(&mut self, _value: LdcConstant) {}
    fn visit_iinc_insn(&mut self, _var_index: u16, _increment: i16) {}
    fn visit_table_switch(&mut self, _default: i32, _low: i32, _high: i32, _targets: &[i32]) {}
    fn visit_lookup_switch(&mut self, _default: i32, _pairs: &[(i32, i32)]) {}
    fn visit_multi_anewarray_insn(&mut self, _type_name: &str, _dims: u8) {}
    fn visit_maxs(&mut self, _max_stack: u16, _max_locals: u16) {}
    fn visit_end(&mut self) {}
}

/// A visitor to visit a JPMS module descriptor.
pub trait ModuleVisitor {
    fn visit_main_class(&mut self, _main_class: &str) {}
    fn visit_package(&mut self, _package: &str) {}
    fn visit_require(&mut self, _module: &str, _access_flags: u16, _version: Option<&str>) {}
    fn visit_export(&mut self, _package: &str, _access_flags: u16, _modules: &[String]) {}
    fn visit_open(&mut self, _package: &str, _access_flags: u16, _modules: &[String]) {}
    fn visit_use(&mut self, _service: &str) {}
    fn visit_provide(&mut self, _service: &str, _providers: &[String]) {}
    fn visit_end(&mut self) {}
}

/// A visitor to visit a Java class.
///
/// The methods of this trait must be called in the following order:
/// `visit` -> `visit_source` -> `visit_module` -> (`visit_field` | `visit_method`)* -> `visit_end`.
pub trait ClassVisitor {
    /// Visits the header of the class.
    ///
    /// # Arguments
    /// * `major` - The major version number of the class file.
    /// * `minor` - The minor version number of the class file.
    /// * `access_flags` - The class's access flags (see `Opcodes`).
    /// * `name` - The internal name of the class.
    /// * `super_name` - The internal name of the super class (e.g., `java/lang/String`, `a/b/c`).
    ///   Use `None` for `Object`.
    /// * `interfaces` - The internal names of the class's interfaces.
    fn visit(
        &mut self,
        _major: u16,
        _minor: u16,
        _access_flags: u16,
        _name: &str,
        _super_name: Option<&str>,
        _interfaces: &[String],
    ) {
    }

    /// Visits the source file name of the class.
    fn visit_source(&mut self, _source: &str) {}

    /// Visits the JPMS module descriptor of this class, if this is a `module-info.class`.
    fn visit_module(
        &mut self,
        _name: &str,
        _access_flags: u16,
        _version: Option<&str>,
    ) -> Option<Box<dyn ModuleVisitor>> {
        None
    }

    /// Visits a field of the class.
    ///
    /// Returns an optional `FieldVisitor` to visit the field's content.
    fn visit_field(
        &mut self,
        _access_flags: u16,
        _name: &str,
        _descriptor: &str,
    ) -> Option<Box<dyn FieldVisitor>> {
        None
    }

    /// Visits a method of the class.
    ///
    /// Returns an optional `MethodVisitor` to visit the method's code.
    fn visit_method(
        &mut self,
        _access_flags: u16,
        _name: &str,
        _descriptor: &str,
    ) -> Option<Box<dyn MethodVisitor>> {
        None
    }

    /// Visits the end of the class.
    fn visit_end(&mut self) {}
}

/// A parser to make a [`ClassVisitor`] visit a `ClassFile` structure.
///
/// This class parses a byte array conforming to the Java class file format and
/// calls the appropriate methods of a given class visitor for each field, method,
/// and bytecode instruction encountered.
pub struct ClassReader {
    bytes: Vec<u8>,
}

impl ClassReader {
    /// Constructs a new `ClassReader` with the given class file bytes.
    ///
    /// # Arguments
    ///
    /// * `bytes` - A byte slice containing the JVM class file data.
    pub fn new(bytes: &[u8]) -> Self {
        Self {
            bytes: bytes.to_vec(),
        }
    }

    /// Makes the given visitor visit the Java class of this `ClassReader`.
    ///
    /// This method parses the class file data and drives the visitor events.
    ///
    /// # Arguments
    ///
    /// * `visitor` - The visitor that must visit this class.
    /// * `_options` - Option flags (currently unused, reserve for future parsing options like skipping debug info).
    ///
    /// # Errors
    ///
    /// Returns a [`ClassReadError`] if the class file is malformed or contains unsupported versions.
    pub fn accept(
        &self,
        visitor: &mut dyn ClassVisitor,
        _options: u32,
    ) -> Result<(), ClassReadError> {
        let class_file = read_class_file(&self.bytes)?;
        let name = class_file.class_name(class_file.this_class)?.to_string();
        let super_name = if class_file.super_class == 0 {
            None
        } else {
            Some(class_file.class_name(class_file.super_class)?.to_string())
        };
        let mut interfaces = Vec::with_capacity(class_file.interfaces.len());
        for index in &class_file.interfaces {
            interfaces.push(class_file.class_name(*index)?.to_string());
        }

        visitor.visit(
            class_file.major_version,
            class_file.minor_version,
            class_file.access_flags,
            &name,
            super_name.as_deref(),
            &interfaces,
        );

        for attr in &class_file.attributes {
            if let AttributeInfo::SourceFile { sourcefile_index } = attr {
                let source = class_file.cp_utf8(*sourcefile_index)?;
                visitor.visit_source(source);
            }
        }

        if let Some(module_attr) = class_file.attributes.iter().find_map(|attr| match attr {
            AttributeInfo::Module(module) => Some(module),
            _ => None,
        }) {
            let version = if module_attr.module_version_index == 0 {
                None
            } else {
                Some(class_file.cp_utf8(module_attr.module_version_index)?)
            };
            if let Some(mut mv) = visitor.visit_module(
                class_file.module_name(module_attr.module_name_index)?,
                module_attr.module_flags,
                version,
            ) {
                if let Some(main_class_index) =
                    class_file.attributes.iter().find_map(|attr| match attr {
                        AttributeInfo::ModuleMainClass { main_class_index } => {
                            Some(*main_class_index)
                        }
                        _ => None,
                    })
                {
                    mv.visit_main_class(class_file.class_name(main_class_index)?);
                }
                if let Some(package_index_table) =
                    class_file.attributes.iter().find_map(|attr| match attr {
                        AttributeInfo::ModulePackages {
                            package_index_table,
                        } => Some(package_index_table.as_slice()),
                        _ => None,
                    })
                {
                    for package_index in package_index_table {
                        mv.visit_package(class_file.package_name(*package_index)?);
                    }
                }
                for require in &module_attr.requires {
                    let version = if require.requires_version_index == 0 {
                        None
                    } else {
                        Some(class_file.cp_utf8(require.requires_version_index)?)
                    };
                    mv.visit_require(
                        class_file.module_name(require.requires_index)?,
                        require.requires_flags,
                        version,
                    );
                }
                for export in &module_attr.exports {
                    let modules = export
                        .exports_to_index
                        .iter()
                        .map(|index| class_file.module_name(*index).map(str::to_string))
                        .collect::<Result<Vec<_>, _>>()?;
                    mv.visit_export(
                        class_file.package_name(export.exports_index)?,
                        export.exports_flags,
                        &modules,
                    );
                }
                for open in &module_attr.opens {
                    let modules = open
                        .opens_to_index
                        .iter()
                        .map(|index| class_file.module_name(*index).map(str::to_string))
                        .collect::<Result<Vec<_>, _>>()?;
                    mv.visit_open(
                        class_file.package_name(open.opens_index)?,
                        open.opens_flags,
                        &modules,
                    );
                }
                for uses_index in &module_attr.uses_index {
                    mv.visit_use(class_file.class_name(*uses_index)?);
                }
                for provide in &module_attr.provides {
                    let providers = provide
                        .provides_with_index
                        .iter()
                        .map(|index| class_file.class_name(*index).map(str::to_string))
                        .collect::<Result<Vec<_>, _>>()?;
                    mv.visit_provide(class_file.class_name(provide.provides_index)?, &providers);
                }
                mv.visit_end();
            }
        }

        for field in &class_file.fields {
            let field_name = class_file.cp_utf8(field.name_index)?;
            let field_desc = class_file.cp_utf8(field.descriptor_index)?;
            if let Some(mut fv) = visitor.visit_field(field.access_flags, field_name, field_desc) {
                fv.visit_end();
            }
        }

        for method in &class_file.methods {
            let method_name = class_file.cp_utf8(method.name_index)?;
            let method_desc = class_file.cp_utf8(method.descriptor_index)?;
            if let Some(mut mv) =
                visitor.visit_method(method.access_flags, method_name, method_desc)
            {
                let code = method.attributes.iter().find_map(|attr| match attr {
                    AttributeInfo::Code(code) => Some(code),
                    _ => None,
                });
                if let Some(code) = code {
                    mv.visit_code();
                    let instructions = parse_code_instructions_with_offsets(&code.code)?;
                    for instruction in instructions {
                        visit_instruction(
                            &class_file.constant_pool,
                            instruction.offset as i32,
                            instruction.insn,
                            &mut *mv,
                        )?;
                    }
                    mv.visit_maxs(code.max_stack, code.max_locals);
                }
                mv.visit_end();
            }
        }

        visitor.visit_end();
        Ok(())
    }

    /// Converts the read class data directly into a `ClassNode`.
    ///
    /// This is a convenience method that parses the bytes and builds a
    /// complete object model of the class.
    pub fn to_class_node(&self) -> Result<crate::nodes::ClassNode, ClassReadError> {
        let class_file = read_class_file(&self.bytes)?;
        class_file.to_class_node()
    }
}

#[derive(Debug, Clone)]
pub struct ClassFile {
    pub minor_version: u16,
    pub major_version: u16,
    pub constant_pool: Vec<CpInfo>,
    pub access_flags: u16,
    pub this_class: u16,
    pub super_class: u16,
    pub interfaces: Vec<u16>,
    pub fields: Vec<FieldInfo>,
    pub methods: Vec<MethodInfo>,
    pub attributes: Vec<AttributeInfo>,
}

impl ClassFile {
    pub fn cp_utf8(&self, index: u16) -> Result<&str, ClassReadError> {
        match self
            .constant_pool
            .get(index as usize)
            .ok_or(ClassReadError::InvalidIndex(index))?
        {
            CpInfo::Utf8(value) => Ok(value.as_str()),
            _ => Err(ClassReadError::InvalidIndex(index)),
        }
    }

    pub fn class_name(&self, index: u16) -> Result<&str, ClassReadError> {
        match self
            .constant_pool
            .get(index as usize)
            .ok_or(ClassReadError::InvalidIndex(index))?
        {
            CpInfo::Class { name_index } => self.cp_utf8(*name_index),
            _ => Err(ClassReadError::InvalidIndex(index)),
        }
    }

    pub fn module_name(&self, index: u16) -> Result<&str, ClassReadError> {
        match self
            .constant_pool
            .get(index as usize)
            .ok_or(ClassReadError::InvalidIndex(index))?
        {
            CpInfo::Module { name_index } => self.cp_utf8(*name_index),
            _ => Err(ClassReadError::InvalidIndex(index)),
        }
    }

    pub fn package_name(&self, index: u16) -> Result<&str, ClassReadError> {
        match self
            .constant_pool
            .get(index as usize)
            .ok_or(ClassReadError::InvalidIndex(index))?
        {
            CpInfo::Package { name_index } => self.cp_utf8(*name_index),
            _ => Err(ClassReadError::InvalidIndex(index)),
        }
    }

    pub fn to_class_node(&self) -> Result<crate::nodes::ClassNode, ClassReadError> {
        let name = self.class_name(self.this_class)?.to_string();
        let super_name = if self.super_class == 0 {
            None
        } else {
            Some(self.class_name(self.super_class)?.to_string())
        };
        let source_file = self.attributes.iter().find_map(|attr| match attr {
            AttributeInfo::SourceFile { sourcefile_index } => self
                .cp_utf8(*sourcefile_index)
                .ok()
                .map(|value| value.to_string()),
            _ => None,
        });
        let mut interfaces = Vec::with_capacity(self.interfaces.len());
        for index in &self.interfaces {
            interfaces.push(self.class_name(*index)?.to_string());
        }

        let mut fields = Vec::with_capacity(self.fields.len());
        for field in &self.fields {
            let name = self.cp_utf8(field.name_index)?.to_string();
            let descriptor = self.cp_utf8(field.descriptor_index)?.to_string();
            fields.push(crate::nodes::FieldNode {
                access_flags: field.access_flags,
                name,
                descriptor,
                attributes: field.attributes.clone(),
            });
        }

        let mut methods = Vec::with_capacity(self.methods.len());
        for method in &self.methods {
            let name = self.cp_utf8(method.name_index)?.to_string();
            let descriptor = self.cp_utf8(method.descriptor_index)?.to_string();
            let mut method_attributes = method.attributes.clone();
            method_attributes.retain(|attr| !matches!(attr, AttributeInfo::Code(_)));
            let code = method.attributes.iter().find_map(|attr| match attr {
                AttributeInfo::Code(code) => Some(code),
                _ => None,
            });

            let (
                has_code,
                max_stack,
                max_locals,
                instructions,
                instruction_offsets,
                insn_nodes,
                exception_table,
                try_catch_blocks,
                line_numbers,
                local_variables,
                code_attributes,
            ) = if let Some(code) = code {
                let mut list = InsnList::new();
                let mut instruction_offsets = Vec::with_capacity(code.instructions.len());
                for insn in &code.instructions {
                    list.add(insn.clone());
                }
                for node in parse_code_instructions_with_offsets(&code.code)? {
                    instruction_offsets.push(node.offset);
                }
                let line_numbers = code
                    .attributes
                    .iter()
                    .find_map(|attr| match attr {
                        AttributeInfo::LineNumberTable { entries } => Some(entries.clone()),
                        _ => None,
                    })
                    .unwrap_or_default();
                let local_variables = code
                    .attributes
                    .iter()
                    .find_map(|attr| match attr {
                        AttributeInfo::LocalVariableTable { entries } => Some(entries.clone()),
                        _ => None,
                    })
                    .unwrap_or_default();
                (
                    true,
                    code.max_stack,
                    code.max_locals,
                    list,
                    instruction_offsets,
                    code.insn_nodes.clone(),
                    code.exception_table.clone(),
                    code.try_catch_blocks.clone(),
                    line_numbers,
                    local_variables,
                    code.attributes.clone(),
                )
            } else {
                (
                    false,
                    0,
                    0,
                    InsnList::new(),
                    Vec::new(),
                    Vec::new(),
                    Vec::new(),
                    Vec::new(),
                    Vec::new(),
                    Vec::new(),
                    Vec::new(),
                )
            };
            let method_parameters = method
                .attributes
                .iter()
                .find_map(|attr| match attr {
                    AttributeInfo::MethodParameters { parameters } => Some(parameters.clone()),
                    _ => None,
                })
                .unwrap_or_default();
            let exceptions = method
                .attributes
                .iter()
                .find_map(|attr| match attr {
                    AttributeInfo::Exceptions {
                        exception_index_table,
                    } => Some(exception_index_table),
                    _ => None,
                })
                .map(|entries| -> Result<Vec<String>, ClassReadError> {
                    let mut values = Vec::with_capacity(entries.len());
                    for index in entries {
                        values.push(self.class_name(*index)?.to_string());
                    }
                    Ok(values)
                })
                .transpose()?
                .unwrap_or_default();
            let signature = method.attributes.iter().find_map(|attr| match attr {
                AttributeInfo::Signature { signature_index } => self
                    .cp_utf8(*signature_index)
                    .ok()
                    .map(|value| value.to_string()),
                _ => None,
            });

            methods.push(crate::nodes::MethodNode {
                access_flags: method.access_flags,
                name,
                descriptor,
                has_code,
                max_stack,
                max_locals,
                instructions,
                instruction_offsets,
                insn_nodes,
                exception_table,
                try_catch_blocks,
                line_numbers,
                local_variables,
                method_parameters,
                exceptions,
                signature,
                code_attributes,
                attributes: method_attributes,
            });
        }

        let mut inner_classes = Vec::new();
        for attr in &self.attributes {
            if let AttributeInfo::InnerClasses { classes } = attr {
                for entry in classes {
                    let name = self.class_name(entry.inner_class_info_index)?.to_string();
                    let outer_name = if entry.outer_class_info_index == 0 {
                        None
                    } else {
                        Some(self.class_name(entry.outer_class_info_index)?.to_string())
                    };
                    let inner_name = if entry.inner_name_index == 0 {
                        None
                    } else {
                        Some(self.cp_utf8(entry.inner_name_index)?.to_string())
                    };
                    inner_classes.push(crate::nodes::InnerClassNode {
                        name,
                        outer_name,
                        inner_name,
                        access_flags: entry.inner_class_access_flags,
                    });
                }
            }
        }

        let mut outer_class = String::new();
        if let Some(class_index) = self.attributes.iter().find_map(|attr| match attr {
            AttributeInfo::EnclosingMethod { class_index, .. } => Some(*class_index),
            _ => None,
        }) {
            outer_class = self.class_name(class_index)?.to_string();
        }
        if outer_class.is_empty() {
            for attr in &self.attributes {
                if let AttributeInfo::InnerClasses { classes } = attr
                    && let Some(entry) = classes.iter().find(|entry| {
                        entry.inner_class_info_index == self.this_class
                            && entry.outer_class_info_index != 0
                    })
                {
                    outer_class = self.class_name(entry.outer_class_info_index)?.to_string();
                    break;
                }
            }
        }

        let module = self
            .attributes
            .iter()
            .find_map(|attr| match attr {
                AttributeInfo::Module(module) => Some(module),
                _ => None,
            })
            .map(|module| {
                let requires = module
                    .requires
                    .iter()
                    .map(|require| {
                        Ok(crate::nodes::ModuleRequireNode {
                            module: self.module_name(require.requires_index)?.to_string(),
                            access_flags: require.requires_flags,
                            version: if require.requires_version_index == 0 {
                                None
                            } else {
                                Some(self.cp_utf8(require.requires_version_index)?.to_string())
                            },
                        })
                    })
                    .collect::<Result<Vec<_>, ClassReadError>>()?;
                let exports = module
                    .exports
                    .iter()
                    .map(|export| {
                        Ok(crate::nodes::ModuleExportNode {
                            package: self.package_name(export.exports_index)?.to_string(),
                            access_flags: export.exports_flags,
                            modules: export
                                .exports_to_index
                                .iter()
                                .map(|index| self.module_name(*index).map(str::to_string))
                                .collect::<Result<Vec<_>, ClassReadError>>()?,
                        })
                    })
                    .collect::<Result<Vec<_>, ClassReadError>>()?;
                let opens = module
                    .opens
                    .iter()
                    .map(|open| {
                        Ok(crate::nodes::ModuleOpenNode {
                            package: self.package_name(open.opens_index)?.to_string(),
                            access_flags: open.opens_flags,
                            modules: open
                                .opens_to_index
                                .iter()
                                .map(|index| self.module_name(*index).map(str::to_string))
                                .collect::<Result<Vec<_>, ClassReadError>>()?,
                        })
                    })
                    .collect::<Result<Vec<_>, ClassReadError>>()?;
                let provides = module
                    .provides
                    .iter()
                    .map(|provide| {
                        Ok(crate::nodes::ModuleProvideNode {
                            service: self.class_name(provide.provides_index)?.to_string(),
                            providers: provide
                                .provides_with_index
                                .iter()
                                .map(|index| self.class_name(*index).map(str::to_string))
                                .collect::<Result<Vec<_>, ClassReadError>>()?,
                        })
                    })
                    .collect::<Result<Vec<_>, ClassReadError>>()?;
                let packages = self
                    .attributes
                    .iter()
                    .find_map(|attr| match attr {
                        AttributeInfo::ModulePackages {
                            package_index_table,
                        } => Some(package_index_table),
                        _ => None,
                    })
                    .map(|package_index_table| {
                        package_index_table
                            .iter()
                            .map(|index| self.package_name(*index).map(str::to_string))
                            .collect::<Result<Vec<_>, ClassReadError>>()
                    })
                    .transpose()?
                    .unwrap_or_default();
                let main_class = self
                    .attributes
                    .iter()
                    .find_map(|attr| match attr {
                        AttributeInfo::ModuleMainClass { main_class_index } => {
                            Some(*main_class_index)
                        }
                        _ => None,
                    })
                    .map(|index| self.class_name(index).map(str::to_string))
                    .transpose()?;

                Ok(crate::nodes::ModuleNode {
                    name: self.module_name(module.module_name_index)?.to_string(),
                    access_flags: module.module_flags,
                    version: if module.module_version_index == 0 {
                        None
                    } else {
                        Some(self.cp_utf8(module.module_version_index)?.to_string())
                    },
                    requires,
                    exports,
                    opens,
                    uses: module
                        .uses_index
                        .iter()
                        .map(|index| self.class_name(*index).map(str::to_string))
                        .collect::<Result<Vec<_>, ClassReadError>>()?,
                    provides,
                    packages,
                    main_class,
                })
            })
            .transpose()?;

        let permitted_subclasses = self
            .attributes
            .iter()
            .find_map(|attr| match attr {
                AttributeInfo::PermittedSubclasses { classes } => Some(classes),
                _ => None,
            })
            .map(|classes| {
                classes
                    .iter()
                    .map(|index| self.class_name(*index).map(str::to_string))
                    .collect::<Result<Vec<_>, ClassReadError>>()
            })
            .transpose()?
            .unwrap_or_default();

        let record_components = self
            .attributes
            .iter()
            .find_map(|attr| match attr {
                AttributeInfo::Record { components } => Some(components),
                _ => None,
            })
            .map(|components| {
                components
                    .iter()
                    .map(|comp| {
                        Ok(crate::nodes::RecordComponentNode {
                            name: self.cp_utf8(comp.name_index)?.to_string(),
                            descriptor: self.cp_utf8(comp.descriptor_index)?.to_string(),
                            attributes: comp.attributes.clone(),
                        })
                    })
                    .collect::<Result<Vec<_>, ClassReadError>>()
            })
            .transpose()?
            .unwrap_or_default();

        Ok(crate::nodes::ClassNode {
            minor_version: self.minor_version,
            major_version: self.major_version,
            access_flags: self.access_flags,
            constant_pool: self.constant_pool.clone(),
            this_class: self.this_class,
            name,
            super_name,
            source_file,
            interfaces,
            interface_indices: self.interfaces.clone(),
            fields,
            methods,
            attributes: self.attributes.clone(),
            inner_classes,
            outer_class,
            module,
            record_components,
            permitted_subclasses,
        })
    }
}

#[derive(Debug, Clone)]
pub struct FieldInfo {
    pub access_flags: u16,
    pub name_index: u16,
    pub descriptor_index: u16,
    pub attributes: Vec<AttributeInfo>,
}

#[derive(Debug, Clone)]
pub struct MethodInfo {
    pub access_flags: u16,
    pub name_index: u16,
    pub descriptor_index: u16,
    pub attributes: Vec<AttributeInfo>,
}

#[derive(Debug, Clone)]
pub struct RecordComponent {
    pub name_index: u16,
    pub descriptor_index: u16,
    pub attributes: Vec<AttributeInfo>,
}

#[derive(Debug, Clone)]
pub enum AttributeInfo {
    Code(CodeAttribute),
    ConstantValue { constantvalue_index: u16 },
    Exceptions { exception_index_table: Vec<u16> },
    SourceFile { sourcefile_index: u16 },
    LineNumberTable { entries: Vec<LineNumber> },
    LocalVariableTable { entries: Vec<LocalVariable> },
    Signature { signature_index: u16 },
    StackMapTable { entries: Vec<StackMapFrame> },
    Deprecated,
    Synthetic,
    InnerClasses { classes: Vec<InnerClass> },
    EnclosingMethod { class_index: u16, method_index: u16 },
    Module(ModuleAttribute),
    ModulePackages { package_index_table: Vec<u16> },
    ModuleMainClass { main_class_index: u16 },
    BootstrapMethods { methods: Vec<BootstrapMethod> },
    MethodParameters { parameters: Vec<MethodParameter> },
    RuntimeVisibleAnnotations { annotations: Vec<Annotation> },
    RuntimeInvisibleAnnotations { annotations: Vec<Annotation> },
    RuntimeVisibleParameterAnnotations { parameters: ParameterAnnotations },
    RuntimeInvisibleParameterAnnotations { parameters: ParameterAnnotations },
    RuntimeVisibleTypeAnnotations { annotations: Vec<TypeAnnotation> },
    RuntimeInvisibleTypeAnnotations { annotations: Vec<TypeAnnotation> },
    PermittedSubclasses { classes: Vec<u16> },
    Record { components: Vec<RecordComponent> },
    Unknown { name: String, info: Vec<u8> },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleAttribute {
    pub module_name_index: u16,
    pub module_flags: u16,
    pub module_version_index: u16,
    pub requires: Vec<ModuleRequire>,
    pub exports: Vec<ModuleExport>,
    pub opens: Vec<ModuleOpen>,
    pub uses_index: Vec<u16>,
    pub provides: Vec<ModuleProvide>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleRequire {
    pub requires_index: u16,
    pub requires_flags: u16,
    pub requires_version_index: u16,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleExport {
    pub exports_index: u16,
    pub exports_flags: u16,
    pub exports_to_index: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleOpen {
    pub opens_index: u16,
    pub opens_flags: u16,
    pub opens_to_index: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleProvide {
    pub provides_index: u16,
    pub provides_with_index: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Annotation {
    pub type_descriptor_index: u16,
    pub element_value_pairs: Vec<ElementValuePair>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ElementValuePair {
    pub element_name_index: u16,
    pub value: ElementValue,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ElementValue {
    ConstValueIndex {
        tag: u8,
        const_value_index: u16,
    },
    EnumConstValue {
        type_name_index: u16,
        const_name_index: u16,
    },
    ClassInfoIndex {
        class_info_index: u16,
    },
    AnnotationValue(Box<Annotation>),
    ArrayValue(Vec<ElementValue>),
}

#[derive(Debug, Clone, PartialEq)]
pub struct ParameterAnnotations {
    pub parameters: Vec<Vec<Annotation>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypeAnnotation {
    pub target_type: u8,
    pub target_info: TypeAnnotationTargetInfo,
    pub target_path: TypePath,
    pub annotation: Annotation,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypePath {
    pub path: Vec<TypePathEntry>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypePathEntry {
    pub type_path_kind: u8,
    pub type_argument_index: u8,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TypeAnnotationTargetInfo {
    TypeParameter {
        type_parameter_index: u8,
    },
    Supertype {
        supertype_index: u16,
    },
    TypeParameterBound {
        type_parameter_index: u8,
        bound_index: u8,
    },
    Empty,
    FormalParameter {
        formal_parameter_index: u8,
    },
    Throws {
        throws_type_index: u16,
    },
    LocalVar {
        table: Vec<LocalVarTargetTableEntry>,
    },
    Catch {
        exception_table_index: u16,
    },
    Offset {
        offset: u16,
    },
    TypeArgument {
        offset: u16,
        type_argument_index: u8,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct LocalVarTargetTableEntry {
    pub start_pc: u16,
    pub length: u16,
    pub index: u16,
}

#[derive(Debug, Clone)]
pub struct CodeAttribute {
    pub max_stack: u16,
    pub max_locals: u16,
    pub code: Vec<u8>,
    pub instructions: Vec<Insn>,
    pub insn_nodes: Vec<AbstractInsnNode>,
    pub exception_table: Vec<ExceptionTableEntry>,
    pub try_catch_blocks: Vec<TryCatchBlockNode>,
    pub attributes: Vec<AttributeInfo>,
}

#[derive(Debug, Clone)]
pub struct ExceptionTableEntry {
    pub start_pc: u16,
    pub end_pc: u16,
    pub handler_pc: u16,
    pub catch_type: u16,
}

#[derive(Debug, Clone)]
pub struct LineNumber {
    pub start_pc: u16,
    pub line_number: u16,
}

#[derive(Debug, Clone)]
pub struct LocalVariable {
    pub start_pc: u16,
    pub length: u16,
    pub name_index: u16,
    pub descriptor_index: u16,
    pub index: u16,
}

#[derive(Debug, Clone)]
pub struct InnerClass {
    pub inner_class_info_index: u16,
    pub outer_class_info_index: u16,
    pub inner_name_index: u16,
    pub inner_class_access_flags: u16,
}

#[derive(Debug, Clone)]
pub struct BootstrapMethod {
    pub bootstrap_method_ref: u16,
    pub bootstrap_arguments: Vec<u16>,
}

#[derive(Debug, Clone)]
pub struct MethodParameter {
    pub name_index: u16,
    pub access_flags: u16,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerificationTypeInfo {
    Top,
    Integer,
    Float,
    Long,
    Double,
    Null,
    UninitializedThis,
    Object { cpool_index: u16 },
    Uninitialized { offset: u16 },
}

#[derive(Debug, Clone)]
pub enum StackMapFrame {
    SameFrame {
        offset_delta: u16,
    },
    SameLocals1StackItemFrame {
        offset_delta: u16,
        stack: VerificationTypeInfo,
    },
    SameLocals1StackItemFrameExtended {
        offset_delta: u16,
        stack: VerificationTypeInfo,
    },
    ChopFrame {
        offset_delta: u16,
        k: u8,
    },
    SameFrameExtended {
        offset_delta: u16,
    },
    AppendFrame {
        offset_delta: u16,
        locals: Vec<VerificationTypeInfo>,
    },
    FullFrame {
        offset_delta: u16,
        locals: Vec<VerificationTypeInfo>,
        stack: Vec<VerificationTypeInfo>,
    },
}

pub fn read_class_file(bytes: &[u8]) -> Result<ClassFile, ClassReadError> {
    let mut reader = ByteReader::new(bytes);
    let magic = reader.read_u4()?;
    if magic != 0xCAFEBABE {
        return Err(ClassReadError::InvalidMagic(magic));
    }
    let minor_version = reader.read_u2()?;
    let major_version = reader.read_u2()?;
    if major_version > constants::V25 {
        return Err(ClassReadError::InvalidClassVersion(major_version));
    }
    let constant_pool = read_constant_pool(&mut reader)?;
    let access_flags = reader.read_u2()?;
    let this_class = reader.read_u2()?;
    let super_class = reader.read_u2()?;
    let interfaces = read_u2_table(&mut reader)?;
    let fields = read_fields(&mut reader, &constant_pool)?;
    let methods = read_methods(&mut reader, &constant_pool)?;
    let attributes = read_attributes(&mut reader, &constant_pool)?;

    Ok(ClassFile {
        minor_version,
        major_version,
        constant_pool,
        access_flags,
        this_class,
        super_class,
        interfaces,
        fields,
        methods,
        attributes,
    })
}

fn read_constant_pool(reader: &mut ByteReader<'_>) -> Result<Vec<CpInfo>, ClassReadError> {
    let count = reader.read_u2()? as usize;
    let mut pool = Vec::with_capacity(count);
    pool.push(CpInfo::Unusable);

    let mut index = 1;
    while index < count {
        let tag = reader.read_u1()?;
        let entry = match tag {
            1 => {
                let len = reader.read_u2()? as usize;
                let bytes = reader.read_bytes(len)?;
                let value = decode_modified_utf8(&bytes)?;
                CpInfo::Utf8(value)
            }
            3 => {
                let value = reader.read_u4()? as i32;
                CpInfo::Integer(value)
            }
            4 => {
                let value = f32::from_bits(reader.read_u4()?);
                CpInfo::Float(value)
            }
            5 => {
                let value = reader.read_u8()? as i64;
                CpInfo::Long(value)
            }
            6 => {
                let value = f64::from_bits(reader.read_u8()?);
                CpInfo::Double(value)
            }
            7 => CpInfo::Class {
                name_index: reader.read_u2()?,
            },
            8 => CpInfo::String {
                string_index: reader.read_u2()?,
            },
            9 => CpInfo::Fieldref {
                class_index: reader.read_u2()?,
                name_and_type_index: reader.read_u2()?,
            },
            10 => CpInfo::Methodref {
                class_index: reader.read_u2()?,
                name_and_type_index: reader.read_u2()?,
            },
            11 => CpInfo::InterfaceMethodref {
                class_index: reader.read_u2()?,
                name_and_type_index: reader.read_u2()?,
            },
            12 => CpInfo::NameAndType {
                name_index: reader.read_u2()?,
                descriptor_index: reader.read_u2()?,
            },
            15 => CpInfo::MethodHandle {
                reference_kind: reader.read_u1()?,
                reference_index: reader.read_u2()?,
            },
            16 => CpInfo::MethodType {
                descriptor_index: reader.read_u2()?,
            },
            17 => CpInfo::Dynamic {
                bootstrap_method_attr_index: reader.read_u2()?,
                name_and_type_index: reader.read_u2()?,
            },
            18 => CpInfo::InvokeDynamic {
                bootstrap_method_attr_index: reader.read_u2()?,
                name_and_type_index: reader.read_u2()?,
            },
            19 => CpInfo::Module {
                name_index: reader.read_u2()?,
            },
            20 => CpInfo::Package {
                name_index: reader.read_u2()?,
            },
            _ => return Err(ClassReadError::InvalidConstantPoolTag(tag)),
        };

        pool.push(entry);

        if tag == 5 || tag == 6 {
            pool.push(CpInfo::Unusable);
            index += 2;
        } else {
            index += 1;
        }
    }

    Ok(pool)
}

fn read_u2_table(reader: &mut ByteReader<'_>) -> Result<Vec<u16>, ClassReadError> {
    let count = reader.read_u2()? as usize;
    let mut values = Vec::with_capacity(count);
    for _ in 0..count {
        values.push(reader.read_u2()?);
    }
    Ok(values)
}

fn read_fields(
    reader: &mut ByteReader<'_>,
    cp: &[CpInfo],
) -> Result<Vec<FieldInfo>, ClassReadError> {
    let count = reader.read_u2()? as usize;
    let mut fields = Vec::with_capacity(count);
    for _ in 0..count {
        let access_flags = reader.read_u2()?;
        let name_index = reader.read_u2()?;
        let descriptor_index = reader.read_u2()?;
        let attributes = read_attributes(reader, cp)?;
        fields.push(FieldInfo {
            access_flags,
            name_index,
            descriptor_index,
            attributes,
        });
    }
    Ok(fields)
}

fn read_methods(
    reader: &mut ByteReader<'_>,
    cp: &[CpInfo],
) -> Result<Vec<MethodInfo>, ClassReadError> {
    let count = reader.read_u2()? as usize;
    let mut methods = Vec::with_capacity(count);
    for _ in 0..count {
        let access_flags = reader.read_u2()?;
        let name_index = reader.read_u2()?;
        let descriptor_index = reader.read_u2()?;
        let attributes = read_attributes(reader, cp)?;
        methods.push(MethodInfo {
            access_flags,
            name_index,
            descriptor_index,
            attributes,
        });
    }
    Ok(methods)
}

fn read_attributes(
    reader: &mut ByteReader<'_>,
    cp: &[CpInfo],
) -> Result<Vec<AttributeInfo>, ClassReadError> {
    let count = reader.read_u2()? as usize;
    let mut attributes = Vec::with_capacity(count);
    for _ in 0..count {
        let name_index = reader.read_u2()?;
        let length = reader.read_u4()? as usize;
        let name = cp_utf8(cp, name_index)?;
        let info = reader.read_bytes(length)?;
        let attribute = parse_attribute(name, info, cp)?;
        attributes.push(attribute);
    }
    Ok(attributes)
}

fn parse_attribute(
    name: &str,
    info: Vec<u8>,
    cp: &[CpInfo],
) -> Result<AttributeInfo, ClassReadError> {
    let mut reader = ByteReader::new(&info);
    let attribute = match name {
        "Code" => {
            let max_stack = reader.read_u2()?;
            let max_locals = reader.read_u2()?;
            let code_length = reader.read_u4()? as usize;
            let code = reader.read_bytes(code_length)?;
            let instructions = parse_code_instructions(&code)?;
            let exception_table_length = reader.read_u2()? as usize;
            let mut exception_table = Vec::with_capacity(exception_table_length);
            for _ in 0..exception_table_length {
                exception_table.push(ExceptionTableEntry {
                    start_pc: reader.read_u2()?,
                    end_pc: reader.read_u2()?,
                    handler_pc: reader.read_u2()?,
                    catch_type: reader.read_u2()?,
                });
            }
            let attributes = read_attributes(&mut reader, cp)?;
            let (mut insn_nodes, try_catch_blocks, label_by_offset) =
                build_insn_nodes(&code, &exception_table, cp)?;
            let line_numbers = attributes.iter().find_map(|attr| match attr {
                AttributeInfo::LineNumberTable { entries } => Some(entries.as_slice()),
                _ => None,
            });
            if let Some(entries) = line_numbers {
                insn_nodes = attach_line_numbers(insn_nodes, entries, &label_by_offset);
            }
            AttributeInfo::Code(CodeAttribute {
                max_stack,
                max_locals,
                code,
                instructions,
                insn_nodes,
                exception_table,
                try_catch_blocks,
                attributes,
            })
        }
        "ConstantValue" => AttributeInfo::ConstantValue {
            constantvalue_index: reader.read_u2()?,
        },
        "Exceptions" => {
            let count = reader.read_u2()? as usize;
            let mut exception_index_table = Vec::with_capacity(count);
            for _ in 0..count {
                exception_index_table.push(reader.read_u2()?);
            }
            AttributeInfo::Exceptions {
                exception_index_table,
            }
        }
        "SourceFile" => AttributeInfo::SourceFile {
            sourcefile_index: reader.read_u2()?,
        },
        "LineNumberTable" => {
            let count = reader.read_u2()? as usize;
            let mut entries = Vec::with_capacity(count);
            for _ in 0..count {
                entries.push(LineNumber {
                    start_pc: reader.read_u2()?,
                    line_number: reader.read_u2()?,
                });
            }
            AttributeInfo::LineNumberTable { entries }
        }
        "LocalVariableTable" => {
            let count = reader.read_u2()? as usize;
            let mut entries = Vec::with_capacity(count);
            for _ in 0..count {
                entries.push(LocalVariable {
                    start_pc: reader.read_u2()?,
                    length: reader.read_u2()?,
                    name_index: reader.read_u2()?,
                    descriptor_index: reader.read_u2()?,
                    index: reader.read_u2()?,
                });
            }
            AttributeInfo::LocalVariableTable { entries }
        }
        "Signature" => AttributeInfo::Signature {
            signature_index: reader.read_u2()?,
        },
        "StackMapTable" => {
            let count = reader.read_u2()? as usize;
            let mut entries = Vec::with_capacity(count);
            for _ in 0..count {
                let frame_type = reader.read_u1()?;
                let frame = match frame_type {
                    0..=63 => StackMapFrame::SameFrame {
                        offset_delta: frame_type as u16,
                    },
                    64..=127 => StackMapFrame::SameLocals1StackItemFrame {
                        offset_delta: (frame_type - 64) as u16,
                        stack: parse_verification_type(&mut reader)?,
                    },
                    247 => StackMapFrame::SameLocals1StackItemFrameExtended {
                        offset_delta: reader.read_u2()?,
                        stack: parse_verification_type(&mut reader)?,
                    },
                    248..=250 => StackMapFrame::ChopFrame {
                        offset_delta: reader.read_u2()?,
                        k: 251 - frame_type,
                    },
                    251 => StackMapFrame::SameFrameExtended {
                        offset_delta: reader.read_u2()?,
                    },
                    252..=254 => {
                        let offset_delta = reader.read_u2()?;
                        let locals_count = (frame_type - 251) as usize;
                        let mut locals = Vec::with_capacity(locals_count);
                        for _ in 0..locals_count {
                            locals.push(parse_verification_type(&mut reader)?);
                        }
                        StackMapFrame::AppendFrame {
                            offset_delta,
                            locals,
                        }
                    }
                    255 => {
                        let offset_delta = reader.read_u2()?;
                        let locals_count = reader.read_u2()? as usize;
                        let mut locals = Vec::with_capacity(locals_count);
                        for _ in 0..locals_count {
                            locals.push(parse_verification_type(&mut reader)?);
                        }
                        let stack_count = reader.read_u2()? as usize;
                        let mut stack = Vec::with_capacity(stack_count);
                        for _ in 0..stack_count {
                            stack.push(parse_verification_type(&mut reader)?);
                        }
                        StackMapFrame::FullFrame {
                            offset_delta,
                            locals,
                            stack,
                        }
                    }
                    _ => {
                        return Err(ClassReadError::InvalidAttribute(
                            "StackMapTable".to_string(),
                        ));
                    }
                };
                entries.push(frame);
            }
            AttributeInfo::StackMapTable { entries }
        }
        "Deprecated" => AttributeInfo::Deprecated,
        "Synthetic" => AttributeInfo::Synthetic,
        "InnerClasses" => {
            let count = reader.read_u2()? as usize;
            let mut classes = Vec::with_capacity(count);
            for _ in 0..count {
                classes.push(InnerClass {
                    inner_class_info_index: reader.read_u2()?,
                    outer_class_info_index: reader.read_u2()?,
                    inner_name_index: reader.read_u2()?,
                    inner_class_access_flags: reader.read_u2()?,
                });
            }
            AttributeInfo::InnerClasses { classes }
        }
        "EnclosingMethod" => AttributeInfo::EnclosingMethod {
            class_index: reader.read_u2()?,
            method_index: reader.read_u2()?,
        },
        "Module" => AttributeInfo::Module(parse_module_attribute(&mut reader)?),
        "ModulePackages" => {
            let count = reader.read_u2()? as usize;
            let mut package_index_table = Vec::with_capacity(count);
            for _ in 0..count {
                package_index_table.push(reader.read_u2()?);
            }
            AttributeInfo::ModulePackages {
                package_index_table,
            }
        }
        "ModuleMainClass" => AttributeInfo::ModuleMainClass {
            main_class_index: reader.read_u2()?,
        },
        "BootstrapMethods" => {
            let count = reader.read_u2()? as usize;
            let mut methods = Vec::with_capacity(count);
            for _ in 0..count {
                let bootstrap_method_ref = reader.read_u2()?;
                let arg_count = reader.read_u2()? as usize;
                let mut bootstrap_arguments = Vec::with_capacity(arg_count);
                for _ in 0..arg_count {
                    bootstrap_arguments.push(reader.read_u2()?);
                }
                methods.push(BootstrapMethod {
                    bootstrap_method_ref,
                    bootstrap_arguments,
                });
            }
            AttributeInfo::BootstrapMethods { methods }
        }
        "MethodParameters" => {
            let count = reader.read_u1()? as usize;
            let mut parameters = Vec::with_capacity(count);
            for _ in 0..count {
                parameters.push(MethodParameter {
                    name_index: reader.read_u2()?,
                    access_flags: reader.read_u2()?,
                });
            }
            AttributeInfo::MethodParameters { parameters }
        }
        "RuntimeVisibleAnnotations" => {
            let annotations = parse_annotations(&mut reader)?;
            AttributeInfo::RuntimeVisibleAnnotations { annotations }
        }
        "RuntimeInvisibleAnnotations" => {
            let annotations = parse_annotations(&mut reader)?;
            AttributeInfo::RuntimeInvisibleAnnotations { annotations }
        }
        "RuntimeVisibleParameterAnnotations" => {
            let parameters = parse_parameter_annotations(&mut reader)?;
            AttributeInfo::RuntimeVisibleParameterAnnotations { parameters }
        }
        "RuntimeInvisibleParameterAnnotations" => {
            let parameters = parse_parameter_annotations(&mut reader)?;
            AttributeInfo::RuntimeInvisibleParameterAnnotations { parameters }
        }
        "RuntimeVisibleTypeAnnotations" => {
            let annotations = parse_type_annotations(&mut reader)?;
            AttributeInfo::RuntimeVisibleTypeAnnotations { annotations }
        }
        "RuntimeInvisibleTypeAnnotations" => {
            let annotations = parse_type_annotations(&mut reader)?;
            AttributeInfo::RuntimeInvisibleTypeAnnotations { annotations }
        }
        "PermittedSubclasses" => {
            let count = reader.read_u2()? as usize;
            let mut classes = Vec::with_capacity(count);
            for _ in 0..count {
                classes.push(reader.read_u2()?);
            }
            AttributeInfo::PermittedSubclasses { classes }
        }
        "Record" => {
            let count = reader.read_u2()? as usize;
            let mut components = Vec::with_capacity(count);
            for _ in 0..count {
                let name_index = reader.read_u2()?;
                let descriptor_index = reader.read_u2()?;
                let attributes = read_attributes(&mut reader, cp)?;
                components.push(RecordComponent {
                    name_index,
                    descriptor_index,
                    attributes,
                });
            }
            AttributeInfo::Record { components }
        }
        _ => {
            return Ok(AttributeInfo::Unknown {
                name: name.to_string(),
                info,
            });
        }
    };

    if reader.remaining() != 0 {
        return Err(ClassReadError::InvalidAttribute(name.to_string()));
    }

    Ok(attribute)
}

fn parse_module_attribute(reader: &mut ByteReader<'_>) -> Result<ModuleAttribute, ClassReadError> {
    let module_name_index = reader.read_u2()?;
    let module_flags = reader.read_u2()?;
    let module_version_index = reader.read_u2()?;

    let requires_count = reader.read_u2()? as usize;
    let mut requires = Vec::with_capacity(requires_count);
    for _ in 0..requires_count {
        requires.push(ModuleRequire {
            requires_index: reader.read_u2()?,
            requires_flags: reader.read_u2()?,
            requires_version_index: reader.read_u2()?,
        });
    }

    let exports_count = reader.read_u2()? as usize;
    let mut exports = Vec::with_capacity(exports_count);
    for _ in 0..exports_count {
        let exports_index = reader.read_u2()?;
        let exports_flags = reader.read_u2()?;
        let exports_to_count = reader.read_u2()? as usize;
        let mut exports_to_index = Vec::with_capacity(exports_to_count);
        for _ in 0..exports_to_count {
            exports_to_index.push(reader.read_u2()?);
        }
        exports.push(ModuleExport {
            exports_index,
            exports_flags,
            exports_to_index,
        });
    }

    let opens_count = reader.read_u2()? as usize;
    let mut opens = Vec::with_capacity(opens_count);
    for _ in 0..opens_count {
        let opens_index = reader.read_u2()?;
        let opens_flags = reader.read_u2()?;
        let opens_to_count = reader.read_u2()? as usize;
        let mut opens_to_index = Vec::with_capacity(opens_to_count);
        for _ in 0..opens_to_count {
            opens_to_index.push(reader.read_u2()?);
        }
        opens.push(ModuleOpen {
            opens_index,
            opens_flags,
            opens_to_index,
        });
    }

    let uses_count = reader.read_u2()? as usize;
    let mut uses_index = Vec::with_capacity(uses_count);
    for _ in 0..uses_count {
        uses_index.push(reader.read_u2()?);
    }

    let provides_count = reader.read_u2()? as usize;
    let mut provides = Vec::with_capacity(provides_count);
    for _ in 0..provides_count {
        let provides_index = reader.read_u2()?;
        let provides_with_count = reader.read_u2()? as usize;
        let mut provides_with_index = Vec::with_capacity(provides_with_count);
        for _ in 0..provides_with_count {
            provides_with_index.push(reader.read_u2()?);
        }
        provides.push(ModuleProvide {
            provides_index,
            provides_with_index,
        });
    }

    Ok(ModuleAttribute {
        module_name_index,
        module_flags,
        module_version_index,
        requires,
        exports,
        opens,
        uses_index,
        provides,
    })
}

fn parse_annotations(reader: &mut ByteReader) -> Result<Vec<Annotation>, ClassReadError> {
    let num = reader.read_u2()? as usize;
    let mut out = Vec::with_capacity(num);
    for _ in 0..num {
        out.push(parse_annotation(reader)?);
    }
    Ok(out)
}

fn parse_annotation(reader: &mut ByteReader) -> Result<Annotation, ClassReadError> {
    let type_descriptor_index = reader.read_u2()?;
    let num_pairs = reader.read_u2()? as usize;
    let mut element_value_pairs = Vec::with_capacity(num_pairs);
    for _ in 0..num_pairs {
        let element_name_index = reader.read_u2()?;
        let value = parse_element_value(reader)?;
        element_value_pairs.push(ElementValuePair {
            element_name_index,
            value,
        });
    }
    Ok(Annotation {
        type_descriptor_index,
        element_value_pairs,
    })
}

fn parse_parameter_annotations(
    reader: &mut ByteReader,
) -> Result<ParameterAnnotations, ClassReadError> {
    let num_params = reader.read_u1()? as usize;
    let mut parameters = Vec::with_capacity(num_params);
    for _ in 0..num_params {
        let num_ann = reader.read_u2()? as usize;
        let mut anns = Vec::with_capacity(num_ann);
        for _ in 0..num_ann {
            anns.push(parse_annotation(reader)?);
        }
        parameters.push(anns);
    }
    Ok(ParameterAnnotations { parameters })
}

fn parse_type_annotations(reader: &mut ByteReader) -> Result<Vec<TypeAnnotation>, ClassReadError> {
    let num = reader.read_u2()? as usize;
    let mut out = Vec::with_capacity(num);
    for _ in 0..num {
        out.push(parse_type_annotation(reader)?);
    }
    Ok(out)
}

fn parse_type_annotation(reader: &mut ByteReader) -> Result<TypeAnnotation, ClassReadError> {
    let target_type = reader.read_u1()?;
    let target_info = parse_type_annotation_target_info(reader, target_type)?;
    let target_path = parse_type_path(reader)?;
    let annotation = parse_annotation(reader)?;
    Ok(TypeAnnotation {
        target_type,
        target_info,
        target_path,
        annotation,
    })
}

fn parse_type_path(reader: &mut ByteReader) -> Result<TypePath, ClassReadError> {
    let path_length = reader.read_u1()? as usize;
    let mut path = Vec::with_capacity(path_length);
    for _ in 0..path_length {
        path.push(TypePathEntry {
            type_path_kind: reader.read_u1()?,
            type_argument_index: reader.read_u1()?,
        });
    }
    Ok(TypePath { path })
}

fn parse_type_annotation_target_info(
    reader: &mut ByteReader,
    target_type: u8,
) -> Result<TypeAnnotationTargetInfo, ClassReadError> {
    use crate::constants::*;

    let ti = match target_type {
        TA_TARGET_CLASS_TYPE_PARAMETER | TA_TARGET_METHOD_TYPE_PARAMETER => {
            TypeAnnotationTargetInfo::TypeParameter {
                type_parameter_index: reader.read_u1()?,
            }
        }

        TA_TARGET_CLASS_EXTENDS => TypeAnnotationTargetInfo::Supertype {
            supertype_index: reader.read_u2()?,
        },

        TA_TARGET_CLASS_TYPE_PARAMETER_BOUND | TA_TARGET_METHOD_TYPE_PARAMETER_BOUND => {
            TypeAnnotationTargetInfo::TypeParameterBound {
                type_parameter_index: reader.read_u1()?,
                bound_index: reader.read_u1()?,
            }
        }

        TA_TARGET_FIELD | TA_TARGET_METHOD_RETURN | TA_TARGET_METHOD_RECEIVER => {
            TypeAnnotationTargetInfo::Empty
        }

        TA_TARGET_METHOD_FORMAL_PARAMETER => TypeAnnotationTargetInfo::FormalParameter {
            formal_parameter_index: reader.read_u1()?,
        },

        TA_TARGET_THROWS => TypeAnnotationTargetInfo::Throws {
            throws_type_index: reader.read_u2()?,
        },

        TA_TARGET_LOCAL_VARIABLE | TA_TARGET_RESOURCE_VARIABLE => {
            let table_length = reader.read_u2()? as usize;
            let mut table = Vec::with_capacity(table_length);
            for _ in 0..table_length {
                table.push(LocalVarTargetTableEntry {
                    start_pc: reader.read_u2()?,
                    length: reader.read_u2()?,
                    index: reader.read_u2()?,
                });
            }
            TypeAnnotationTargetInfo::LocalVar { table }
        }

        TA_TARGET_EXCEPTION_PARAMETER => TypeAnnotationTargetInfo::Catch {
            exception_table_index: reader.read_u2()?,
        },

        TA_TARGET_INSTANCEOF
        | TA_TARGET_NEW
        | TA_TARGET_CONSTRUCTOR_REFERENCE_RECEIVER
        | TA_TARGET_METHOD_REFERENCE_RECEIVER => TypeAnnotationTargetInfo::Offset {
            offset: reader.read_u2()?,
        },

        TA_TARGET_CAST
        | TA_TARGET_CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
        | TA_TARGET_METHOD_INVOCATION_TYPE_ARGUMENT
        | TA_TARGET_CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
        | TA_TARGET_METHOD_REFERENCE_TYPE_ARGUMENT => TypeAnnotationTargetInfo::TypeArgument {
            offset: reader.read_u2()?,
            type_argument_index: reader.read_u1()?,
        },

        _ => {
            return Err(ClassReadError::InvalidAttribute(format!(
                "TypeAnnotationTargetInfo(target_type=0x{:02X})",
                target_type
            )));
        }
    };

    Ok(ti)
}

fn parse_element_value(reader: &mut ByteReader) -> Result<ElementValue, ClassReadError> {
    let tag = reader.read_u1()?;
    let v = match tag {
        b'B' | b'C' | b'D' | b'F' | b'I' | b'J' | b'S' | b'Z' | b's' => {
            ElementValue::ConstValueIndex {
                tag,
                const_value_index: reader.read_u2()?,
            }
        }
        b'e' => ElementValue::EnumConstValue {
            type_name_index: reader.read_u2()?,
            const_name_index: reader.read_u2()?,
        },
        b'c' => ElementValue::ClassInfoIndex {
            class_info_index: reader.read_u2()?,
        },
        b'@' => ElementValue::AnnotationValue(Box::new(parse_annotation(reader)?)),
        b'[' => {
            let n = reader.read_u2()? as usize;
            let mut items = Vec::with_capacity(n);
            for _ in 0..n {
                items.push(parse_element_value(reader)?);
            }
            ElementValue::ArrayValue(items)
        }
        _ => {
            return Err(ClassReadError::InvalidAttribute(
                "AnnotationElementValue".to_string(),
            ));
        }
    };
    Ok(v)
}

fn parse_verification_type(
    reader: &mut ByteReader<'_>,
) -> Result<VerificationTypeInfo, ClassReadError> {
    let tag = reader.read_u1()?;
    let kind = match tag {
        0 => VerificationTypeInfo::Top,
        1 => VerificationTypeInfo::Integer,
        2 => VerificationTypeInfo::Float,
        3 => VerificationTypeInfo::Double,
        4 => VerificationTypeInfo::Long,
        5 => VerificationTypeInfo::Null,
        6 => VerificationTypeInfo::UninitializedThis,
        7 => VerificationTypeInfo::Object {
            cpool_index: reader.read_u2()?,
        },
        8 => VerificationTypeInfo::Uninitialized {
            offset: reader.read_u2()?,
        },
        _ => {
            return Err(ClassReadError::InvalidAttribute(
                "StackMapTable".to_string(),
            ));
        }
    };
    Ok(kind)
}

fn cp_utf8(cp: &[CpInfo], index: u16) -> Result<&str, ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::Utf8(value)) => Ok(value.as_str()),
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn cp_class_name(cp: &[CpInfo], index: u16) -> Result<&str, ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::Class { name_index }) => cp_utf8(cp, *name_index),
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn cp_name_and_type(cp: &[CpInfo], index: u16) -> Result<(&str, &str), ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::NameAndType {
            name_index,
            descriptor_index,
        }) => Ok((cp_utf8(cp, *name_index)?, cp_utf8(cp, *descriptor_index)?)),
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn cp_field_ref(cp: &[CpInfo], index: u16) -> Result<(&str, &str, &str), ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::Fieldref {
            class_index,
            name_and_type_index,
        }) => {
            let owner = cp_class_name(cp, *class_index)?;
            let (name, desc) = cp_name_and_type(cp, *name_and_type_index)?;
            Ok((owner, name, desc))
        }
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn cp_method_ref(cp: &[CpInfo], index: u16) -> Result<(&str, &str, &str, bool), ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::Methodref {
            class_index,
            name_and_type_index,
        }) => {
            let owner = cp_class_name(cp, *class_index)?;
            let (name, desc) = cp_name_and_type(cp, *name_and_type_index)?;
            Ok((owner, name, desc, false))
        }
        Some(CpInfo::InterfaceMethodref {
            class_index,
            name_and_type_index,
        }) => {
            let owner = cp_class_name(cp, *class_index)?;
            let (name, desc) = cp_name_and_type(cp, *name_and_type_index)?;
            Ok((owner, name, desc, true))
        }
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn cp_invoke_dynamic(cp: &[CpInfo], index: u16) -> Result<(&str, &str), ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::InvokeDynamic {
            name_and_type_index,
            ..
        }) => cp_name_and_type(cp, *name_and_type_index),
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn cp_ldc_constant(cp: &[CpInfo], index: u16) -> Result<LdcConstant, ClassReadError> {
    match cp.get(index as usize) {
        Some(CpInfo::Integer(value)) => Ok(LdcConstant::Integer(*value)),
        Some(CpInfo::Float(value)) => Ok(LdcConstant::Float(*value)),
        Some(CpInfo::Long(value)) => Ok(LdcConstant::Long(*value)),
        Some(CpInfo::Double(value)) => Ok(LdcConstant::Double(*value)),
        Some(CpInfo::String { string_index }) => {
            Ok(LdcConstant::String(cp_utf8(cp, *string_index)?.to_string()))
        }
        Some(CpInfo::Class { name_index }) => {
            Ok(LdcConstant::Class(cp_utf8(cp, *name_index)?.to_string()))
        }
        Some(CpInfo::MethodType { descriptor_index }) => Ok(LdcConstant::MethodType(
            cp_utf8(cp, *descriptor_index)?.to_string(),
        )),
        Some(CpInfo::MethodHandle {
            reference_kind,
            reference_index,
        }) => Ok(LdcConstant::MethodHandle {
            reference_kind: *reference_kind,
            reference_index: *reference_index,
        }),
        Some(CpInfo::Dynamic { .. }) => Ok(LdcConstant::Dynamic),
        _ => Err(ClassReadError::InvalidIndex(index)),
    }
}

fn decode_modified_utf8(bytes: &[u8]) -> Result<String, ClassReadError> {
    let mut code_units = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        let byte = bytes[i];
        if byte & 0x80 == 0 {
            code_units.push(byte as u16);
            i += 1;
        } else if byte & 0xE0 == 0xC0 {
            if i + 1 >= bytes.len() {
                return Err(ClassReadError::Utf8Error("truncated 2-byte".to_string()));
            }
            let byte2 = bytes[i + 1];
            if byte2 & 0xC0 != 0x80 {
                return Err(ClassReadError::Utf8Error("invalid 2-byte".to_string()));
            }
            let value = (((byte & 0x1F) as u16) << 6) | ((byte2 & 0x3F) as u16);
            code_units.push(value);
            i += 2;
        } else if byte & 0xF0 == 0xE0 {
            if i + 2 >= bytes.len() {
                return Err(ClassReadError::Utf8Error("truncated 3-byte".to_string()));
            }
            let byte2 = bytes[i + 1];
            let byte3 = bytes[i + 2];
            if byte2 & 0xC0 != 0x80 || byte3 & 0xC0 != 0x80 {
                return Err(ClassReadError::Utf8Error("invalid 3-byte".to_string()));
            }
            let value = (((byte & 0x0F) as u16) << 12)
                | (((byte2 & 0x3F) as u16) << 6)
                | ((byte3 & 0x3F) as u16);
            code_units.push(value);
            i += 3;
        } else {
            return Err(ClassReadError::Utf8Error(
                "invalid leading byte".to_string(),
            ));
        }
    }

    String::from_utf16(&code_units)
        .map_err(|_| ClassReadError::Utf8Error("invalid utf16".to_string()))
}

fn parse_code_instructions(code: &[u8]) -> Result<Vec<Insn>, ClassReadError> {
    let mut reader = ByteReader::new(code);
    let mut insns = Vec::new();

    while reader.remaining() > 0 {
        let opcode_offset = reader.pos();
        let opcode = reader.read_u1()?;
        let insn = match opcode {
            opcodes::NOP..=opcodes::DCONST_1 => Insn::Simple(opcode.into()),
            opcodes::BIPUSH => Insn::Int(IntInsnNode {
                insn: opcode.into(),
                operand: reader.read_i1()? as i32,
            }),
            opcodes::SIPUSH => Insn::Int(IntInsnNode {
                insn: opcode.into(),
                operand: reader.read_i2()? as i32,
            }),
            opcodes::LDC => Insn::Ldc(LdcInsnNode {
                insn: opcode.into(),
                value: LdcValue::Index(reader.read_u1()? as u16),
            }),
            opcodes::LDC_W | opcodes::LDC2_W => Insn::Ldc(LdcInsnNode {
                insn: opcode.into(),
                value: LdcValue::Index(reader.read_u2()?),
            }),
            opcodes::ILOAD..=opcodes::ALOAD => Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
            }),
            opcodes::ILOAD_0..=opcodes::SALOAD => Insn::Simple(opcode.into()),
            opcodes::ISTORE..=opcodes::ASTORE => Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
            }),
            opcodes::ISTORE_0..=opcodes::SASTORE => Insn::Simple(opcode.into()),
            opcodes::POP..=opcodes::LXOR => Insn::Simple(opcode.into()),
            opcodes::IINC => Insn::Iinc(IincInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
                increment: reader.read_i1()? as i16,
            }),
            opcodes::I2L..=opcodes::DCMPG => Insn::Simple(opcode.into()),
            opcodes::IFEQ..=opcodes::JSR => Insn::Jump(JumpInsnNode {
                insn: opcode.into(),
                offset: reader.read_i2()? as i32,
            }),
            opcodes::RET => Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
            }),
            opcodes::TABLESWITCH => read_table_switch(&mut reader, opcode_offset)?,
            opcodes::LOOKUPSWITCH => read_lookup_switch(&mut reader, opcode_offset)?,
            opcodes::IRETURN..=opcodes::RETURN => Insn::Simple(InsnNode { opcode }),
            opcodes::GETSTATIC..=opcodes::PUTFIELD => Insn::Field(FieldInsnNode {
                insn: opcode.into(),
                field_ref: MemberRef::Index(reader.read_u2()?),
            }),
            opcodes::INVOKEVIRTUAL..=opcodes::INVOKESTATIC => Insn::Method(MethodInsnNode {
                insn: opcode.into(),
                method_ref: MemberRef::Index(reader.read_u2()?),
            }),
            opcodes::INVOKEINTERFACE => {
                let method_index = reader.read_u2()?;
                let count = reader.read_u1()?;
                let _ = reader.read_u1()?;
                Insn::InvokeInterface(InvokeInterfaceInsnNode {
                    insn: opcode.into(),
                    method_index,
                    count,
                })
            }
            opcodes::INVOKEDYNAMIC => {
                let method_index = reader.read_u2()?;
                let _ = reader.read_u2()?;
                Insn::InvokeDynamic(InvokeDynamicInsnNode::from_index(method_index))
            }
            opcodes::NEW => Insn::Type(TypeInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
            }),
            opcodes::NEWARRAY => Insn::Int(IntInsnNode {
                insn: opcode.into(),
                operand: reader.read_u1()? as i32,
            }),
            opcodes::ANEWARRAY => Insn::Type(TypeInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
            }),
            opcodes::ARRAYLENGTH | opcodes::ATHROW => Insn::Simple(opcode.into()),
            opcodes::CHECKCAST | opcodes::INSTANCEOF => Insn::Type(TypeInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
            }),
            opcodes::MONITORENTER | opcodes::MONITOREXIT => Insn::Simple(opcode.into()),
            opcodes::WIDE => read_wide(&mut reader)?,
            opcodes::MULTIANEWARRAY => Insn::MultiANewArray(MultiANewArrayInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
                dimensions: reader.read_u1()?,
            }),
            opcodes::IFNULL | opcodes::IFNONNULL => Insn::Jump(JumpInsnNode {
                insn: opcode.into(),
                offset: reader.read_i2()? as i32,
            }),
            opcodes::GOTO_W | opcodes::JSR_W => Insn::Jump(JumpInsnNode {
                insn: opcode.into(),
                offset: reader.read_i4()?,
            }),
            opcodes::BREAKPOINT => Insn::Simple(opcode.into()),
            opcodes::IMPDEP1 | opcodes::IMPDEP2 => Insn::Simple(opcode.into()),
            _ => {
                return Err(ClassReadError::InvalidOpcode {
                    opcode,
                    offset: opcode_offset,
                });
            }
        };

        insns.push(insn);
    }

    Ok(insns)
}

pub(crate) fn parse_code_instructions_public(code: &[u8]) -> Result<Vec<Insn>, ClassReadError> {
    parse_code_instructions(code)
}

#[derive(Debug, Clone)]
struct ParsedInstruction {
    offset: u16,
    insn: Insn,
}

fn parse_code_instructions_with_offsets(
    code: &[u8],
) -> Result<Vec<ParsedInstruction>, ClassReadError> {
    let mut reader = ByteReader::new(code);
    let mut insns = Vec::new();

    while reader.remaining() > 0 {
        let opcode_offset = reader.pos();
        let opcode = reader.read_u1()?;
        let insn = match opcode {
            opcodes::NOP..=opcodes::DCONST_1 => Insn::Simple(opcode.into()),
            opcodes::BIPUSH => Insn::Int(IntInsnNode {
                insn: opcode.into(),
                operand: reader.read_i1()? as i32,
            }),
            opcodes::SIPUSH => Insn::Int(IntInsnNode {
                insn: opcode.into(),
                operand: reader.read_i2()? as i32,
            }),
            opcodes::LDC => Insn::Ldc(LdcInsnNode {
                insn: opcode.into(),
                value: LdcValue::Index(reader.read_u1()? as u16),
            }),
            opcodes::LDC_W | opcodes::LDC2_W => Insn::Ldc(LdcInsnNode {
                insn: opcode.into(),
                value: LdcValue::Index(reader.read_u2()?),
            }),
            opcodes::ILOAD..=opcodes::ALOAD => Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
            }),
            opcodes::ILOAD_0..=opcodes::SALOAD => Insn::Simple(opcode.into()),
            opcodes::ISTORE..=opcodes::ASTORE => Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
            }),
            opcodes::ISTORE_0..=opcodes::SASTORE => Insn::Simple(opcode.into()),
            opcodes::POP..=opcodes::LXOR => Insn::Simple(opcode.into()),
            opcodes::IINC => Insn::Iinc(IincInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
                increment: reader.read_i1()? as i16,
            }),
            opcodes::I2L..=opcodes::DCMPG => Insn::Simple(opcode.into()),
            opcodes::IFEQ..=opcodes::JSR => Insn::Jump(JumpInsnNode {
                insn: opcode.into(),
                offset: reader.read_i2()? as i32,
            }),
            opcodes::RET => Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u1()? as u16,
            }),
            opcodes::TABLESWITCH => read_table_switch(&mut reader, opcode_offset)?,
            opcodes::LOOKUPSWITCH => read_lookup_switch(&mut reader, opcode_offset)?,
            opcodes::IRETURN..=opcodes::RETURN => Insn::Simple(opcode.into()),
            opcodes::GETSTATIC..=opcodes::PUTFIELD => Insn::Field(FieldInsnNode {
                insn: opcode.into(),
                field_ref: MemberRef::Index(reader.read_u2()?),
            }),
            opcodes::INVOKEVIRTUAL..=opcodes::INVOKESTATIC => Insn::Method(MethodInsnNode {
                insn: opcode.into(),
                method_ref: MemberRef::Index(reader.read_u2()?),
            }),
            opcodes::INVOKEINTERFACE => {
                let method_index = reader.read_u2()?;
                let count = reader.read_u1()?;
                let _ = reader.read_u1()?;
                Insn::InvokeInterface(InvokeInterfaceInsnNode {
                    insn: opcode.into(),
                    method_index,
                    count,
                })
            }
            opcodes::INVOKEDYNAMIC => {
                let method_index = reader.read_u2()?;
                let _ = reader.read_u2()?;
                Insn::InvokeDynamic(InvokeDynamicInsnNode::from_index(method_index))
            }
            opcodes::NEW => Insn::Type(TypeInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
            }),
            opcodes::NEWARRAY => Insn::Int(IntInsnNode {
                insn: opcode.into(),
                operand: reader.read_u1()? as i32,
            }),
            opcodes::ANEWARRAY => Insn::Type(TypeInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
            }),
            opcodes::ARRAYLENGTH | opcodes::ATHROW => Insn::Simple(opcode.into()),
            opcodes::CHECKCAST | opcodes::INSTANCEOF => Insn::Type(TypeInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
            }),
            opcodes::MONITORENTER | opcodes::MONITOREXIT => Insn::Simple(opcode.into()),
            opcodes::WIDE => read_wide(&mut reader)?,
            opcodes::MULTIANEWARRAY => Insn::MultiANewArray(MultiANewArrayInsnNode {
                insn: opcode.into(),
                type_index: reader.read_u2()?,
                dimensions: reader.read_u1()?,
            }),
            opcodes::IFNULL | opcodes::IFNONNULL => Insn::Jump(JumpInsnNode {
                insn: opcode.into(),
                offset: reader.read_i2()? as i32,
            }),
            opcodes::GOTO_W | opcodes::JSR_W => Insn::Jump(JumpInsnNode {
                insn: opcode.into(),
                offset: reader.read_i4()?,
            }),
            opcodes::BREAKPOINT => Insn::Simple(opcode.into()),
            opcodes::IMPDEP1 | opcodes::IMPDEP2 => Insn::Simple(opcode.into()),
            _ => {
                return Err(ClassReadError::InvalidOpcode {
                    opcode,
                    offset: opcode_offset,
                });
            }
        };

        insns.push(ParsedInstruction {
            offset: opcode_offset as u16,
            insn,
        });
    }

    Ok(insns)
}

fn build_insn_nodes(
    code: &[u8],
    exception_table: &[ExceptionTableEntry],
    cp: &[CpInfo],
) -> Result<
    (
        Vec<AbstractInsnNode>,
        Vec<TryCatchBlockNode>,
        std::collections::HashMap<u16, LabelNode>,
    ),
    ClassReadError,
> {
    let instructions = parse_code_instructions_with_offsets(code)?;
    let mut offsets = std::collections::HashSet::new();
    for instruction in &instructions {
        offsets.insert(instruction.offset);
        match &instruction.insn {
            Insn::Jump(node) => {
                offsets.insert((instruction.offset as i32 + node.offset) as u16);
            }
            Insn::TableSwitch(node) => {
                offsets.insert((instruction.offset as i32 + node.default_offset) as u16);
                for offset in &node.offsets {
                    offsets.insert((instruction.offset as i32 + *offset) as u16);
                }
            }
            Insn::LookupSwitch(node) => {
                offsets.insert((instruction.offset as i32 + node.default_offset) as u16);
                for (_, offset) in &node.pairs {
                    offsets.insert((instruction.offset as i32 + *offset) as u16);
                }
            }
            _ => {}
        }
    }
    for entry in exception_table {
        offsets.insert(entry.start_pc);
        offsets.insert(entry.end_pc);
        offsets.insert(entry.handler_pc);
    }
    offsets.insert(code.len() as u16);

    let mut label_by_offset = std::collections::HashMap::new();
    for (next_id, offset) in offsets.into_iter().enumerate() {
        label_by_offset.insert(offset, LabelNode { id: next_id });
    }

    let mut nodes = Vec::new();
    for instruction in instructions {
        if let Some(label) = label_by_offset.get(&{ instruction.offset }) {
            nodes.push(AbstractInsnNode::Label(*label));
        }
        nodes.push(AbstractInsnNode::Insn(instruction.insn));
    }
    if let Some(label) = label_by_offset.get(&(code.len() as u16)) {
        nodes.push(AbstractInsnNode::Label(*label));
    }

    let mut try_catch_blocks = Vec::new();
    for entry in exception_table {
        let start = *label_by_offset
            .get(&entry.start_pc)
            .ok_or_else(|| ClassReadError::InvalidAttribute("missing start label".to_string()))?;
        let end = *label_by_offset
            .get(&entry.end_pc)
            .ok_or_else(|| ClassReadError::InvalidAttribute("missing end label".to_string()))?;
        let handler = *label_by_offset
            .get(&entry.handler_pc)
            .ok_or_else(|| ClassReadError::InvalidAttribute("missing handler label".to_string()))?;
        let catch_type = if entry.catch_type == 0 {
            None
        } else {
            Some(cp_class_name(cp, entry.catch_type)?.to_string())
        };
        try_catch_blocks.push(TryCatchBlockNode {
            start,
            end,
            handler,
            catch_type,
        });
    }

    Ok((nodes, try_catch_blocks, label_by_offset))
}

pub(crate) fn build_insn_nodes_public(
    code: &[u8],
    exception_table: &[ExceptionTableEntry],
    cp: &[CpInfo],
) -> Result<(Vec<AbstractInsnNode>, Vec<TryCatchBlockNode>), ClassReadError> {
    let (nodes, try_catch_blocks, _) = build_insn_nodes(code, exception_table, cp)?;
    Ok((nodes, try_catch_blocks))
}

fn attach_line_numbers(
    nodes: Vec<AbstractInsnNode>,
    entries: &[LineNumber],
    label_by_offset: &std::collections::HashMap<u16, LabelNode>,
) -> Vec<AbstractInsnNode> {
    let mut lines_by_label = std::collections::HashMap::<usize, Vec<LineNumberInsnNode>>::new();
    for entry in entries {
        if let Some(label) = label_by_offset.get(&entry.start_pc) {
            lines_by_label
                .entry(label.id)
                .or_default()
                .push(LineNumberInsnNode {
                    line: entry.line_number,
                    start: *label,
                });
        }
    }

    let mut merged = Vec::with_capacity(nodes.len() + entries.len());
    for node in nodes {
        let label_id = match &node {
            AbstractInsnNode::Label(label) => Some(label.id),
            _ => None,
        };
        merged.push(node);
        if let Some(label_id) = label_id
            && let Some(lines) = lines_by_label.remove(&label_id)
        {
            for line in lines {
                merged.push(AbstractInsnNode::LineNumber(line));
            }
        }
    }
    merged
}

fn read_table_switch(
    reader: &mut ByteReader<'_>,
    opcode_offset: usize,
) -> Result<Insn, ClassReadError> {
    reader.align4(opcode_offset)?;
    let default_offset = reader.read_i4()?;
    let low = reader.read_i4()?;
    let high = reader.read_i4()?;
    let count = if high < low {
        0
    } else {
        (high - low + 1) as usize
    };
    let mut offsets = Vec::with_capacity(count);
    for _ in 0..count {
        offsets.push(reader.read_i4()?);
    }
    Ok(Insn::TableSwitch(TableSwitchInsnNode {
        insn: opcodes::TABLESWITCH.into(),
        default_offset,
        low,
        high,
        offsets,
    }))
}

fn read_lookup_switch(
    reader: &mut ByteReader<'_>,
    opcode_offset: usize,
) -> Result<Insn, ClassReadError> {
    reader.align4(opcode_offset)?;
    let default_offset = reader.read_i4()?;
    let npairs = reader.read_i4()? as usize;
    let mut pairs = Vec::with_capacity(npairs);
    for _ in 0..npairs {
        let key = reader.read_i4()?;
        let offset = reader.read_i4()?;
        pairs.push((key, offset));
    }
    Ok(Insn::LookupSwitch(LookupSwitchInsnNode {
        insn: opcodes::LOOKUPSWITCH.into(),
        default_offset,
        pairs,
    }))
}

fn read_wide(reader: &mut ByteReader<'_>) -> Result<Insn, ClassReadError> {
    let opcode = reader.read_u1()?;
    match opcode {
        opcodes::ILOAD..=opcodes::ALOAD | opcodes::ISTORE..=opcodes::ASTORE | opcodes::RET => {
            Ok(Insn::Var(VarInsnNode {
                insn: opcode.into(),
                var_index: reader.read_u2()?,
            }))
        }
        opcodes::IINC => Ok(Insn::Iinc(IincInsnNode {
            insn: opcode.into(),
            var_index: reader.read_u2()?,
            increment: reader.read_i2()?,
        })),
        _ => Err(ClassReadError::InvalidOpcode {
            opcode,
            offset: reader.pos().saturating_sub(1),
        }),
    }
}

fn visit_instruction(
    cp: &[CpInfo],
    offset: i32,
    insn: Insn,
    mv: &mut dyn MethodVisitor,
) -> Result<(), ClassReadError> {
    match insn {
        Insn::Simple(node) => {
            mv.visit_insn(node.opcode);
        }
        Insn::Int(node) => {
            mv.visit_int_insn(node.insn.opcode, node.operand);
        }
        Insn::Var(node) => {
            mv.visit_var_insn(node.insn.opcode, node.var_index);
        }
        Insn::Type(node) => {
            let type_name = cp_class_name(cp, node.type_index)?;
            mv.visit_type_insn(node.insn.opcode, type_name);
        }
        Insn::Field(node) => {
            let index = match node.field_ref {
                MemberRef::Index(index) => index,
                MemberRef::Symbolic { .. } => {
                    return Err(ClassReadError::InvalidIndex(0));
                }
            };
            let (owner, name, desc) = cp_field_ref(cp, index)?;
            mv.visit_field_insn(node.insn.opcode, owner, name, desc);
        }
        Insn::Method(node) => {
            let index = match node.method_ref {
                MemberRef::Index(index) => index,
                MemberRef::Symbolic { .. } => {
                    return Err(ClassReadError::InvalidIndex(0));
                }
            };
            let (owner, name, desc, is_interface) = cp_method_ref(cp, index)?;
            mv.visit_method_insn(node.insn.opcode, owner, name, desc, is_interface);
        }
        Insn::InvokeInterface(node) => {
            let (owner, name, desc, _is_interface) = cp_method_ref(cp, node.method_index)?;
            mv.visit_method_insn(node.insn.opcode, owner, name, desc, true);
        }
        Insn::InvokeDynamic(node) => {
            let (name, desc) = cp_invoke_dynamic(cp, node.method_index)?;
            mv.visit_invoke_dynamic_insn(name, desc);
        }
        Insn::Jump(node) => {
            let target = offset + node.offset;
            mv.visit_jump_insn(node.insn.opcode, target);
        }
        Insn::Ldc(node) => {
            let index = match node.value {
                LdcValue::Index(index) => index,
                LdcValue::String(value) => {
                    mv.visit_ldc_insn(LdcConstant::String(value));
                    return Ok(());
                }
                LdcValue::Type(value) => {
                    match value.clone() {
                        Type::Method { .. } => {
                            mv.visit_ldc_insn(LdcConstant::MethodType(value.get_descriptor()));
                        }
                        _ => {
                            mv.visit_ldc_insn(LdcConstant::Class(value.get_descriptor()));
                        }
                    }

                    return Ok(());
                }
                LdcValue::Int(value) => {
                    mv.visit_ldc_insn(LdcConstant::Integer(value));
                    return Ok(());
                }
                LdcValue::Float(value) => {
                    mv.visit_ldc_insn(LdcConstant::Float(value));
                    return Ok(());
                }
                LdcValue::Long(value) => {
                    mv.visit_ldc_insn(LdcConstant::Long(value));
                    return Ok(());
                }
                LdcValue::Double(value) => {
                    mv.visit_ldc_insn(LdcConstant::Double(value));
                    return Ok(());
                }
            };
            let constant = cp_ldc_constant(cp, index)?;
            mv.visit_ldc_insn(constant);
        }
        Insn::Iinc(node) => {
            mv.visit_iinc_insn(node.var_index, node.increment);
        }
        Insn::TableSwitch(node) => {
            let targets = node
                .offsets
                .iter()
                .map(|value| offset + *value)
                .collect::<Vec<_>>();
            mv.visit_table_switch(offset + node.default_offset, node.low, node.high, &targets);
        }
        Insn::LookupSwitch(node) => {
            let pairs = node
                .pairs
                .iter()
                .map(|(key, value)| (*key, offset + *value))
                .collect::<Vec<_>>();
            mv.visit_lookup_switch(offset + node.default_offset, &pairs);
        }
        Insn::MultiANewArray(node) => {
            let type_name = cp_class_name(cp, node.type_index)?;
            mv.visit_multi_anewarray_insn(type_name, node.dimensions);
        }
    }
    Ok(())
}

pub struct ByteReader<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> ByteReader<'a> {
    pub fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    pub fn remaining(&self) -> usize {
        self.data.len().saturating_sub(self.pos)
    }

    pub fn pos(&self) -> usize {
        self.pos
    }

    pub fn align4(&mut self, opcode_offset: usize) -> Result<(), ClassReadError> {
        let mut padding = (4 - ((opcode_offset + 1) % 4)) % 4;
        while padding > 0 {
            self.read_u1()?;
            padding -= 1;
        }
        Ok(())
    }

    pub fn read_u1(&mut self) -> Result<u8, ClassReadError> {
        if self.pos >= self.data.len() {
            return Err(ClassReadError::UnexpectedEof);
        }
        let value = self.data[self.pos];
        self.pos += 1;
        Ok(value)
    }

    pub fn read_i1(&mut self) -> Result<i8, ClassReadError> {
        Ok(self.read_u1()? as i8)
    }

    pub fn read_u2(&mut self) -> Result<u16, ClassReadError> {
        let bytes = self.read_bytes(2)?;
        Ok(u16::from_be_bytes([bytes[0], bytes[1]]))
    }

    pub fn read_i2(&mut self) -> Result<i16, ClassReadError> {
        Ok(self.read_u2()? as i16)
    }

    pub fn read_u4(&mut self) -> Result<u32, ClassReadError> {
        let bytes = self.read_bytes(4)?;
        Ok(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }

    pub fn read_i4(&mut self) -> Result<i32, ClassReadError> {
        let bytes = self.read_bytes(4)?;
        Ok(i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }

    pub fn read_u8(&mut self) -> Result<u64, ClassReadError> {
        let bytes = self.read_bytes(8)?;
        Ok(u64::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
        ]))
    }

    pub fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>, ClassReadError> {
        if self.pos + len > self.data.len() {
            return Err(ClassReadError::UnexpectedEof);
        }
        let bytes = self.data[self.pos..self.pos + len].to_vec();
        self.pos += len;
        Ok(bytes)
    }
}

#[cfg(test)]
mod tests {
    use crate::class_writer::ClassWriter;
    use crate::constants::*;
    use crate::insn::{Label, LabelNode};
    use crate::opcodes;

    use super::*;
    use std::cell::RefCell;
    use std::rc::Rc;

    // A mock visitor to capture parsing results
    struct MockClassVisitor {
        pub visited_name: Rc<RefCell<Option<String>>>,
        pub visited_methods: Rc<RefCell<Vec<String>>>,
    }

    impl MockClassVisitor {
        fn new() -> Self {
            Self {
                visited_name: Rc::new(RefCell::new(None)),
                visited_methods: Rc::new(RefCell::new(Vec::new())),
            }
        }
    }

    impl ClassVisitor for MockClassVisitor {
        fn visit(
            &mut self,
            _major: u16,
            _minor: u16,
            _access_flags: u16,
            name: &str,
            _super_name: Option<&str>,
            _interfaces: &[String],
        ) {
            *self.visited_name.borrow_mut() = Some(name.to_string());
        }

        fn visit_method(
            &mut self,
            _access_flags: u16,
            name: &str,
            _descriptor: &str,
        ) -> Option<Box<dyn MethodVisitor>> {
            self.visited_methods.borrow_mut().push(name.to_string());
            None
        }
    }

    /// Helper to generate a minimal valid class file byte array (Java 8).
    /// Class Name: "TestClass"
    fn generate_minimal_class() -> Vec<u8> {
        let mut w = Vec::new();
        // Magic
        w.extend_from_slice(&0xCAFEBABE_u32.to_be_bytes());
        // Version (Java 8 = 52.0)
        w.extend_from_slice(&0_u16.to_be_bytes()); // minor
        w.extend_from_slice(&52_u16.to_be_bytes()); // major

        // Constant Pool (Count: 5)
        // 1: UTF8 "TestClass"
        // 2: Class #1
        // 3: UTF8 "java/lang/Object"
        // 4: Class #3
        w.extend_from_slice(&5_u16.to_be_bytes()); // Count (N+1)

        // #1 UTF8
        w.push(1);
        let name = "TestClass";
        w.extend_from_slice(&(name.len() as u16).to_be_bytes());
        w.extend_from_slice(name.as_bytes());

        // #2 Class
        w.push(7);
        w.extend_from_slice(&1_u16.to_be_bytes());

        // #3 UTF8
        w.push(1);
        let obj = "java/lang/Object";
        w.extend_from_slice(&(obj.len() as u16).to_be_bytes());
        w.extend_from_slice(obj.as_bytes());

        // #4 Class
        w.push(7);
        w.extend_from_slice(&3_u16.to_be_bytes());

        // Access Flags (PUBLIC)
        w.extend_from_slice(&0x0021_u16.to_be_bytes());
        // This Class (#2)
        w.extend_from_slice(&2_u16.to_be_bytes());
        // Super Class (#4)
        w.extend_from_slice(&4_u16.to_be_bytes());

        // Interfaces Count
        w.extend_from_slice(&0_u16.to_be_bytes());
        // Fields Count
        w.extend_from_slice(&0_u16.to_be_bytes());
        // Methods Count
        w.extend_from_slice(&0_u16.to_be_bytes());
        // Attributes Count
        w.extend_from_slice(&0_u16.to_be_bytes());

        w
    }

    fn generate_module_info_class() -> Vec<u8> {
        let mut writer = ClassWriter::new(0);
        writer.visit(V9, 0, ACC_MODULE, "module-info", None, &[]);

        let mut module = writer.visit_module("com.example.app", ACC_OPEN, Some("1.0"));
        module.visit_main_class("com/example/app/Main");
        module.visit_package("com/example/api");
        module.visit_package("com/example/internal");
        module.visit_require("java.base", ACC_MANDATED, None);
        module.visit_require(
            "com.example.lib",
            ACC_TRANSITIVE | ACC_STATIC_PHASE,
            Some("2.1"),
        );
        module.visit_export("com/example/api", 0, &["com.example.consumer"]);
        module.visit_open("com/example/internal", 0, &["com.example.runtime"]);
        module.visit_use("com/example/spi/Service");
        module.visit_provide("com/example/spi/Service", &["com/example/impl/ServiceImpl"]);
        module.visit_end(&mut writer);

        writer.to_bytes().expect("module-info should encode")
    }

    #[test]
    fn test_class_reader_header() {
        let bytes = generate_minimal_class();
        let reader = ClassReader::new(&bytes);
        let mut visitor = MockClassVisitor::new();

        let result = reader.accept(&mut visitor, 0);

        assert!(result.is_ok(), "Should parse valid class file");
        assert_eq!(
            *visitor.visited_name.borrow(),
            Some("TestClass".to_string())
        );
    }

    #[test]
    fn test_invalid_magic() {
        // expected CA FE BA BE
        let bytes = vec![0x00, 0x00, 0x00, 0x00];
        let reader = ClassReader::new(&bytes);
        let mut visitor = MockClassVisitor::new();

        let result = reader.accept(&mut visitor, 0);
        assert!(matches!(result, Err(ClassReadError::InvalidMagic(_))));
    }

    #[test]
    fn test_code_reader_alignment() {
        // Test internal alignment logic for switch instructions
        let data = vec![0x00, 0x00, 0x00, 0x00]; // 4 bytes
        let mut reader = super::ByteReader::new(&data);

        // If we are at pos 1, padding to 4-byte boundary
        reader.pos = 1;
        // 1 -> align 4 -> skips 3 bytes -> pos 4
        assert!(reader.align4(0).is_ok());
        assert_eq!(reader.pos(), 4);
    }

    #[test]
    fn test_parse_runtime_visible_type_annotations_supertype() {
        // RuntimeVisibleTypeAnnotations:
        // u2 num_annotations = 1
        // type_annotation:
        //   u1 target_type = TA_TARGET_CLASS_EXTENDS
        //   u2 supertype_index = 5
        //   type_path: u1 path_len=0
        //   annotation:
        //     u2 type_descriptor_index=10
        //     u2 num_pairs=0
        let mut info = vec![];
        u2(1, &mut info);
        u1(TA_TARGET_CLASS_EXTENDS, &mut info);
        u2(5, &mut info);
        u1(0, &mut info);
        u2(10, &mut info);
        u2(0, &mut info);

        let cp: Vec<CpInfo> = vec![];
        let attr = parse_attribute("RuntimeVisibleTypeAnnotations", info, &cp).unwrap();

        match attr {
            AttributeInfo::RuntimeVisibleTypeAnnotations { annotations } => {
                assert_eq!(annotations.len(), 1);
                let a = &annotations[0];
                assert_eq!(a.target_type, TA_TARGET_CLASS_EXTENDS);
                assert!(matches!(
                    a.target_info,
                    TypeAnnotationTargetInfo::Supertype { supertype_index: 5 }
                ));
                assert_eq!(a.target_path.path.len(), 0);
                assert_eq!(a.annotation.type_descriptor_index, 10);
                assert_eq!(a.annotation.element_value_pairs.len(), 0);
            }
            other => panic!("unexpected attr: {:?}", other),
        }
    }

    #[test]
    fn test_parse_runtime_visible_type_annotations_formal_parameter_with_path() {
        // target_type = TA_TARGET_METHOD_FORMAL_PARAMETER
        // u1 formal_parameter_index = 2
        // type_path len=1 entry(kind=TA_TYPE_PATH_ARRAY, arg_index=0)
        // annotation: type_index=9, num_pairs=0
        let mut info = vec![];
        u2(1, &mut info);
        u1(TA_TARGET_METHOD_FORMAL_PARAMETER, &mut info);
        u1(2, &mut info);

        u1(1, &mut info); // path_length
        u1(TA_TYPE_PATH_ARRAY, &mut info);
        u1(0, &mut info);

        u2(9, &mut info);
        u2(0, &mut info);

        let cp: Vec<CpInfo> = vec![];
        let attr = parse_attribute("RuntimeVisibleTypeAnnotations", info, &cp).unwrap();

        match attr {
            AttributeInfo::RuntimeVisibleTypeAnnotations { annotations } => {
                let a = &annotations[0];
                assert_eq!(a.target_type, TA_TARGET_METHOD_FORMAL_PARAMETER);
                assert!(matches!(
                    a.target_info,
                    TypeAnnotationTargetInfo::FormalParameter {
                        formal_parameter_index: 2
                    }
                ));
                assert_eq!(a.target_path.path.len(), 1);
                assert_eq!(a.target_path.path[0].type_path_kind, TA_TYPE_PATH_ARRAY);
                assert_eq!(a.target_path.path[0].type_argument_index, 0);
            }
            other => panic!("unexpected attr: {:?}", other),
        }
    }

    #[test]
    fn test_parse_runtime_visible_type_annotations_localvar_table() {
        // target_type = TA_TARGET_LOCAL_VARIABLE
        // u2 table_length = 1
        // entry: start_pc=1 length=2 index=3
        // type_path len=0
        // annotation: type_index=8, num_pairs=0
        let mut info = vec![];
        u2(1, &mut info);
        u1(TA_TARGET_LOCAL_VARIABLE, &mut info);

        u2(1, &mut info); // table_length
        u2(1, &mut info);
        u2(2, &mut info);
        u2(3, &mut info);

        u1(0, &mut info); // path_length
        u2(8, &mut info);
        u2(0, &mut info);

        let cp: Vec<CpInfo> = vec![];
        let attr = parse_attribute("RuntimeVisibleTypeAnnotations", info, &cp).unwrap();

        match attr {
            AttributeInfo::RuntimeVisibleTypeAnnotations { annotations } => {
                let a = &annotations[0];
                assert_eq!(a.target_type, TA_TARGET_LOCAL_VARIABLE);
                match &a.target_info {
                    TypeAnnotationTargetInfo::LocalVar { table } => {
                        assert_eq!(table.len(), 1);
                        assert_eq!(table[0].start_pc, 1);
                        assert_eq!(table[0].length, 2);
                        assert_eq!(table[0].index, 3);
                    }
                    other => panic!("unexpected target_info: {:?}", other),
                }
            }
            other => panic!("unexpected attr: {:?}", other),
        }
    }

    #[test]
    fn test_parse_module_attribute_family() {
        let mut module_info = vec![];
        u2(1, &mut module_info);
        u2(ACC_OPEN, &mut module_info);
        u2(2, &mut module_info);
        u2(1, &mut module_info);
        u2(3, &mut module_info);
        u2(ACC_TRANSITIVE | ACC_STATIC_PHASE, &mut module_info);
        u2(4, &mut module_info);
        u2(1, &mut module_info);
        u2(5, &mut module_info);
        u2(ACC_MANDATED, &mut module_info);
        u2(2, &mut module_info);
        u2(6, &mut module_info);
        u2(7, &mut module_info);
        u2(1, &mut module_info);
        u2(8, &mut module_info);
        u2(0, &mut module_info);
        u2(1, &mut module_info);
        u2(9, &mut module_info);
        u2(1, &mut module_info);
        u2(10, &mut module_info);
        u2(1, &mut module_info);
        u2(11, &mut module_info);
        u2(2, &mut module_info);
        u2(12, &mut module_info);
        u2(13, &mut module_info);

        let attr = parse_attribute("Module", module_info, &[]).expect("module attr should parse");
        match attr {
            AttributeInfo::Module(module) => {
                assert_eq!(module.module_name_index, 1);
                assert_eq!(module.module_flags, ACC_OPEN);
                assert_eq!(module.module_version_index, 2);
                assert_eq!(
                    module.requires,
                    vec![ModuleRequire {
                        requires_index: 3,
                        requires_flags: ACC_TRANSITIVE | ACC_STATIC_PHASE,
                        requires_version_index: 4,
                    }]
                );
                assert_eq!(
                    module.exports,
                    vec![ModuleExport {
                        exports_index: 5,
                        exports_flags: ACC_MANDATED,
                        exports_to_index: vec![6, 7],
                    }]
                );
                assert_eq!(
                    module.opens,
                    vec![ModuleOpen {
                        opens_index: 8,
                        opens_flags: 0,
                        opens_to_index: vec![9],
                    }]
                );
                assert_eq!(module.uses_index, vec![10]);
                assert_eq!(
                    module.provides,
                    vec![ModuleProvide {
                        provides_index: 11,
                        provides_with_index: vec![12, 13],
                    }]
                );
            }
            other => panic!("unexpected attr: {:?}", other),
        }

        let mut packages_info = vec![];
        u2(2, &mut packages_info);
        u2(21, &mut packages_info);
        u2(22, &mut packages_info);
        let attr = parse_attribute("ModulePackages", packages_info, &[])
            .expect("module packages attr should parse");
        match attr {
            AttributeInfo::ModulePackages {
                package_index_table,
            } => {
                assert_eq!(package_index_table, vec![21, 22]);
            }
            other => panic!("unexpected attr: {:?}", other),
        }

        let mut main_class_info = vec![];
        u2(23, &mut main_class_info);
        let attr = parse_attribute("ModuleMainClass", main_class_info, &[])
            .expect("module main class attr should parse");
        match attr {
            AttributeInfo::ModuleMainClass { main_class_index } => {
                assert_eq!(main_class_index, 23);
            }
            other => panic!("unexpected attr: {:?}", other),
        }
    }

    fn u1(v: u8, out: &mut Vec<u8>) {
        out.push(v);
    }
    fn u2(v: u16, out: &mut Vec<u8>) {
        out.extend_from_slice(&v.to_be_bytes());
    }

    #[test]
    fn test_method_node_contains_offsets_and_line_numbers() {
        let mut writer = ClassWriter::new(0);
        writer.visit(
            52,
            0,
            ACC_PUBLIC,
            "TestNodeData",
            Some("java/lang/Object"),
            &[],
        );

        let mut ctor = writer.visit_method(ACC_PUBLIC, "<init>", "()V");
        ctor.visit_code();
        ctor.visit_var_insn(opcodes::ALOAD, 0);
        ctor.visit_method_insn(
            opcodes::INVOKESPECIAL,
            "java/lang/Object",
            "<init>",
            "()V",
            false,
        );
        ctor.visit_insn(opcodes::RETURN);
        ctor.visit_maxs(1, 1);
        ctor.visit_end(&mut writer);

        let mut method = writer.visit_method(ACC_PUBLIC | ACC_STATIC, "answer", "()I");
        let start = Label::new();
        method.visit_code();
        method.visit_label(start);
        method.visit_line_number(123, LabelNode::from_label(start));
        method.visit_insn(opcodes::ICONST_1);
        method.visit_insn(opcodes::IRETURN);
        method.visit_maxs(1, 0);
        method.visit_end(&mut writer);

        let bytes = writer.to_bytes().expect("class should encode");
        let class = ClassReader::new(&bytes)
            .to_class_node()
            .expect("class should decode");
        let method = class
            .methods
            .iter()
            .find(|method| method.name == "answer")
            .expect("method should exist");

        assert_eq!(method.instruction_offsets, vec![0, 1]);
        assert_eq!(method.line_numbers.len(), 1);
        assert_eq!(method.line_numbers[0].line_number, 123);
        assert!(
            method
                .insn_nodes
                .iter()
                .any(|node| matches!(node, AbstractInsnNode::LineNumber(_)))
        );
        assert!(
            method
                .insn_nodes
                .iter()
                .any(|node| matches!(node, AbstractInsnNode::Label(_)))
        );
        assert!(method.try_catch_blocks.is_empty());
    }

    #[test]
    fn test_method_node_contains_try_catch_blocks() {
        let mut writer = ClassWriter::new(0);
        writer.visit(
            52,
            0,
            ACC_PUBLIC,
            "TestTryCatchNode",
            Some("java/lang/Object"),
            &[],
        );

        let mut ctor = writer.visit_method(ACC_PUBLIC, "<init>", "()V");
        ctor.visit_code();
        ctor.visit_var_insn(opcodes::ALOAD, 0);
        ctor.visit_method_insn(
            opcodes::INVOKESPECIAL,
            "java/lang/Object",
            "<init>",
            "()V",
            false,
        );
        ctor.visit_insn(opcodes::RETURN);
        ctor.visit_maxs(1, 1);
        ctor.visit_end(&mut writer);

        let start = Label::new();
        let end = Label::new();
        let handler = Label::new();
        let mut method =
            writer.visit_method(ACC_PUBLIC | ACC_STATIC, "safeLen", "(Ljava/lang/String;)I");
        method.visit_code();
        method.visit_label(start);
        method.visit_var_insn(opcodes::ALOAD, 0);
        method.visit_method_insn(
            opcodes::INVOKEVIRTUAL,
            "java/lang/String",
            "length",
            "()I",
            false,
        );
        method.visit_insn(opcodes::IRETURN);
        method.visit_label(end);
        method.visit_label(handler);
        method.visit_var_insn(opcodes::ASTORE, 1);
        method.visit_insn(opcodes::ICONST_M1);
        method.visit_insn(opcodes::IRETURN);
        method.visit_try_catch_block(start, end, handler, Some("java/lang/RuntimeException"));
        method.visit_maxs(1, 2);
        method.visit_end(&mut writer);

        let bytes = writer.to_bytes().expect("class should encode");
        let class = ClassReader::new(&bytes)
            .to_class_node()
            .expect("class should decode");
        let method = class
            .methods
            .iter()
            .find(|method| method.name == "safeLen")
            .expect("method should exist");

        assert_eq!(method.exception_table.len(), 1);
        assert_eq!(method.try_catch_blocks.len(), 1);
        assert_eq!(
            method.try_catch_blocks[0].catch_type.as_deref(),
            Some("java/lang/RuntimeException")
        );
    }

    #[test]
    fn test_parse_runtime_visible_annotations_one_empty() {
        // u2 num_annotations=1
        // annotation: type=10, pairs=0
        let mut info = vec![];
        u2(1, &mut info);
        u2(10, &mut info);
        u2(0, &mut info);

        let cp: Vec<CpInfo> = vec![];
        let attr = parse_attribute("RuntimeVisibleAnnotations", info, &cp).unwrap();
        match attr {
            AttributeInfo::RuntimeVisibleAnnotations { annotations } => {
                assert_eq!(annotations.len(), 1);
                assert_eq!(annotations[0].type_descriptor_index, 10);
                assert_eq!(annotations[0].element_value_pairs.len(), 0);
            }
            other => panic!("unexpected attr: {:?}", other),
        }
    }

    #[test]
    fn test_parse_runtime_visible_parameter_annotations_two_params() {
        // u1 num_params=2
        // p0: u2 num_ann=1, annotation(type=10,pairs=0)
        // p1: u2 num_ann=0
        let mut info = vec![];
        u1(2, &mut info);
        u2(1, &mut info);
        u2(10, &mut info);
        u2(0, &mut info);
        u2(0, &mut info);

        let cp: Vec<CpInfo> = vec![];
        let attr = parse_attribute("RuntimeVisibleParameterAnnotations", info, &cp).unwrap();
        match attr {
            AttributeInfo::RuntimeVisibleParameterAnnotations { parameters } => {
                assert_eq!(parameters.parameters.len(), 2);
                assert_eq!(parameters.parameters[0].len(), 1);
                assert_eq!(parameters.parameters[1].len(), 0);
            }
            other => panic!("unexpected attr: {:?}", other),
        }
    }

    #[test]
    fn test_class_reader_decodes_module_info_node() {
        let bytes = generate_module_info_class();
        let class = ClassReader::new(&bytes)
            .to_class_node()
            .expect("module-info should decode");

        assert_eq!(class.name, "module-info");
        assert_eq!(class.access_flags, ACC_MODULE);

        let module = class.module.expect("module descriptor should be decoded");
        assert_eq!(module.name, "com.example.app");
        assert_eq!(module.access_flags, ACC_OPEN);
        assert_eq!(module.version.as_deref(), Some("1.0"));
        assert_eq!(module.main_class.as_deref(), Some("com/example/app/Main"));
        assert_eq!(
            module.packages,
            vec![
                "com/example/api".to_string(),
                "com/example/internal".to_string()
            ]
        );
        assert_eq!(module.requires.len(), 2);
        assert_eq!(module.requires[0].module, "java.base");
        assert_eq!(module.requires[0].access_flags, ACC_MANDATED);
        assert_eq!(module.requires[0].version, None);
        assert_eq!(module.requires[1].module, "com.example.lib");
        assert_eq!(
            module.requires[1].access_flags,
            ACC_TRANSITIVE | ACC_STATIC_PHASE
        );
        assert_eq!(module.requires[1].version.as_deref(), Some("2.1"));
        assert_eq!(module.exports.len(), 1);
        assert_eq!(module.exports[0].package, "com/example/api");
        assert_eq!(
            module.exports[0].modules,
            vec!["com.example.consumer".to_string()]
        );
        assert_eq!(module.opens.len(), 1);
        assert_eq!(module.opens[0].package, "com/example/internal");
        assert_eq!(
            module.opens[0].modules,
            vec!["com.example.runtime".to_string()]
        );
        assert_eq!(module.uses, vec!["com/example/spi/Service".to_string()]);
        assert_eq!(module.provides.len(), 1);
        assert_eq!(module.provides[0].service, "com/example/spi/Service");
        assert_eq!(
            module.provides[0].providers,
            vec!["com/example/impl/ServiceImpl".to_string()]
        );
    }
}