go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
//! Java language support.
//!
//! Provides comprehensive Java extraction including:
//! - Classes, interfaces, enums with inheritance chains
//! - Methods with full modifier support (public/private/protected, static, final, etc.)
//! - Constructors
//! - Annotations
//! - Javadoc comments
//! - Generic type parameters
//! - Import statements (regular and static)

use std::collections::HashMap;
use tree_sitter::{Node, Parser, Tree};

use crate::ast::types::{ClassInfo, FieldInfo, FunctionInfo, ImportInfo};
use crate::cfg::types::{BlockId, BlockType, CFGBlock, CFGEdge, CFGInfo};
use crate::dfg::types::{DFGInfo, DataflowEdge, DataflowKind};
use crate::error::{Result, BrrrError};
use crate::lang::traits::Language;

/// Java language implementation.
pub struct Java;

/// Javadoc tag types for efficient byte-level detection.
/// Used to avoid repeated string comparisons in Javadoc parsing.
#[derive(Clone, Copy, PartialEq, Eq)]
enum JavadocTag {
    Param,
    Return,
    Throws,
    Other,
}

impl JavadocTag {
    /// Detect Javadoc tag using byte-level matching.
    /// Returns (tag_type, offset_after_tag) for efficient value extraction.
    ///
    /// Uses branch on second byte after '@' for O(1) initial dispatch,
    /// then verifies remaining bytes. Avoids substring creation entirely.
    #[inline]
    fn detect(bytes: &[u8]) -> (Self, usize) {
        // Minimum: "@x" = 2 bytes
        if bytes.len() < 2 || bytes[0] != b'@' {
            return (Self::Other, 0);
        }

        // Branch on second byte for fast dispatch
        match bytes[1] {
            b'p' => {
                // @param = 6 bytes
                if bytes.len() >= 6
                    && bytes[2] == b'a'
                    && bytes[3] == b'r'
                    && bytes[4] == b'a'
                    && bytes[5] == b'm'
                    && (bytes.len() == 6 || !bytes[6].is_ascii_alphabetic())
                {
                    return (Self::Param, 6);
                }
            }
            b'r' => {
                // @return = 7 bytes
                if bytes.len() >= 7
                    && bytes[2] == b'e'
                    && bytes[3] == b't'
                    && bytes[4] == b'u'
                    && bytes[5] == b'r'
                    && bytes[6] == b'n'
                    && (bytes.len() == 7 || !bytes[7].is_ascii_alphabetic())
                {
                    return (Self::Return, 7);
                }
            }
            b't' => {
                // @throws = 7 bytes
                if bytes.len() >= 7
                    && bytes[2] == b'h'
                    && bytes[3] == b'r'
                    && bytes[4] == b'o'
                    && bytes[5] == b'w'
                    && bytes[6] == b's'
                    && (bytes.len() == 7 || !bytes[7].is_ascii_alphabetic())
                {
                    return (Self::Throws, 7);
                }
            }
            b'e' => {
                // @exception = 10 bytes
                if bytes.len() >= 10
                    && bytes[2] == b'x'
                    && bytes[3] == b'c'
                    && bytes[4] == b'e'
                    && bytes[5] == b'p'
                    && bytes[6] == b't'
                    && bytes[7] == b'i'
                    && bytes[8] == b'o'
                    && bytes[9] == b'n'
                    && (bytes.len() == 10 || !bytes[10].is_ascii_alphabetic())
                {
                    return (Self::Throws, 10); // Treat @exception as Throws
                }
            }
            _ => {}
        }

        (Self::Other, 0)
    }
}

/// BUG #5 & #6 FIX: Comprehensive modifier extraction for Java declarations.
/// Includes Java 17+ sealed class modifiers.
#[derive(Default)]
struct ExtractedModifiers {
    visibility: Option<String>,
    is_static: bool,
    is_final: bool,
    is_abstract: bool,
    is_synchronized: bool,
    is_default: bool,
    // BUG #5 FIX: Missing modifiers
    is_native: bool,
    is_volatile: bool,
    is_transient: bool,
    is_strictfp: bool,
    // BUG #6 FIX: Java 17+ sealed class modifiers
    is_sealed: bool,
    is_non_sealed: bool,
    permits: Vec<String>,
    annotations: Vec<String>,
}

impl Java {
    /// Extract text from a node safely.
    fn node_text<'a>(&self, node: Node<'a>, source: &'a [u8]) -> &'a str {
        node.utf8_text(source).unwrap_or("")
    }

    /// Find a child node by field name.
    fn child_by_field<'a>(&self, node: Node<'a>, field: &str) -> Option<Node<'a>> {
        node.child_by_field_name(field)
    }

    /// Extract modifiers from a method or class declaration.
    /// Returns (visibility, is_static, is_final, is_abstract, is_synchronized, is_default, annotations)
    fn extract_modifiers(
        &self,
        node: Node,
        source: &[u8],
    ) -> (Option<String>, bool, bool, bool, bool, bool, Vec<String>) {
        let mods = self.extract_all_modifiers(node, source);
        (
            mods.visibility,
            mods.is_static,
            mods.is_final,
            mods.is_abstract,
            mods.is_synchronized,
            mods.is_default,
            mods.annotations,
        )
    }

    /// BUG #5 & #6 FIX: Extract all modifiers including Java 17+ sealed class modifiers.
    /// Returns ExtractedModifiers struct with all modifier flags and annotations.
    fn extract_all_modifiers(&self, node: Node, source: &[u8]) -> ExtractedModifiers {
        let mut mods = ExtractedModifiers::default();

        // Find the modifiers node
        let modifiers_node = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "modifiers");

        if let Some(mod_node) = modifiers_node {
            let mut cursor = mod_node.walk();
            for child in mod_node.children(&mut cursor) {
                match child.kind() {
                    // Visibility modifiers
                    "public" => mods.visibility = Some("public".to_string()),
                    "private" => mods.visibility = Some("private".to_string()),
                    "protected" => mods.visibility = Some("protected".to_string()),
                    // Common modifiers
                    "static" => mods.is_static = true,
                    "final" => mods.is_final = true,
                    "abstract" => mods.is_abstract = true,
                    "synchronized" => mods.is_synchronized = true,
                    "default" => mods.is_default = true,
                    // BUG #5 FIX: Missing modifiers
                    "native" => mods.is_native = true,
                    "volatile" => mods.is_volatile = true,
                    "transient" => mods.is_transient = true,
                    "strictfp" => mods.is_strictfp = true,
                    // BUG #6 FIX: Java 17+ sealed class modifiers
                    "sealed" => mods.is_sealed = true,
                    "non-sealed" => mods.is_non_sealed = true,
                    // Annotations
                    "marker_annotation" | "annotation" => {
                        mods.annotations.push(self.extract_annotation(child, source));
                    }
                    _ => {}
                }
            }
        }

        // BUG #6 FIX: Extract permits clause for sealed classes
        mods.permits = self.extract_permits_clause(node, source);

        mods
    }

    /// BUG #6 FIX: Extract permits clause from sealed class declaration.
    /// Returns list of permitted subclass names.
    fn extract_permits_clause(&self, node: Node, source: &[u8]) -> Vec<String> {
        let mut permits = Vec::new();

        for child in node.children(&mut node.walk()) {
            // Look for permits clause (tree-sitter-java may represent this differently)
            if child.kind() == "permits" || child.kind() == "permits_clause" {
                for type_child in child.children(&mut child.walk()) {
                    match type_child.kind() {
                        "type_identifier" | "scoped_type_identifier" => {
                            permits.push(self.extract_type(type_child, source));
                        }
                        "type_list" => {
                            for inner in type_child.children(&mut type_child.walk()) {
                                if matches!(inner.kind(), "type_identifier" | "scoped_type_identifier")
                                {
                                    permits.push(self.extract_type(inner, source));
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
        }

        permits
    }

    /// Extract annotation text (e.g., "@Override" or "@Table(name = \"users\")").
    fn extract_annotation(&self, node: Node, source: &[u8]) -> String {
        self.node_text(node, source).to_string()
    }

    /// Extract type string from a type node.
    /// BUG #28 FIX: Handles Java 10+ var keyword for local variable type inference.
    /// BUG #18 FIX: Handles wildcard type bounds (? extends T, ? super T).
    fn extract_type(&self, node: Node, source: &[u8]) -> String {
        let text = self.node_text(node, source);
        match node.kind() {
            "void_type" => "void".to_string(),
            "type_identifier" => {
                // BUG #28 FIX: Handle Java 10+ var keyword for local variable type inference
                // var is a reserved type name (not a keyword), so it appears as type_identifier
                if text == "var" {
                    "var".to_string()
                } else {
                    text.to_string()
                }
            }
            "integral_type" | "floating_point_type" | "boolean_type" => text.to_string(),
            "generic_type" => self.extract_generic_type(node, source),
            "array_type" => self.extract_array_type(node, source),
            "scoped_type_identifier" => self.extract_scoped_type(node, source),
            // BUG #18 FIX: Properly extract wildcard type bounds
            "wildcard" => self.extract_wildcard_type(node, source),
            _ => text.to_string(),
        }
    }

    /// BUG #18 FIX: Extract wildcard type with bounds.
    /// Handles: ? (unbounded), ? extends T (upper bound), ? super T (lower bound).
    fn extract_wildcard_type(&self, node: Node, source: &[u8]) -> String {
        let mut result = String::from("?");

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                // Upper bound: ? extends T
                "extends" => {
                    result.push_str(" extends ");
                }
                // Lower bound: ? super T
                "super" => {
                    result.push_str(" super ");
                }
                // The bounded type itself
                "type_identifier" | "generic_type" | "scoped_type_identifier" | "array_type" => {
                    result.push_str(&self.extract_type(child, source));
                }
                _ => {}
            }
        }

        result
    }

    /// Extract generic type (e.g., "List<String>").
    fn extract_generic_type(&self, node: Node, source: &[u8]) -> String {
        let mut result = String::new();

        // Get the base type
        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "type_identifier" | "scoped_type_identifier" => {
                    result.push_str(self.node_text(child, source));
                }
                "type_arguments" => {
                    result.push_str(self.extract_type_arguments(child, source).as_str());
                }
                _ => {}
            }
        }

        result
    }

    /// Extract type arguments (e.g., "<String, Integer>").
    fn extract_type_arguments(&self, node: Node, source: &[u8]) -> String {
        let mut args = Vec::new();

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "type_identifier" | "generic_type" | "wildcard" | "scoped_type_identifier" => {
                    args.push(self.extract_type(child, source));
                }
                _ => {}
            }
        }

        if args.is_empty() {
            "<>".to_string()
        } else {
            format!("<{}>", args.join(", "))
        }
    }

    /// Extract array type (e.g., "String[]").
    fn extract_array_type(&self, node: Node, source: &[u8]) -> String {
        let mut base_type = String::new();
        let mut dimensions = 0;

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "type_identifier"
                | "integral_type"
                | "floating_point_type"
                | "boolean_type"
                | "generic_type" => {
                    base_type = self.extract_type(child, source);
                }
                "dimensions" => {
                    // BUG #4 FIX: Count actual `[` tokens for robust dimension counting.
                    // This avoids fragile assumptions about tree-sitter node structure.
                    dimensions = self.count_array_dimensions(child, source);
                }
                _ => {}
            }
        }

        format!("{}{}", base_type, "[]".repeat(dimensions.max(1)))
    }

    /// BUG #4 FIX: Count array dimensions by counting `[` bracket tokens.
    /// This is more robust than dividing child_count() which can vary based on tree-sitter version.
    fn count_array_dimensions(&self, dimensions_node: Node, source: &[u8]) -> usize {
        // Method 1: Count `[` children directly in the AST
        let mut count = 0;
        for child in dimensions_node.children(&mut dimensions_node.walk()) {
            if child.kind() == "[" {
                count += 1;
            }
        }

        // Fallback: If no `[` children found, count from text representation
        if count == 0 {
            let text = self.node_text(dimensions_node, source);
            count = text.chars().filter(|&c| c == '[').count();
        }

        // Ensure at least 1 dimension if we have a dimensions node
        count.max(1)
    }

    /// Extract scoped type identifier (e.g., "java.util.List").
    fn extract_scoped_type(&self, node: Node, source: &[u8]) -> String {
        self.node_text(node, source).to_string()
    }

    /// Extract method parameters from formal_parameters node.
    fn extract_parameters(&self, node: Node, source: &[u8]) -> Vec<String> {
        let mut params = Vec::new();

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "formal_parameter" | "spread_parameter" => {
                    params.push(self.extract_single_parameter(child, source));
                }
                "receiver_parameter" => {
                    // Java receiver parameter (Type this) - skip for signature
                }
                _ => {}
            }
        }

        params
    }

    /// Extract a single parameter with type and name.
    fn extract_single_parameter(&self, node: Node, source: &[u8]) -> String {
        let mut param_type = String::new();
        let mut param_name = String::new();
        let mut is_vararg = false;

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "type_identifier"
                | "integral_type"
                | "floating_point_type"
                | "boolean_type"
                | "generic_type"
                | "array_type"
                | "scoped_type_identifier"
                | "void_type" => {
                    param_type = self.extract_type(child, source);
                }
                "identifier" => {
                    param_name = self.node_text(child, source).to_string();
                }
                "..." => {
                    is_vararg = true;
                }
                "modifiers" => {
                    // Parameter modifiers like final - skip for signature
                }
                "dimensions" => {
                    // Array dimensions after parameter name
                    param_type.push_str("[]");
                }
                _ => {}
            }
        }

        if is_vararg {
            format!("{}... {}", param_type, param_name)
        } else if param_name.is_empty() {
            param_type
        } else {
            format!("{} {}", param_type, param_name)
        }
    }

    /// Extract type parameters for generics (e.g., "<T, K extends Comparable>").
    fn extract_type_parameters(&self, node: Node, source: &[u8]) -> Option<String> {
        let type_params = self.child_by_field(node, "type_parameters")?;
        let mut params = Vec::new();

        for child in type_params.children(&mut type_params.walk()) {
            if child.kind() == "type_parameter" {
                params.push(self.extract_type_parameter(child, source));
            }
        }

        if params.is_empty() {
            None
        } else {
            Some(format!("<{}>", params.join(", ")))
        }
    }

    /// Extract a single type parameter.
    fn extract_type_parameter(&self, node: Node, source: &[u8]) -> String {
        let mut result = String::new();

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "type_identifier" => {
                    result.push_str(self.node_text(child, source));
                }
                "type_bound" => {
                    result.push_str(" extends ");
                    result.push_str(self.extract_type_bound(child, source).as_str());
                }
                _ => {}
            }
        }

        result
    }

    /// Extract type bound (e.g., "extends Comparable<T>").
    fn extract_type_bound(&self, node: Node, source: &[u8]) -> String {
        let mut bounds = Vec::new();

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "type_identifier" | "generic_type" | "scoped_type_identifier" => {
                    bounds.push(self.extract_type(child, source));
                }
                _ => {}
            }
        }

        bounds.join(" & ")
    }

    /// Extract Javadoc comment by looking at preceding sibling.
    /// BUG #17 FIX: Skip over modifiers/annotations nodes when searching for Javadoc.
    /// When annotations are between Javadoc and method, we need to traverse past them.
    fn extract_javadoc(&self, node: Node, source: &[u8]) -> Option<String> {
        // Look for block_comment before the declaration
        // BUG #17 FIX: Need to skip modifiers/annotations that may be between Javadoc and declaration
        let mut prev = node.prev_sibling();
        while let Some(sibling) = prev {
            match sibling.kind() {
                "block_comment" => {
                    let comment = self.node_text(sibling, source);
                    // Check if it's a Javadoc comment (starts with /**)
                    if comment.starts_with("/**") {
                        return Some(self.parse_javadoc(comment));
                    }
                    return None;
                }
                "line_comment" => {
                    // Skip line comments, they're not Javadoc
                    prev = sibling.prev_sibling();
                }
                // BUG #17 FIX: Skip modifiers/annotations - they can appear between Javadoc and declaration
                "modifiers" | "marker_annotation" | "annotation" => {
                    prev = sibling.prev_sibling();
                }
                _ => {
                    // Stop at any other non-comment node
                    return None;
                }
            }
        }
        None
    }

    /// Parse Javadoc comment to extract meaningful content.
    /// BUG #19 FIX: Parse Javadoc comment into structured format.
    /// Extracts @param, @return, @throws tags into a clean, structured output.
    fn parse_javadoc(&self, comment: &str) -> String {
        // Remove /** and */ markers
        let content = comment
            .trim_start_matches("/**")
            .trim_end_matches("*/")
            .trim();

        // Process each line - remove leading asterisks and whitespace
        let lines: Vec<&str> = content
            .lines()
            .map(|line| line.trim().trim_start_matches('*').trim())
            .filter(|line| !line.is_empty())
            .collect();

        // BUG #19 FIX: Parse @tags into structured sections
        let mut description_lines = Vec::new();
        let mut params = Vec::new();
        let mut returns = Vec::new();
        let mut throws = Vec::new();

        let mut current_tag: Option<JavadocTag> = None;
        let mut current_value = String::new();

        for line in lines {
            let bytes = line.as_bytes();
            // Fast check: first byte must be '@' for tag detection
            if !bytes.is_empty() && bytes[0] == b'@' {
                // Save previous tag if any
                self.save_javadoc_tag(
                    current_tag,
                    &current_value,
                    &mut params,
                    &mut returns,
                    &mut throws,
                );

                // Parse new tag using byte-level detection
                let (tag, offset) = JavadocTag::detect(bytes);
                match tag {
                    JavadocTag::Param | JavadocTag::Return | JavadocTag::Throws => {
                        current_tag = Some(tag);
                        // Extract value after tag, avoiding substring creation until needed
                        current_value = if offset < bytes.len() {
                            // SAFETY: offset is within bounds, line is valid UTF-8
                            line[offset..].trim().to_string()
                        } else {
                            String::new()
                        };
                    }
                    JavadocTag::Other => {
                        // Other tags like @see, @since, @deprecated - treat as description
                        current_tag = None;
                        description_lines.push(line.to_string());
                    }
                }
            } else if current_tag.is_some() {
                // Continuation of current tag
                if !current_value.is_empty() {
                    current_value.push(' ');
                }
                current_value.push_str(line);
            } else {
                // Part of main description
                description_lines.push(line.to_string());
            }
        }

        // Save last tag
        self.save_javadoc_tag(
            current_tag,
            &current_value,
            &mut params,
            &mut returns,
            &mut throws,
        );

        // Build final structured output
        let mut result = description_lines.join("\n");

        if !params.is_empty() {
            if !result.is_empty() {
                result.push_str("\n\n");
            }
            result.push_str("Parameters:\n");
            for param in params {
                result.push_str(&format!("  {}\n", param));
            }
        }

        if !returns.is_empty() {
            if !result.is_empty() && !result.ends_with('\n') {
                result.push('\n');
            }
            result.push_str("Returns: ");
            result.push_str(&returns.join(", "));
        }

        if !throws.is_empty() {
            if !result.is_empty() {
                result.push_str("\n");
            }
            result.push_str("Throws:\n");
            for throw in throws {
                result.push_str(&format!("  {}\n", throw));
            }
        }

        result.trim_end().to_string()
    }

    /// BUG #19 FIX: Helper to save parsed Javadoc tag to appropriate collection.
    /// Uses enum-based dispatch for type safety and compiler optimization.
    #[inline]
    fn save_javadoc_tag(
        &self,
        tag: Option<JavadocTag>,
        value: &str,
        params: &mut Vec<String>,
        returns: &mut Vec<String>,
        throws: &mut Vec<String>,
    ) {
        if value.is_empty() {
            return;
        }
        match tag {
            Some(JavadocTag::Param) => params.push(value.to_string()),
            Some(JavadocTag::Return) => returns.push(value.to_string()),
            Some(JavadocTag::Throws) => throws.push(value.to_string()),
            Some(JavadocTag::Other) | None => {}
        }
    }

    /// Extract superclass from class declaration.
    fn extract_superclass(&self, node: Node, source: &[u8]) -> Option<String> {
        let superclass_node = self.child_by_field(node, "superclass")?;

        // Find the type identifier in the superclass node
        for child in superclass_node.children(&mut superclass_node.walk()) {
            match child.kind() {
                "type_identifier" | "generic_type" | "scoped_type_identifier" => {
                    return Some(self.extract_type(child, source));
                }
                _ => {}
            }
        }

        None
    }

    /// Extract implemented interfaces.
    fn extract_interfaces(&self, node: Node, source: &[u8]) -> Vec<String> {
        let interfaces_node = match self.child_by_field(node, "interfaces") {
            Some(n) => n,
            None => return Vec::new(),
        };

        let mut interfaces = Vec::new();

        // Look for type_list inside super_interfaces
        for child in interfaces_node.children(&mut interfaces_node.walk()) {
            if child.kind() == "type_list" {
                for type_child in child.children(&mut child.walk()) {
                    match type_child.kind() {
                        "type_identifier" | "generic_type" | "scoped_type_identifier" => {
                            interfaces.push(self.extract_type(type_child, source));
                        }
                        _ => {}
                    }
                }
            }
        }

        interfaces
    }

    /// Extract extended interfaces for interface declarations.
    fn extract_extends_interfaces(&self, node: Node, source: &[u8]) -> Vec<String> {
        let mut interfaces = Vec::new();

        for child in node.children(&mut node.walk()) {
            if child.kind() == "extends_interfaces" {
                for type_list_child in child.children(&mut child.walk()) {
                    if type_list_child.kind() == "type_list" {
                        for type_child in type_list_child.children(&mut type_list_child.walk()) {
                            match type_child.kind() {
                                "type_identifier" | "generic_type" | "scoped_type_identifier" => {
                                    interfaces.push(self.extract_type(type_child, source));
                                }
                                _ => {}
                            }
                        }
                    }
                }
            }
        }

        interfaces
    }

    /// Extract methods from a class/interface/enum body.
    /// BUG #9 & #10 FIX: Also extracts static and instance initializer blocks.
    fn extract_methods(&self, body_node: Node, source: &[u8]) -> Vec<FunctionInfo> {
        let mut methods = Vec::new();
        let mut static_init_count = 0;
        let mut instance_init_count = 0;

        for child in body_node.children(&mut body_node.walk()) {
            match child.kind() {
                "method_declaration" => {
                    if let Some(func) = self.extract_method(child, source) {
                        methods.push(func);
                    }
                }
                "constructor_declaration" => {
                    if let Some(func) = self.extract_constructor(child, source) {
                        methods.push(func);
                    }
                }
                "enum_body_declarations" => {
                    // Enum can have methods inside enum_body_declarations
                    methods.extend(self.extract_methods(child, source));
                }
                // BUG #9 FIX: Static initializer blocks
                "static_initializer" => {
                    static_init_count += 1;
                    methods.push(self.extract_static_initializer(child, source, static_init_count));
                }
                // BUG #10 FIX: Instance initializer blocks (standalone blocks in class body)
                "block" => {
                    instance_init_count += 1;
                    methods.push(self.extract_instance_initializer(
                        child,
                        source,
                        instance_init_count,
                    ));
                }
                _ => {}
            }
        }

        methods
    }

    /// BUG #20 FIX: Extract field declarations from a class/interface/enum body.
    /// Returns a Vec of FieldInfo with type, visibility, modifiers, and initial values.
    fn extract_fields(&self, body_node: Node, source: &[u8]) -> Vec<FieldInfo> {
        let mut fields = Vec::new();

        for child in body_node.children(&mut body_node.walk()) {
            if child.kind() == "field_declaration" {
                fields.extend(self.extract_field(child, source));
            }
        }

        fields
    }

    /// BUG #20 FIX: Extract a single field declaration.
    /// A field declaration can declare multiple variables: int x, y = 5;
    fn extract_field(&self, node: Node, source: &[u8]) -> Vec<FieldInfo> {
        let mut fields = Vec::new();
        let mut field_type: Option<String> = None;
        let mut visibility: Option<String> = None;
        let mut is_static = false;
        let mut is_final = false;
        let mut annotations = Vec::new();

        // Extract modifiers
        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "modifiers" => {
                    for mod_child in child.children(&mut child.walk()) {
                        match mod_child.kind() {
                            "public" => visibility = Some("public".to_string()),
                            "private" => visibility = Some("private".to_string()),
                            "protected" => visibility = Some("protected".to_string()),
                            "static" => is_static = true,
                            "final" => is_final = true,
                            "marker_annotation" | "annotation" => {
                                annotations.push(self.extract_annotation(mod_child, source));
                            }
                            _ => {}
                        }
                    }
                }
                // Extract the type
                "type_identifier"
                | "integral_type"
                | "floating_point_type"
                | "boolean_type"
                | "generic_type"
                | "array_type"
                | "scoped_type_identifier"
                | "void_type" => {
                    field_type = Some(self.extract_type(child, source));
                }
                // Extract each variable declarator
                "variable_declarator" => {
                    let mut name = String::new();
                    let mut default_value: Option<String> = None;
                    let mut extra_dims = 0;

                    for var_child in child.children(&mut child.walk()) {
                        match var_child.kind() {
                            "identifier" => {
                                name = self.node_text(var_child, source).to_string();
                            }
                            "dimensions" => {
                                extra_dims = self.count_array_dimensions(var_child, source);
                            }
                            _ if var_child.kind().ends_with("_expression")
                                || var_child.kind().ends_with("_literal")
                                || var_child.kind() == "null_literal"
                                || var_child.kind() == "identifier"
                                || var_child.kind() == "array_initializer"
                                || var_child.kind() == "object_creation_expression" =>
                            {
                                // This is the initializer value
                                if var_child.prev_sibling().map(|s| s.kind()) == Some("=") {
                                    default_value =
                                        Some(self.node_text(var_child, source).to_string());
                                }
                            }
                            _ => {}
                        }
                    }

                    // Build final type (handle int[] x or int x[])
                    let final_type = if extra_dims > 0 {
                        field_type
                            .clone()
                            .map(|t| format!("{}{}", t, "[]".repeat(extra_dims)))
                    } else {
                        field_type.clone()
                    };

                    if !name.is_empty() {
                        fields.push(FieldInfo {
                            name,
                            field_type: final_type,
                            visibility: visibility.clone(),
                            is_static,
                            is_final,
                            default_value,
                            annotations: annotations.clone(),
                            line_number: node.start_position().row + 1,
                        });
                    }
                }
                _ => {}
            }
        }

        fields
    }

    /// BUG #9 FIX: Extract static initializer block.
    /// Static initializers are represented as special FunctionInfo entries.
    fn extract_static_initializer(
        &self,
        node: Node,
        source: &[u8],
        index: usize,
    ) -> FunctionInfo {
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        // Extract docstring if there's a comment before the static block
        let docstring = self.extract_javadoc(node, source);

        FunctionInfo {
            name: format!("<static_init_{}>", index),
            params: Vec::new(),
            return_type: None,
            docstring,
            is_method: false,
            is_async: false,
            decorators: vec!["static".to_string(), "initializer".to_string()],
            line_number,
            end_line_number,
            language: "java".to_string(),
        }
    }

    /// BUG #10 FIX: Extract instance initializer block.
    /// Instance initializers are standalone blocks that run before constructor.
    fn extract_instance_initializer(
        &self,
        node: Node,
        _source: &[u8],
        index: usize,
    ) -> FunctionInfo {
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        FunctionInfo {
            name: format!("<instance_init_{}>", index),
            params: Vec::new(),
            return_type: None,
            docstring: None,
            is_method: false,
            is_async: false,
            decorators: vec!["initializer".to_string()],
            line_number,
            end_line_number,
            language: "java".to_string(),
        }
    }

    /// Extract a single method declaration.
    fn extract_method(&self, node: Node, source: &[u8]) -> Option<FunctionInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (
            visibility,
            is_static,
            is_final,
            is_abstract,
            is_synchronized,
            is_default,
            annotations,
        ) = self.extract_modifiers(node, source);

        // Build decorators list from modifiers
        let mut decorators = annotations.clone();
        if let Some(vis) = &visibility {
            decorators.insert(0, vis.clone());
        }
        if is_static {
            decorators.push("static".to_string());
        }
        if is_final {
            decorators.push("final".to_string());
        }
        if is_abstract {
            decorators.push("abstract".to_string());
        }
        if is_synchronized {
            decorators.push("synchronized".to_string());
        }
        if is_default {
            decorators.push("default".to_string());
        }

        // Extract type parameters (generics)
        let type_params = self.extract_type_parameters(node, source);
        if let Some(tp) = type_params {
            decorators.push(format!("generic:{}", tp));
        }

        // BUG #8 FIX: Extract throws clause
        let throws = self.extract_throws_clause(node, source);
        if !throws.is_empty() {
            decorators.push(format!("throws:{}", throws.join(", ")));
        }

        // Extract return type
        let return_type = self
            .child_by_field(node, "type")
            .map(|n| self.extract_type(n, source));

        // Extract parameters
        let params_node = self.child_by_field(node, "parameters")?;
        let params = self.extract_parameters(params_node, source);

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(FunctionInfo {
            name,
            params,
            return_type,
            docstring,
            is_method: true,
            is_async: false, // Java doesn't have async keyword
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// BUG #8 FIX: Extract throws clause from method/constructor declaration.
    /// Returns list of exception type names declared in the throws clause.
    fn extract_throws_clause(&self, node: Node, source: &[u8]) -> Vec<String> {
        let mut throws = Vec::new();

        for child in node.children(&mut node.walk()) {
            // tree-sitter-java uses "throws" for the throws clause
            if child.kind() == "throws" {
                for type_child in child.children(&mut child.walk()) {
                    match type_child.kind() {
                        "type_identifier" | "scoped_type_identifier" | "generic_type" => {
                            throws.push(self.extract_type(type_child, source));
                        }
                        _ => {}
                    }
                }
            }
        }

        throws
    }

    /// Extract a constructor declaration.
    fn extract_constructor(&self, node: Node, source: &[u8]) -> Option<FunctionInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (visibility, _, _, _, _, _, annotations) = self.extract_modifiers(node, source);

        // BUG #29 FIX: Make visibility position consistent with methods
        // Methods use [visibility, annotations, modifiers...], so constructors should too
        // Changed from insert(1, vis) to insert(0, vis) for consistency
        let mut decorators = annotations.clone();
        if let Some(vis) = visibility {
            decorators.insert(0, vis);
        }
        decorators.push("constructor".to_string());

        // BUG #8 FIX: Constructors can also have throws clauses
        let throws = self.extract_throws_clause(node, source);
        if !throws.is_empty() {
            decorators.push(format!("throws:{}", throws.join(", ")));
        }

        // Extract parameters
        let params_node = self.child_by_field(node, "parameters")?;
        let params = self.extract_parameters(params_node, source);

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(FunctionInfo {
            name,
            params,
            return_type: None, // Constructors don't have return type
            docstring,
            is_method: true,
            is_async: false,
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// Extract scoped identifier path (e.g., "java.util.List").
    fn extract_scoped_identifier(&self, node: Node, source: &[u8]) -> String {
        let mut parts = Vec::new();
        self.collect_identifier_parts(node, source, &mut parts);
        parts.join(".")
    }

    /// Recursively collect identifier parts.
    fn collect_identifier_parts(&self, node: Node, source: &[u8], parts: &mut Vec<String>) {
        match node.kind() {
            "identifier" => {
                parts.push(self.node_text(node, source).to_string());
            }
            "scoped_identifier" => {
                if let Some(scope) = self.child_by_field(node, "scope") {
                    self.collect_identifier_parts(scope, source, parts);
                }
                if let Some(name) = self.child_by_field(node, "name") {
                    parts.push(self.node_text(name, source).to_string());
                }
            }
            _ => {}
        }
    }

    /// Build CFG blocks from a method body.
    fn build_cfg_from_body(
        &self,
        body: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let entry_id = BlockId(*next_id);
        *next_id += 1;

        let mut statements = Vec::new();
        let mut exits = Vec::new();

        // Process statements in the body
        for child in body.children(&mut body.walk()) {
            match child.kind() {
                "if_statement" => {
                    // Create current block with statements so far
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                    }

                    // Process if statement branches
                    let (if_entry, if_exits) =
                        self.build_cfg_for_if(child, source, blocks, edges, next_id);
                    edges.push(CFGEdge::from_label(entry_id, if_entry, None));
                    exits.extend(if_exits);
                }
                "while_statement" | "for_statement" | "enhanced_for_statement" => {
                    let (loop_entry, loop_exits) =
                        self.build_cfg_for_loop(child, source, blocks, edges, next_id);
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                        edges.push(CFGEdge::from_label(entry_id, loop_entry, None));
                    }
                    exits.extend(loop_exits);
                }
                // BUG #11 FIX: Handle do-while loops
                "do_statement" => {
                    let (do_entry, do_exits) =
                        self.build_cfg_for_do_while(child, source, blocks, edges, next_id);
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                        edges.push(CFGEdge::from_label(entry_id, do_entry, None));
                    }
                    exits.extend(do_exits);
                }
                // BUG #12 FIX: Handle labeled statements
                "labeled_statement" => {
                    let (label_entry, label_exits) =
                        self.build_cfg_for_labeled(child, source, blocks, edges, next_id);
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                        edges.push(CFGEdge::from_label(entry_id, label_entry, None));
                    }
                    exits.extend(label_exits);
                }
                // BUG #12 FIX: Handle break/continue statements (control flow exits)
                "break_statement" | "continue_statement" => {
                    statements.push(self.node_text(child, source).trim().to_string());
                    let stmt_id = BlockId(*next_id);
                    *next_id += 1;
                    let block = CFGBlock {
                id: stmt_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                    blocks.insert(stmt_id, block);
                    // Break/continue are control flow exits from current block
                    exits.push(stmt_id);
                    statements.clear();
                }
                "return_statement" => {
                    statements.push(self.node_text(child, source).trim().to_string());
                    // Return statement is an exit
                    let return_id = BlockId(*next_id);
                    *next_id += 1;
                    let block = CFGBlock {
                id: return_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                    blocks.insert(return_id, block);
                    exits.push(return_id);
                    statements.clear();
                }
                "throw_statement" => {
                    statements.push(self.node_text(child, source).trim().to_string());
                    let throw_id = BlockId(*next_id);
                    *next_id += 1;
                    let block = CFGBlock {
                id: throw_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                    blocks.insert(throw_id, block);
                    exits.push(throw_id);
                    statements.clear();
                }
                // FEATURE: Handle yield statements (Java 14+ switch expressions)
                // yield is similar to return - it exits the switch expression with a value
                "yield_statement" => {
                    statements.push(self.node_text(child, source).trim().to_string());
                    let yield_id = BlockId(*next_id);
                    *next_id += 1;
                    let block = CFGBlock {
                id: yield_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                    blocks.insert(yield_id, block);
                    // yield is an exit point from the switch expression
                    exits.push(yield_id);
                    statements.clear();
                }
                "try_statement" => {
                    let (try_entry, try_exits) =
                        self.build_cfg_for_try(child, source, blocks, edges, next_id);
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                        edges.push(CFGEdge::from_label(entry_id, try_entry, None));
                    }
                    exits.extend(try_exits);
                }
                "switch_expression" | "switch_statement" => {
                    let (switch_entry, switch_exits) =
                        self.build_cfg_for_switch(child, source, blocks, edges, next_id);
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                        edges.push(CFGEdge::from_label(entry_id, switch_entry, None));
                    }
                    exits.extend(switch_exits);
                }
                // BUG #23 FIX: Handle synchronized blocks
                "synchronized_statement" => {
                    let (sync_entry, sync_exits) =
                        self.build_cfg_for_synchronized(child, source, blocks, edges, next_id);
                    if !statements.is_empty() {
                        let block = CFGBlock {
                id: entry_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: child.start_position().row + 1,
            };
                        blocks.insert(entry_id, block);
                        statements.clear();
                        edges.push(CFGEdge::from_label(entry_id, sync_entry, None));
                    }
                    exits.extend(sync_exits);
                }
                // BUG #24 FIX: Handle assert statements (can throw AssertionError)
                "assert_statement" => {
                    statements.push(self.node_text(child, source).trim().to_string());
                    // Assert can throw AssertionError, creating a potential exit point
                    let assert_id = BlockId(*next_id);
                    *next_id += 1;
                    let block = CFGBlock {
                id: assert_id,
                label: statements.join("; "),
                block_type: BlockType::Body,
                statements: statements.clone(),
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                    blocks.insert(assert_id, block);
                    // Assert has two paths: continue (pass) or throw (fail)
                    // For simplicity, we mark it as a potential exit due to AssertionError
                    exits.push(assert_id);
                    statements.clear();
                }
                "{" | "}" => {
                    // Skip braces
                }
                _ if !child.is_named() => {
                    // Skip unnamed nodes
                }
                _ => {
                    // Regular statement
                    let stmt_text = self.node_text(child, source).trim().to_string();
                    if !stmt_text.is_empty() {
                        statements.push(stmt_text);
                    }
                }
            }
        }

        // Create final block if there are remaining statements
        if !statements.is_empty() || blocks.get(&entry_id).is_none() {
            let block = CFGBlock {
                id: entry_id,
                label: if statements.is_empty() {
                    "entry".to_string()
                } else {
                    statements.join("; ")
                },
                block_type: BlockType::Body,
                statements,
                func_calls: Vec::new(),
                start_line: body.start_position().row + 1,
                end_line: body.end_position().row + 1,
            };
            blocks.insert(entry_id, block);
            if exits.is_empty() {
                exits.push(entry_id);
            }
        }

        (entry_id, exits)
    }

    /// Build CFG for if statement.
    fn build_cfg_for_if(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let condition_id = BlockId(*next_id);
        *next_id += 1;

        // Extract condition
        let condition_text = self
            .child_by_field(node, "condition")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "condition".to_string());

        let block = CFGBlock {
            id: condition_id,
            label: format!("if ({})", condition_text),
            block_type: BlockType::Body,
            statements: vec![format!("if ({})", condition_text)],
            func_calls: Vec::new(),
            start_line: node.start_position().row + 1,
            end_line: node.start_position().row + 1,
        };
        blocks.insert(condition_id, block);

        let mut exits = Vec::new();

        // Process consequence (then branch)
        if let Some(consequence) = self.child_by_field(node, "consequence") {
            let (then_entry, then_exits) =
                self.build_cfg_from_body(consequence, source, blocks, edges, next_id);
            edges.push(CFGEdge::from_label(condition_id, then_entry, Some("true".to_string())));
            exits.extend(then_exits);
        }

        // Process alternative (else branch)
        if let Some(alternative) = self.child_by_field(node, "alternative") {
            let (else_entry, else_exits) =
                self.build_cfg_from_body(alternative, source, blocks, edges, next_id);
            edges.push(CFGEdge::from_label(condition_id, else_entry, Some("false".to_string())));
            exits.extend(else_exits);
        } else {
            // No else branch - condition itself is an exit for false case
            exits.push(condition_id);
        }

        (condition_id, exits)
    }

    /// Build CFG for loop statements.
    fn build_cfg_for_loop(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let loop_id = BlockId(*next_id);
        *next_id += 1;

        let label = match node.kind() {
            "while_statement" => {
                let cond = self
                    .child_by_field(node, "condition")
                    .map(|n| self.node_text(n, source).to_string())
                    .unwrap_or_default();
                format!("while ({})", cond)
            }
            "for_statement" => "for loop".to_string(),
            "enhanced_for_statement" => "for-each loop".to_string(),
            _ => "loop".to_string(),
        };

        let block = CFGBlock {
                id: loop_id,
                label: label.clone(),
                block_type: BlockType::Body,
                statements: vec![label],
                func_calls: Vec::new(),
                start_line: node.start_position().row + 1,
                end_line: node.start_position().row + 1,
            };
        blocks.insert(loop_id, block);

        // Process loop body
        if let Some(body) = self.child_by_field(node, "body") {
            let (body_entry, body_exits) =
                self.build_cfg_from_body(body, source, blocks, edges, next_id);
            edges.push(CFGEdge::from_label(loop_id, body_entry, Some("true".to_string())));
            // Back edge from body to loop condition
            for exit in &body_exits {
                edges.push(CFGEdge::from_label(*exit, loop_id, None));
            }
        }

        // Exit from loop (false condition)
        let exit_id = BlockId(*next_id);
        *next_id += 1;
        let exit_block = CFGBlock {
                id: exit_id,
                label: "loop exit".to_string(),
                block_type: BlockType::Body,
                statements: vec![],
                func_calls: Vec::new(),
                start_line: node.end_position().row + 1,
                end_line: node.end_position().row + 1,
            };
        blocks.insert(exit_id, exit_block);
        edges.push(CFGEdge::from_label(loop_id, exit_id, Some("false".to_string())));

        (loop_id, vec![exit_id])
    }

    /// Build CFG for try statement.
    /// BUG #22 FIX: Handle try-with-resources (resource_specification in try statements).
    fn build_cfg_for_try(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let try_id = BlockId(*next_id);
        *next_id += 1;

        // BUG #22 FIX: Check for try-with-resources
        let resource_spec = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "resource_specification");

        // Build label including resources if present
        let try_label = if let Some(ref res) = resource_spec {
            let resources_text = self.node_text(*res, source);
            format!("try {}", resources_text)
        } else {
            "try".to_string()
        };

        let block = CFGBlock {
                id: try_id,
                label: try_label.clone(),
                block_type: BlockType::Body,
                statements: vec![try_label],
                func_calls: Vec::new(),
                start_line: node.start_position().row + 1,
                end_line: node.start_position().row + 1,
            };
        blocks.insert(try_id, block);

        let mut exits = Vec::new();

        // BUG #22 FIX: For try-with-resources, create resource acquisition block
        // Resources can throw exceptions during acquisition, so we need edges for that
        let effective_entry = if let Some(ref res) = resource_spec {
            let resource_id = BlockId(*next_id);
            *next_id += 1;

            // Extract resource declarations for the CFG block
            let resource_text = self.extract_resource_declarations(*res, source);

            let resource_block = CFGBlock {
                id: resource_id,
                label: format!("acquire resources: {}", resource_text),
                block_type: BlockType::Body,
                statements: vec![format!("acquire: {}", resource_text)],
                func_calls: Vec::new(),
                start_line: res.start_position().row + 1,
                end_line: res.end_position().row + 1,
            };
            blocks.insert(resource_id, resource_block);

            // Edge from try to resource acquisition
            edges.push(CFGEdge::from_label(try_id, resource_id, Some("acquire".to_string())));

            resource_id
        } else {
            try_id
        };

        // Process try body
        if let Some(body) = self.child_by_field(node, "body") {
            let (body_entry, body_exits) =
                self.build_cfg_from_body(body, source, blocks, edges, next_id);

            // Edge from resource acquisition (or try) to body
            if resource_spec.is_some() {
                edges.push(CFGEdge::from_label(effective_entry, body_entry, Some("success".to_string())));
            } else {
                edges.push(CFGEdge::from_label(try_id, body_entry, None));
            }
            exits.extend(body_exits);
        }

        // BUG #22 FIX: For try-with-resources, add implicit close block
        // Resources are closed in reverse order of declaration
        let close_block_id = if resource_spec.is_some() {
            let close_id = BlockId(*next_id);
            *next_id += 1;

            let close_block = CFGBlock {
                id: close_id,
                label: "close resources".to_string(),
                block_type: BlockType::Body,
                statements: vec!["close resources (auto)".to_string()],
                func_calls: Vec::new(),
                start_line: node.end_position().row + 1,
                end_line: node.end_position().row + 1,
            };
            blocks.insert(close_id, close_block);

            // All body exits flow through resource close
            for exit in &exits {
                edges.push(CFGEdge::from_label(*exit, close_id, Some("close".to_string())));
            }

            // Replace exits with close block
            exits = vec![close_id];

            Some(close_id)
        } else {
            None
        };

        // Process catch clauses
        for child in node.children(&mut node.walk()) {
            if child.kind() == "catch_clause" {
                if let Some(catch_body) = self.child_by_field(child, "body") {
                    let (catch_entry, catch_exits) =
                        self.build_cfg_from_body(catch_body, source, blocks, edges, next_id);

                    // BUG #22 FIX: For try-with-resources, exceptions can come from:
                    // 1. Resource acquisition
                    // 2. Try body
                    // 3. Resource close (suppressed exceptions)
                    if resource_spec.is_some() {
                        // Exception from resource acquisition
                        edges.push(CFGEdge::from_label(effective_entry, catch_entry, Some("exception (acquire)".to_string())));
                        // Exception from close
                        if let Some(close_id) = close_block_id {
                            edges.push(CFGEdge::from_label(close_id, catch_entry, Some("exception (close)".to_string())));
                        }
                    } else {
                        edges.push(CFGEdge::from_label(try_id, catch_entry, Some("exception".to_string())));
                    }
                    exits.extend(catch_exits);
                }
            }
        }

        // Process finally block
        if let Some(finally_clause) = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "finally_clause")
        {
            if let Some(finally_body) = self.child_by_field(finally_clause, "body") {
                let (finally_entry, finally_exits) =
                    self.build_cfg_from_body(finally_body, source, blocks, edges, next_id);
                // All exits flow through finally
                for exit in &exits {
                    edges.push(CFGEdge::from_label(*exit, finally_entry, None));
                }
                exits = finally_exits;
            }
        }

        (try_id, exits)
    }

    /// BUG #22 FIX: Extract resource declarations from try-with-resources.
    /// Returns a comma-separated string of resource variable names.
    fn extract_resource_declarations(&self, node: Node, source: &[u8]) -> String {
        let mut resources = Vec::new();

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "resource" => {
                    // resource: Type name = expression
                    if let Some(name) = child
                        .children(&mut child.walk())
                        .find(|n| n.kind() == "identifier")
                    {
                        resources.push(self.node_text(name, source).to_string());
                    }
                }
                // Java 9+ allows identifier directly in resource_specification for effectively final variables
                "identifier" => {
                    resources.push(self.node_text(child, source).to_string());
                }
                _ => {}
            }
        }

        if resources.is_empty() {
            "resources".to_string()
        } else {
            resources.join(", ")
        }
    }

    /// Build CFG for switch statement.
    fn build_cfg_for_switch(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let switch_id = BlockId(*next_id);
        *next_id += 1;

        let condition = self
            .child_by_field(node, "condition")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "expr".to_string());

        let block = CFGBlock {
            id: switch_id,
            label: format!("switch ({})", condition),
            block_type: BlockType::Body,
            statements: vec![format!("switch ({})", condition)],
            func_calls: Vec::new(),
            start_line: node.start_position().row + 1,
            end_line: node.start_position().row + 1,
        };
        blocks.insert(switch_id, block);

        let mut exits = Vec::new();

        // Process switch body
        if let Some(body) = self.child_by_field(node, "body") {
            for child in body.children(&mut body.walk()) {
                match child.kind() {
                    // BUG #27 FIX: Handle switch_rule (Java 14+ arrow syntax) properly
                    // switch_rule: case X -> expression; or case X -> { block }
                    // No fall-through, each rule is independent
                    "switch_rule" => {
                        let case_id = BlockId(*next_id);
                        *next_id += 1;

                        // Extract the case label (first line up to ->)
                        let case_text = self.node_text(child, source);
                        let case_label = case_text
                            .lines()
                            .next()
                            .and_then(|s| s.split("->").next())
                            .map(|s| s.trim().to_string())
                            .unwrap_or_else(|| "case".to_string());

                        let case_block = CFGBlock {
                id: case_id,
                label: case_label.clone(),
                block_type: BlockType::Body,
                statements: vec![case_text.to_string()],
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                        blocks.insert(case_id, case_block);

                        edges.push(CFGEdge::from_label(switch_id, case_id, Some(case_label)));
                        // Arrow cases don't fall through, they are direct exits
                        exits.push(case_id);
                    }
                    // Traditional switch case with potential fall-through
                    "switch_block_statement_group" => {
                        let case_id = BlockId(*next_id);
                        *next_id += 1;

                        let case_label = self
                            .node_text(child, source)
                            .lines()
                            .next()
                            .unwrap_or("case")
                            .to_string();
                        let case_block = CFGBlock {
                id: case_id,
                label: case_label.clone(),
                block_type: BlockType::Body,
                statements: vec![case_label],
                func_calls: Vec::new(),
                start_line: child.start_position().row + 1,
                end_line: child.end_position().row + 1,
            };
                        blocks.insert(case_id, case_block);

                        edges.push(CFGEdge::from_label(switch_id, case_id, None));
                        exits.push(case_id);
                    }
                    _ => {}
                }
            }
        }

        if exits.is_empty() {
            exits.push(switch_id);
        }

        (switch_id, exits)
    }

    /// BUG #11 FIX: Build CFG for do-while statement.
    /// Do-while differs from while: body executes at least once before condition check.
    fn build_cfg_for_do_while(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        // In do-while, body executes first, then condition is checked
        let body_id = BlockId(*next_id);
        *next_id += 1;

        // Create body block label
        let body_label = "do".to_string();
        let body_block = CFGBlock {
                id: body_id,
                label: body_label.clone(),
                block_type: BlockType::Body,
                statements: vec![body_label],
                func_calls: Vec::new(),
                start_line: node.start_position().row + 1,
                end_line: node.start_position().row + 1,
            };
        blocks.insert(body_id, body_block);

        // Process do-while body
        let mut body_exits = vec![body_id];
        if let Some(body) = self.child_by_field(node, "body") {
            let (inner_entry, inner_exits) =
                self.build_cfg_from_body(body, source, blocks, edges, next_id);
            edges.push(CFGEdge::from_label(body_id, inner_entry, None));
            body_exits = inner_exits;
        }

        // Condition block (checked after body)
        let condition_id = BlockId(*next_id);
        *next_id += 1;

        let condition_text = self
            .child_by_field(node, "condition")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "condition".to_string());

        let condition_block = CFGBlock {
            id: condition_id,
            label: format!("while ({})", condition_text),
            block_type: BlockType::Body,
            statements: vec![format!("while ({})", condition_text)],
            func_calls: Vec::new(),
            start_line: node.end_position().row + 1,
            end_line: node.end_position().row + 1,
        };
        blocks.insert(condition_id, condition_block);

        // Connect body exits to condition
        for exit in &body_exits {
            edges.push(CFGEdge::from_label(*exit, condition_id, None));
        }

        // Back edge: condition true -> body (loop back)
        edges.push(CFGEdge::from_label(condition_id, body_id, Some("true".to_string())));

        // Exit block (condition false)
        let exit_id = BlockId(*next_id);
        *next_id += 1;
        let exit_block = CFGBlock {
                id: exit_id,
                label: "do-while exit".to_string(),
                block_type: BlockType::Body,
                statements: vec![],
                func_calls: Vec::new(),
                start_line: node.end_position().row + 1,
                end_line: node.end_position().row + 1,
            };
        blocks.insert(exit_id, exit_block);

        edges.push(CFGEdge::from_label(condition_id, exit_id, Some("false".to_string())));

        (body_id, vec![exit_id])
    }

    /// BUG #12 FIX: Build CFG for labeled statement.
    /// Labeled statements allow break/continue to target specific loops.
    fn build_cfg_for_labeled(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let label_id = BlockId(*next_id);
        *next_id += 1;

        // Extract the label name
        let label_name = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "identifier")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "label".to_string());

        let label_block = CFGBlock {
            id: label_id,
            label: format!("{}:", label_name),
            block_type: BlockType::Body,
            statements: vec![format!("{}:", label_name)],
            func_calls: Vec::new(),
            start_line: node.start_position().row + 1,
            end_line: node.start_position().row + 1,
        };
        blocks.insert(label_id, label_block);

        // Find and process the labeled statement (usually a loop or block)
        let mut exits = vec![label_id];
        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "identifier" | ":" => continue, // Skip label parts
                _ => {
                    // This is the statement being labeled
                    let (stmt_entry, stmt_exits) =
                        self.build_cfg_from_body(child, source, blocks, edges, next_id);
                    edges.push(CFGEdge::from_label(label_id, stmt_entry, None));
                    exits = stmt_exits;
                    break;
                }
            }
        }

        (label_id, exits)
    }

    /// BUG #23 FIX: Build CFG for synchronized statement.
    /// synchronized(lock) { body } - acquires monitor on lock object, executes body, releases monitor.
    fn build_cfg_for_synchronized(
        &self,
        node: Node,
        source: &[u8],
        blocks: &mut HashMap<BlockId, CFGBlock>,
        edges: &mut Vec<CFGEdge>,
        next_id: &mut usize,
    ) -> (BlockId, Vec<BlockId>) {
        let sync_id = BlockId(*next_id);
        *next_id += 1;

        // Extract the lock expression
        let lock_expr = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "parenthesized_expression")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "(lock)".to_string());

        let sync_block = CFGBlock {
            id: sync_id,
            label: format!("synchronized {}", lock_expr),
            block_type: BlockType::Body,
            statements: vec![format!("synchronized {}", lock_expr)],
            func_calls: Vec::new(),
            start_line: node.start_position().row + 1,
            end_line: node.start_position().row + 1,
        };
        blocks.insert(sync_id, sync_block);

        // Process the synchronized body
        let mut exits = vec![sync_id];
        if let Some(body) = self.child_by_field(node, "body") {
            let (body_entry, body_exits) =
                self.build_cfg_from_body(body, source, blocks, edges, next_id);
            edges.push(CFGEdge::from_label(sync_id, body_entry, Some("acquire".to_string())));
            exits = body_exits;
        }

        (sync_id, exits)
    }

    /// Extract variable definitions from a node for DFG.
    fn extract_definitions(
        &self,
        node: Node,
        source: &[u8],
        definitions: &mut HashMap<String, Vec<usize>>,
        edges: &mut Vec<DataflowEdge>,
    ) {
        match node.kind() {
            "local_variable_declaration" | "field_declaration" => {
                for child in node.children(&mut node.walk()) {
                    if child.kind() == "variable_declarator" {
                        if let Some(name_node) = self.child_by_field(child, "name") {
                            let var_name = self.node_text(name_node, source).to_string();
                            let line = node.start_position().row + 1;
                            definitions.entry(var_name.clone()).or_default().push(line);
                            edges.push(DataflowEdge {
                                variable: var_name,
                                from_line: line,
                                to_line: line,
                                kind: DataflowKind::Definition,
                            });
                        }
                    }
                }
            }
            "assignment_expression" => {
                if let Some(left) = self.child_by_field(node, "left") {
                    let var_name = self.node_text(left, source).to_string();
                    let line = node.start_position().row + 1;
                    definitions.entry(var_name.clone()).or_default().push(line);
                    edges.push(DataflowEdge {
                        variable: var_name,
                        from_line: line,
                        to_line: line,
                        kind: DataflowKind::Mutation,
                            });
                }
            }
            // BUG #13 FIX: Handle increment/decrement operators (i++, ++i, i--, --i)
            "update_expression" => {
                // Extract the operand being updated
                for child in node.children(&mut node.walk()) {
                    if child.kind() == "identifier" {
                        let var_name = self.node_text(child, source).to_string();
                        let line = node.start_position().row + 1;
                        definitions.entry(var_name.clone()).or_default().push(line);
                        edges.push(DataflowEdge {
                            variable: var_name,
                            from_line: line,
                            to_line: line,
                            kind: DataflowKind::Mutation,
                            });
                        break;
                    }
                }
            }
            // BUG #14 FIX: Handle compound assignment operators (+=, -=, *=, /=, %=, etc.)
            "compound_assignment_expression" => {
                if let Some(left) = self.child_by_field(node, "left") {
                    let var_name = self.node_text(left, source).to_string();
                    let line = node.start_position().row + 1;
                    definitions.entry(var_name.clone()).or_default().push(line);
                    edges.push(DataflowEdge {
                        variable: var_name,
                        from_line: line,
                        to_line: line,
                        kind: DataflowKind::Mutation,
                            });
                }
            }
            // BUG #26 FIX: Handle pattern matching instanceof (Java 16+)
            // Pattern: obj instanceof Type varName
            // The pattern variable is bound in the instanceof expression
            "instanceof_expression" => {
                // Look for a pattern variable in the instanceof expression
                // In tree-sitter-java, pattern instanceof may have a type_identifier followed by an identifier
                // e.g., "obj instanceof String s" - s is the pattern variable
                let mut found_type = false;
                for child in node.children(&mut node.walk()) {
                    match child.kind() {
                        "type_identifier" | "generic_type" | "scoped_type_identifier" => {
                            found_type = true;
                        }
                        "identifier" if found_type => {
                            // This is the pattern variable
                            let var_name = self.node_text(child, source).to_string();
                            let line = node.start_position().row + 1;
                            definitions.entry(var_name.clone()).or_default().push(line);
                            edges.push(DataflowEdge {
                                variable: var_name,
                                from_line: line,
                                to_line: line,
                                kind: DataflowKind::Definition,
                            });
                            break;
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }

        // Recurse into children
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            self.extract_definitions(child, source, definitions, edges);
        }
    }

    /// Extract variable uses from a node for DFG.
    fn extract_uses(
        &self,
        node: Node,
        source: &[u8],
        definitions: &HashMap<String, Vec<usize>>,
        uses: &mut HashMap<String, Vec<usize>>,
        edges: &mut Vec<DataflowEdge>,
    ) {
        match node.kind() {
            "identifier" => {
                // Check if this identifier is a use (not a definition)
                let parent = node.parent();
                let is_definition = parent.is_some_and(|p| {
                    matches!(p.kind(), "variable_declarator" | "formal_parameter")
                        && self.child_by_field(p, "name") == Some(node)
                });

                if !is_definition {
                    let var_name = self.node_text(node, source).to_string();
                    if definitions.contains_key(&var_name) {
                        let line = node.start_position().row + 1;
                        uses.entry(var_name.clone()).or_default().push(line);

                        // Find the most recent definition
                        if let Some(def_lines) = definitions.get(&var_name) {
                            if let Some(&def_line) = def_lines.iter().filter(|&&l| l <= line).max()
                            {
                                edges.push(DataflowEdge {
                                    variable: var_name,
                                    from_line: def_line,
                                    to_line: line,
                                    kind: DataflowKind::Use,
                            });
                            }
                        }
                    }
                }
            }
            "return_statement" => {
                let line = node.start_position().row + 1;
                // Find identifiers in return statement
                for child in node.children(&mut node.walk()) {
                    if child.kind() == "identifier" {
                        let var_name = self.node_text(child, source).to_string();
                        if definitions.contains_key(&var_name) {
                            edges.push(DataflowEdge {
                                variable: var_name,
                                from_line: line,
                                to_line: line,
                                kind: DataflowKind::Return,
                            });
                        }
                    }
                }
            }
            _ => {}
        }

        // Recurse into children
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            self.extract_uses(child, source, definitions, uses, edges);
        }
    }
}

impl Language for Java {
    fn name(&self) -> &'static str {
        "java"
    }

    fn extensions(&self) -> &[&'static str] {
        &[".java"]
    }

    fn parser(&self) -> Result<Parser> {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_java::LANGUAGE.into())
            .map_err(|e| BrrrError::TreeSitter(e.to_string()))?;
        Ok(parser)
    }

    fn extract_function(&self, node: Node, source: &[u8]) -> Option<FunctionInfo> {
        match node.kind() {
            "method_declaration" => self.extract_method(node, source),
            "constructor_declaration" => self.extract_constructor(node, source),
            _ => None,
        }
    }

    fn extract_class(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        match node.kind() {
            "class_declaration" => self.extract_class_declaration(node, source),
            "interface_declaration" => self.extract_interface_declaration(node, source),
            "enum_declaration" => self.extract_enum_declaration(node, source),
            "record_declaration" => self.extract_record_declaration(node, source),
            "annotation_type_declaration" => self.extract_annotation_type_declaration(node, source),
            // BUG #30 FIX: Handle module-info.java module declarations (Java 9+)
            "module_declaration" => self.extract_module_declaration(node, source),
            _ => None,
        }
    }

    fn extract_imports(&self, tree: &Tree, source: &[u8]) -> Vec<ImportInfo> {
        let mut imports = Vec::new();
        let root = tree.root_node();

        for child in root.children(&mut root.walk()) {
            match child.kind() {
                "package_declaration" => {
                    // Extract package declaration as a special ImportInfo entry
                    // This is critical for Java class resolution
                    if let Some(pkg_info) = self.extract_package_declaration(child, source) {
                        imports.push(pkg_info);
                    }
                }
                "import_declaration" => {
                    if let Some(import) = self.extract_import(child, source) {
                        imports.push(import);
                    }
                }
                _ => {}
            }
        }

        imports
    }

    fn function_query(&self) -> &'static str {
        r#"[
            (method_declaration name: (identifier) @name) @method
            (constructor_declaration name: (identifier) @name) @constructor
        ]"#
    }

    fn class_query(&self) -> &'static str {
        r#"[
            (class_declaration name: (identifier) @name) @class
            (interface_declaration name: (identifier) @name) @interface
            (enum_declaration name: (identifier) @name) @enum
            (record_declaration name: (identifier) @name) @record
            (annotation_type_declaration name: (identifier) @name) @annotation_type
        ]"#
    }

    fn call_query(&self) -> &'static str {
        // Enhanced call query with lambda expressions and method references
        // FEATURE: Lambda expressions - capture method calls inside lambda bodies
        // FEATURE: Method references - capture the referenced method name
        //
        // Lambda examples: list.forEach(x -> process(x)) tracks "process"
        // Method reference examples:
        //   - String::valueOf tracks "valueOf"
        //   - System.out::println tracks "println"
        //   - this::method tracks "method"
        //   - super::method tracks "method"
        r#"[
            (method_invocation name: (identifier) @callee) @call
            (object_creation_expression type: (type_identifier) @callee) @call
            (lambda_expression
                body: (method_invocation name: (identifier) @callee)) @call
            (method_reference . (_) "::" (identifier) @callee) @call
        ]"#
    }

    fn build_cfg(&self, node: Node, source: &[u8]) -> Result<CFGInfo> {
        // Get function name
        let function_name = self
            .child_by_field(node, "name")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "unknown".to_string());

        let mut blocks = HashMap::new();
        let mut edges = Vec::new();
        let mut next_id = 0usize;

        // Find the method body
        let body = match node.kind() {
            "method_declaration" => self.child_by_field(node, "body"),
            "constructor_declaration" => self.child_by_field(node, "body"),
            _ => None,
        };

        let (entry, exits) = if let Some(body_node) = body {
            self.build_cfg_from_body(body_node, source, &mut blocks, &mut edges, &mut next_id)
        } else {
            // Abstract method or no body - create single entry/exit block
            let id = BlockId(0);
            let block = CFGBlock {
                id,
                label: "abstract".to_string(),
                block_type: BlockType::Body,
                statements: vec![],
                func_calls: Vec::new(),
                start_line: node.start_position().row + 1,
                end_line: node.end_position().row + 1,
            };
            blocks.insert(id, block);
            (id, vec![id])
        };

        Ok(CFGInfo {
            function_name,
            blocks,
            edges,
            entry,
            exits,
            decision_points: 0, // TODO: Calculate actual decision points for Java
            comprehension_decision_points: 0, // Java doesn't have Python-style comprehensions
            nested_cfgs: HashMap::new(),      // TODO: Handle Java lambdas/anonymous classes as nested CFGs
            is_async: false,                  // Java uses different async patterns (CompletableFuture)
            await_points: 0,                  // Java doesn't have await keyword
            blocking_calls: Vec::new(),       // TODO: Track blocking calls in async contexts
            ..Default::default()
        })
    }

    fn build_dfg(&self, node: Node, source: &[u8]) -> Result<DFGInfo> {
        let function_name = self
            .child_by_field(node, "name")
            .map(|n| self.node_text(n, source).to_string())
            .unwrap_or_else(|| "unknown".to_string());

        let mut definitions = HashMap::new();
        let mut uses = HashMap::new();
        let mut edges = Vec::new();

        // Extract parameters as definitions
        if let Some(params) = self.child_by_field(node, "parameters") {
            for child in params.children(&mut params.walk()) {
                if child.kind() == "formal_parameter" {
                    if let Some(name_node) = self.child_by_field(child, "name") {
                        let var_name = self.node_text(name_node, source).to_string();
                        let line = child.start_position().row + 1;
                        definitions
                            .entry(var_name.clone())
                            .or_insert_with(Vec::new)
                            .push(line);
                        edges.push(DataflowEdge {
                            variable: var_name,
                            from_line: line,
                            to_line: line,
                            kind: DataflowKind::Param,
                            });
                    }
                }
            }
        }

        // Extract definitions from body
        if let Some(body) = self.child_by_field(node, "body") {
            self.extract_definitions(body, source, &mut definitions, &mut edges);
            self.extract_uses(body, source, &definitions, &mut uses, &mut edges);
        }

        Ok(DFGInfo::new(function_name, edges, definitions, uses))
    }
}

impl Java {
    /// Extract class declaration.
    fn extract_class_declaration(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (visibility, is_static, is_final, is_abstract, _, _, annotations) =
            self.extract_modifiers(node, source);

        // Build decorators
        let mut decorators = annotations;
        if let Some(vis) = visibility {
            decorators.insert(0, vis);
        }
        if is_static {
            decorators.push("static".to_string());
        }
        if is_final {
            decorators.push("final".to_string());
        }
        if is_abstract {
            decorators.push("abstract".to_string());
        }

        // Extract type parameters
        if let Some(tp) = self.extract_type_parameters(node, source) {
            decorators.push(format!("generic:{}", tp));
        }

        // Build bases list
        let mut bases = Vec::new();

        // Add superclass if present
        if let Some(superclass) = self.extract_superclass(node, source) {
            bases.push(format!("extends {}", superclass));
        }

        // Add interfaces
        for interface in self.extract_interfaces(node, source) {
            bases.push(format!("implements {}", interface));
        }

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // BUG #20 FIX: Extract methods, fields, and inner classes from class body
        let (methods, fields, inner_classes) = if let Some(body) = self.child_by_field(node, "body")
        {
            (
                self.extract_methods(body, source),
                self.extract_fields(body, source),
                self.extract_inner_classes(body, source),
            )
        } else {
            (Vec::new(), Vec::new(), Vec::new())
        };

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(ClassInfo {
            name,
            bases,
            docstring,
            methods,
            fields,
            inner_classes,
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// BUG #7 FIX: Extract inner/nested classes from a class body.
    /// Recursively extracts class_declaration, interface_declaration, enum_declaration nodes.
    fn extract_inner_classes(&self, body_node: Node, source: &[u8]) -> Vec<ClassInfo> {
        let mut inner_classes = Vec::new();

        for child in body_node.children(&mut body_node.walk()) {
            match child.kind() {
                "class_declaration" => {
                    if let Some(class) = self.extract_class_declaration(child, source) {
                        inner_classes.push(class);
                    }
                }
                "interface_declaration" => {
                    if let Some(interface) = self.extract_interface_declaration(child, source) {
                        inner_classes.push(interface);
                    }
                }
                "enum_declaration" => {
                    if let Some(enum_class) = self.extract_enum_declaration(child, source) {
                        inner_classes.push(enum_class);
                    }
                }
                "record_declaration" => {
                    if let Some(record) = self.extract_record_declaration(child, source) {
                        inner_classes.push(record);
                    }
                }
                "annotation_type_declaration" => {
                    if let Some(annotation) = self.extract_annotation_type_declaration(child, source)
                    {
                        inner_classes.push(annotation);
                    }
                }
                _ => {}
            }
        }

        inner_classes
    }

    /// Extract interface declaration.
    fn extract_interface_declaration(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (visibility, _, _, _, _, _, annotations) = self.extract_modifiers(node, source);

        // Build decorators
        let mut decorators = vec!["interface".to_string()];
        decorators.extend(annotations);
        if let Some(vis) = visibility {
            decorators.insert(1, vis);
        }

        // Extract type parameters
        if let Some(tp) = self.extract_type_parameters(node, source) {
            decorators.push(format!("generic:{}", tp));
        }

        // Build bases list (extended interfaces)
        let bases: Vec<String> = self
            .extract_extends_interfaces(node, source)
            .into_iter()
            .map(|i| format!("extends {}", i))
            .collect();

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // BUG #20 FIX: Extract methods, fields, and inner classes from interface body
        let (methods, fields, inner_classes) = if let Some(body) = self.child_by_field(node, "body")
        {
            (
                self.extract_methods(body, source),
                self.extract_fields(body, source),
                self.extract_inner_classes(body, source),
            )
        } else {
            (Vec::new(), Vec::new(), Vec::new())
        };

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(ClassInfo {
            name,
            bases,
            docstring,
            methods,
            fields,
            inner_classes,
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// Extract enum declaration.
    fn extract_enum_declaration(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (visibility, _, _, _, _, _, annotations) = self.extract_modifiers(node, source);

        // Build decorators
        let mut decorators = vec!["enum".to_string()];
        decorators.extend(annotations);
        if let Some(vis) = visibility {
            decorators.insert(1, vis);
        }

        // Extract implemented interfaces
        let bases: Vec<String> = self
            .extract_interfaces(node, source)
            .into_iter()
            .map(|i| format!("implements {}", i))
            .collect();

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // BUG #20 & #21 FIX: Extract methods, fields, enum constants, and inner classes from enum body
        let (methods, fields, inner_classes) =
            if let Some(body) = self.child_by_field(node, "body") {
                // BUG #21 FIX: Extract enum constants as fields
                let enum_constants = self.extract_enum_constants(body, source);
                let regular_fields = self.extract_fields(body, source);

                // Combine enum constants with regular fields
                let mut all_fields = enum_constants;
                all_fields.extend(regular_fields);

                (
                    self.extract_methods(body, source),
                    all_fields,
                    self.extract_inner_classes(body, source),
                )
            } else {
                (Vec::new(), Vec::new(), Vec::new())
            };

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(ClassInfo {
            name: name.clone(),
            bases,
            docstring,
            methods,
            fields,
            inner_classes,
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// BUG #21 FIX: Extract enum constants from enum body as FieldInfo entries.
    /// Each enum constant is treated as a public static final field of the enum type.
    fn extract_enum_constants(&self, body_node: Node, source: &[u8]) -> Vec<FieldInfo> {
        let mut constants = Vec::new();

        for child in body_node.children(&mut body_node.walk()) {
            if child.kind() == "enum_constant" {
                if let Some(field) = self.extract_enum_constant(child, source) {
                    constants.push(field);
                }
            }
        }

        constants
    }

    /// BUG #21 FIX: Extract a single enum constant as a FieldInfo.
    fn extract_enum_constant(&self, node: Node, source: &[u8]) -> Option<FieldInfo> {
        // Find the constant name
        let name = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "identifier")
            .map(|n| self.node_text(n, source).to_string())?;

        // Extract annotations if present
        let mut annotations = Vec::new();
        if let Some(modifiers) = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "modifiers")
        {
            for child in modifiers.children(&mut modifiers.walk()) {
                if matches!(child.kind(), "marker_annotation" | "annotation") {
                    annotations.push(self.extract_annotation(child, source));
                }
            }
        }

        // Check if this constant has arguments (like DAY(1) or Color.RED("ff0000"))
        let default_value = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "argument_list")
            .map(|n| self.node_text(n, source).to_string());

        Some(FieldInfo {
            name,
            field_type: Some("enum_constant".to_string()),
            visibility: Some("public".to_string()),
            is_static: true,
            is_final: true,
            default_value,
            annotations,
            line_number: node.start_position().row + 1,
        })
    }

    /// Extract record declaration (Java 14+).
    fn extract_record_declaration(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (visibility, _, _, _, _, _, annotations) = self.extract_modifiers(node, source);

        // Build decorators
        let mut decorators = vec!["record".to_string()];
        decorators.extend(annotations);
        if let Some(vis) = visibility {
            decorators.insert(1, vis);
        }

        // Extract type parameters
        if let Some(tp) = self.extract_type_parameters(node, source) {
            decorators.push(format!("generic:{}", tp));
        }

        // Records implement interfaces
        let bases: Vec<String> = self
            .extract_interfaces(node, source)
            .into_iter()
            .map(|i| format!("implements {}", i))
            .collect();

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // BUG #20 FIX: Extract methods, fields, and inner classes from record body
        let (methods, fields, inner_classes) = if let Some(body) = self.child_by_field(node, "body")
        {
            (
                self.extract_methods(body, source),
                self.extract_fields(body, source),
                self.extract_inner_classes(body, source),
            )
        } else {
            (Vec::new(), Vec::new(), Vec::new())
        };

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(ClassInfo {
            name,
            bases,
            docstring,
            methods,
            fields,
            inner_classes,
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// Extract annotation type declaration (@interface).
    fn extract_annotation_type_declaration(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        let name_node = self.child_by_field(node, "name")?;
        let name = self.node_text(name_node, source).to_string();

        // Extract modifiers
        let (visibility, _, _, _, _, _, annotations) = self.extract_modifiers(node, source);

        // Build decorators
        let mut decorators = vec!["annotation".to_string()];
        decorators.extend(annotations);
        if let Some(vis) = visibility {
            decorators.insert(1, vis);
        }

        // Extract Javadoc
        let docstring = self.extract_javadoc(node, source);

        // BUG #20 FIX: Annotation types can have methods (annotation elements), fields, and inner classes
        let (methods, fields, inner_classes) = if let Some(body) = self.child_by_field(node, "body")
        {
            (
                self.extract_methods(body, source),
                self.extract_fields(body, source),
                self.extract_inner_classes(body, source),
            )
        } else {
            (Vec::new(), Vec::new(), Vec::new())
        };

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(ClassInfo {
            name,
            bases: Vec::new(),
            docstring,
            methods,
            fields,
            inner_classes,
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// BUG #30 FIX: Extract module declaration from module-info.java (Java 9+).
    /// Module declarations define module metadata: requires, exports, opens, uses, provides.
    /// Represented as ClassInfo with "module" decorator.
    fn extract_module_declaration(&self, node: Node, source: &[u8]) -> Option<ClassInfo> {
        // Module name is the identifier or scoped_identifier
        let name = node
            .children(&mut node.walk())
            .find(|n| matches!(n.kind(), "identifier" | "scoped_identifier"))
            .map(|n| self.extract_scoped_identifier(n, source))?;

        let mut decorators = vec!["module".to_string()];
        let mut bases = Vec::new();

        // Check for open module: open module com.example { ... }
        if node.children(&mut node.walk()).any(|n| n.kind() == "open") {
            decorators.push("open".to_string());
        }

        // Extract module directives from the body
        if let Some(body) = node
            .children(&mut node.walk())
            .find(|n| n.kind() == "module_body")
        {
            for directive in body.children(&mut body.walk()) {
                match directive.kind() {
                    "requires_module_directive" => {
                        let directive_text = self.node_text(directive, source).trim().to_string();
                        bases.push(directive_text);
                    }
                    "exports_module_directive" => {
                        let directive_text = self.node_text(directive, source).trim().to_string();
                        bases.push(directive_text);
                    }
                    "opens_module_directive" => {
                        let directive_text = self.node_text(directive, source).trim().to_string();
                        bases.push(directive_text);
                    }
                    "uses_module_directive" => {
                        let directive_text = self.node_text(directive, source).trim().to_string();
                        bases.push(directive_text);
                    }
                    "provides_module_directive" => {
                        let directive_text = self.node_text(directive, source).trim().to_string();
                        bases.push(directive_text);
                    }
                    _ => {}
                }
            }
        }

        // Extract Javadoc if present
        let docstring = self.extract_javadoc(node, source);

        // Line numbers
        let line_number = node.start_position().row + 1;
        let end_line_number = Some(node.end_position().row + 1);

        Some(ClassInfo {
            name,
            bases,
            docstring,
            methods: Vec::new(),
            fields: Vec::new(),
            inner_classes: Vec::new(),
            decorators,
            line_number,
            end_line_number,
            language: "java".to_string(),
        })
    }

    /// Extract package declaration as ImportInfo.
    fn extract_package_declaration(&self, node: Node, source: &[u8]) -> Option<ImportInfo> {
        // Find the scoped identifier or identifier in the package declaration
        for child in node.children(&mut node.walk()) {
            if child.kind() == "scoped_identifier" || child.kind() == "identifier" {
                let package_name = self.extract_scoped_identifier(child, source);
                return Some(ImportInfo {
                    module: package_name,
                    names: vec![],
                    aliases: {
                        let mut m = HashMap::new();
                        m.insert("package".to_string(), "true".to_string());
                        m
                    },
                    is_from: false,
                    level: 0,
                    line_number: node.start_position().row + 1,
                    visibility: None, // Java package declarations have no visibility modifier
                });
            }
        }
        None
    }

    /// Extract import declaration.
    fn extract_import(&self, node: Node, source: &[u8]) -> Option<ImportInfo> {
        let mut is_static = false;
        let mut path_parts = Vec::new();

        for child in node.children(&mut node.walk()) {
            match child.kind() {
                "static" => {
                    is_static = true;
                }
                "scoped_identifier" | "identifier" => {
                    path_parts.push(self.extract_scoped_identifier(child, source));
                }
                "asterisk" => {
                    path_parts.push("*".to_string());
                }
                _ => {}
            }
        }

        if path_parts.is_empty() {
            return None;
        }

        let full_path = path_parts.join(".");

        // Determine module and names
        let (module, names) = if full_path.ends_with(".*") {
            // Wildcard import: import java.util.*
            let module = full_path.trim_end_matches(".*").to_string();
            (module, vec!["*".to_string()])
        } else if is_static {
            // Static import: import static java.lang.Math.PI
            // Module is everything except the last part
            let parts: Vec<&str> = full_path.rsplitn(2, '.').collect();
            if parts.len() == 2 {
                (parts[1].to_string(), vec![parts[0].to_string()])
            } else {
                (full_path.clone(), vec![])
            }
        } else {
            // Regular import: import java.util.List
            (full_path, vec![])
        };

        let mut aliases = HashMap::new();
        if is_static {
            aliases.insert("static".to_string(), "true".to_string());
        }

        let is_from = is_static || !names.is_empty();

        Some(ImportInfo {
            module,
            names,
            aliases,
            is_from,
            level: 0, // Java doesn't have relative imports
            line_number: node.start_position().row + 1,
            visibility: None, // Java imports don't have visibility modifiers (unlike Rust re-exports)
        })
    }
}

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

    fn parse_java(code: &str) -> Tree {
        let java = Java;
        let mut parser = java.parser().unwrap();
        parser.parse(code, None).unwrap()
    }

    #[test]
    fn test_extract_simple_method() {
        let code = r#"
public class Test {
    public String getName() {
        return name;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        // Find method node
        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "getName");
                assert_eq!(func.return_type, Some("String".to_string()));
                assert!(func.params.is_empty());
                assert!(func.decorators.contains(&"public".to_string()));
                break;
            }
        }
    }

    #[test]
    fn test_extract_method_with_params() {
        let code = r#"
public class Test {
    public void setName(String name, int age) {
        this.name = name;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "setName");
                assert_eq!(func.return_type, Some("void".to_string()));
                assert_eq!(func.params.len(), 2);
                assert_eq!(func.params[0], "String name");
                assert_eq!(func.params[1], "int age");
                break;
            }
        }
    }

    #[test]
    fn test_extract_generic_method() {
        let code = r#"
public class Test {
    public static <T> List<T> process(List<T> items) {
        return items;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "process");
                assert!(func.decorators.contains(&"static".to_string()));
                assert!(func.decorators.iter().any(|d| d.starts_with("generic:")));
                break;
            }
        }
    }

    #[test]
    fn test_extract_class() {
        let code = r#"
/**
 * User class.
 */
public class User extends BaseEntity implements Serializable {
    public String getName() { return name; }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "User");
                assert!(class.bases.iter().any(|b| b.contains("BaseEntity")));
                assert!(class.bases.iter().any(|b| b.contains("Serializable")));
                assert!(class.docstring.is_some());
                assert_eq!(class.methods.len(), 1);
                break;
            }
        }
    }

    #[test]
    fn test_extract_interface() {
        let code = r#"
public interface Processor<T> extends Runnable {
    void process(T item);
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "interface_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Processor");
                assert!(class.decorators.contains(&"interface".to_string()));
                assert!(class.bases.iter().any(|b| b.contains("Runnable")));
                assert_eq!(class.methods.len(), 1);
                break;
            }
        }
    }

    #[test]
    fn test_extract_enum() {
        let code = r#"
public enum Status {
    ACTIVE, INACTIVE;

    public String getCode() { return ""; }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "enum_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Status");
                assert!(class.decorators.contains(&"enum".to_string()));
                assert_eq!(class.methods.len(), 1);
                break;
            }
        }
    }

    #[test]
    fn test_extract_imports() {
        let code = r#"
import java.util.List;
import java.util.*;
import static java.lang.Math.PI;
import static java.lang.Math.*;

public class Test {}
"#;
        let tree = parse_java(code);
        let java = Java;

        let imports = java.extract_imports(&tree, code.as_bytes());
        assert_eq!(imports.len(), 4);

        // Regular import
        assert_eq!(imports[0].module, "java.util.List");
        assert!(imports[0].names.is_empty());

        // Wildcard import
        assert_eq!(imports[1].module, "java.util");
        assert_eq!(imports[1].names, vec!["*"]);

        // Static import
        assert_eq!(imports[2].module, "java.lang.Math");
        assert_eq!(imports[2].names, vec!["PI"]);
        assert!(imports[2].is_from);

        // Static wildcard import
        assert_eq!(imports[3].module, "java.lang.Math");
        assert_eq!(imports[3].names, vec!["*"]);
    }

    #[test]
    fn test_extract_constructor() {
        let code = r#"
public class User {
    /**
     * Creates a new user.
     */
    public User(String name, int age) {
        this.name = name;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "constructor_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "User");
                assert!(func.return_type.is_none());
                assert!(func.decorators.contains(&"constructor".to_string()));
                assert_eq!(func.params.len(), 2);
                assert!(func.docstring.is_some());
                break;
            }
        }
    }

    #[test]
    fn test_extract_annotations() {
        let code = r#"
public class Test {
    @Override
    @Deprecated
    public String toString() {
        return "";
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert!(func.decorators.iter().any(|d| d.contains("Override")));
                assert!(func.decorators.iter().any(|d| d.contains("Deprecated")));
                break;
            }
        }
    }

    #[test]
    fn test_build_simple_cfg() {
        let code = r#"
public class Test {
    public int add(int a, int b) {
        int sum = a + b;
        return sum;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let cfg = java.build_cfg(child, code.as_bytes()).unwrap();
                assert_eq!(cfg.function_name, "add");
                assert!(!cfg.blocks.is_empty());
                assert!(!cfg.exits.is_empty());
                break;
            }
        }
    }

    #[test]
    fn test_build_dfg() {
        let code = r#"
public class Test {
    public int compute(int x) {
        int y = x + 1;
        int z = y * 2;
        return z;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let dfg = java.build_dfg(child, code.as_bytes()).unwrap();
                assert_eq!(dfg.function_name, "compute");
                assert!(dfg.definitions.contains_key("x"));
                assert!(dfg.definitions.contains_key("y"));
                assert!(dfg.definitions.contains_key("z"));
                assert!(!dfg.edges.is_empty());
                break;
            }
        }
    }

    // =========================================================================
    // Java Feature Tests - Comprehensive verification of modern Java support
    // =========================================================================

    #[test]
    fn test_extract_record() {
        // Java 16+ record declaration
        let code = r#"
/**
 * Represents a point in 2D space.
 */
public record Point(int x, int y) implements Serializable {
    public double distance() {
        return Math.sqrt(x * x + y * y);
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "record_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Point");
                assert!(class.decorators.contains(&"record".to_string()));
                assert!(class.bases.iter().any(|b| b.contains("Serializable")));
                assert!(class.docstring.is_some());
                assert_eq!(class.methods.len(), 1);
                assert_eq!(class.methods[0].name, "distance");
                break;
            }
        }
    }

    #[test]
    fn test_extract_sealed_class() {
        // Java 17+ sealed class
        let code = r#"
public sealed class Shape permits Circle, Rectangle, Triangle {
    public abstract double area();
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Shape");
                // Note: sealed modifier extraction depends on tree-sitter-java version
                break;
            }
        }
    }

    #[test]
    fn test_extract_var_type_inference() {
        // Java 10+ var keyword
        let code = r#"
public class Test {
    public void process() {
        var items = new ArrayList<String>();
        var count = 0;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        // Verify that var is handled as a type
        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "process");
                break;
            }
        }
    }

    #[test]
    fn test_extract_annotation_type() {
        // Annotation type declaration (@interface)
        let code = r#"
/**
 * Marks a test as integration test.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface IntegrationTest {
    String value() default "";
    int timeout() default 60;
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "annotation_type_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "IntegrationTest");
                assert!(class.decorators.contains(&"annotation".to_string()));
                assert!(class.docstring.is_some());
                // Annotation elements may be extracted as methods depending on tree-sitter version
                // At minimum, the annotation type should be properly identified
                break;
            }
        }
    }

    #[test]
    fn test_extract_static_initializer() {
        // Static initializer blocks
        let code = r#"
public class Config {
    private static final Map<String, String> DEFAULTS;

    static {
        DEFAULTS = new HashMap<>();
        DEFAULTS.put("key", "value");
    }

    public String get(String key) {
        return DEFAULTS.get(key);
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Config");
                // Static initializer should be captured as a method
                assert!(class.methods.iter().any(|m| m.name.contains("static_init")));
                break;
            }
        }
    }

    #[test]
    fn test_extract_instance_initializer() {
        // Instance initializer blocks
        let code = r#"
public class Counter {
    private int count;

    {
        count = 0;
    }

    public void increment() {
        count++;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Counter");
                // Instance initializer should be captured
                assert!(class.methods.iter().any(|m| m.name.contains("instance_init")));
                break;
            }
        }
    }

    #[test]
    fn test_extract_inner_class() {
        // Inner/nested class extraction
        let code = r#"
public class Outer {
    private int x;

    public class Inner {
        public void access() {
            System.out.println(x);
        }
    }

    public static class StaticNested {
        public void process() {}
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Outer");
                // Should have 2 inner classes
                assert_eq!(class.inner_classes.len(), 2);
                assert!(class.inner_classes.iter().any(|c| c.name == "Inner"));
                assert!(class.inner_classes.iter().any(|c| c.name == "StaticNested"));
                break;
            }
        }
    }

    #[test]
    fn test_extract_fields() {
        // Field extraction with modifiers
        let code = r#"
public class Entity {
    public static final int MAX_SIZE = 100;
    private String name;
    protected int count = 0;
    @Deprecated
    volatile boolean active;
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Entity");
                assert_eq!(class.fields.len(), 4);

                // Check MAX_SIZE field
                let max_size = class.fields.iter().find(|f| f.name == "MAX_SIZE").unwrap();
                assert!(max_size.is_static);
                assert!(max_size.is_final);
                assert_eq!(max_size.visibility, Some("public".to_string()));

                // Check name field
                let name = class.fields.iter().find(|f| f.name == "name").unwrap();
                assert_eq!(name.visibility, Some("private".to_string()));
                assert_eq!(name.field_type, Some("String".to_string()));

                // Check count field with default value
                let count = class.fields.iter().find(|f| f.name == "count").unwrap();
                assert_eq!(count.visibility, Some("protected".to_string()));
                assert!(count.default_value.is_some());

                break;
            }
        }
    }

    #[test]
    fn test_extract_enum_constants() {
        // Enum constant extraction
        let code = r#"
public enum Priority {
    LOW(1),
    MEDIUM(5),
    HIGH(10);

    private final int value;

    Priority(int value) {
        this.value = value;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "enum_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Priority");
                // Enum constants should be in fields
                assert!(class.fields.iter().any(|f| f.name == "LOW"));
                assert!(class.fields.iter().any(|f| f.name == "MEDIUM"));
                assert!(class.fields.iter().any(|f| f.name == "HIGH"));
                // Enum constants should have proper type marker
                let low_field = class.fields.iter().find(|f| f.name == "LOW").unwrap();
                assert_eq!(low_field.field_type, Some("enum_constant".to_string()));
                assert!(low_field.is_static);
                assert!(low_field.is_final);
                break;
            }
        }
    }

    #[test]
    fn test_extract_package_declaration() {
        // Package declaration extraction
        let code = r#"
package com.example.myapp;

public class MyClass {}
"#;
        let tree = parse_java(code);
        let java = Java;

        let imports = java.extract_imports(&tree, code.as_bytes());
        // Package declaration should be first
        assert!(!imports.is_empty());
        let pkg = &imports[0];
        assert_eq!(pkg.module, "com.example.myapp");
        assert!(pkg.aliases.contains_key("package"));
    }

    #[test]
    fn test_extract_switch_expression() {
        // Java 14+ switch expression CFG
        let code = r#"
public class Test {
    public String getDay(int day) {
        return switch (day) {
            case 1, 2, 3, 4, 5 -> "weekday";
            case 6, 7 -> "weekend";
            default -> "invalid";
        };
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let cfg = java.build_cfg(child, code.as_bytes()).unwrap();
                assert_eq!(cfg.function_name, "getDay");
                // CFG should have multiple blocks for switch cases
                assert!(cfg.blocks.len() >= 1);
                break;
            }
        }
    }

    #[test]
    fn test_extract_pattern_matching_instanceof() {
        // Java 16+ pattern matching instanceof
        let code = r#"
public class Test {
    public void process(Object obj) {
        if (obj instanceof String s) {
            System.out.println(s.length());
        }
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                // DFG should capture the pattern variable 's'
                let dfg = java.build_dfg(child, code.as_bytes()).unwrap();
                assert_eq!(dfg.function_name, "process");
                // Pattern variable 's' should be tracked
                assert!(dfg.definitions.contains_key("s"));
                break;
            }
        }
    }

    #[test]
    fn test_extract_text_block() {
        // Java 15+ text blocks
        let code = r#"
public class Test {
    public String getJson() {
        return """
            {
                "name": "test",
                "value": 42
            }
            """;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "getJson");
                assert_eq!(func.return_type, Some("String".to_string()));
                break;
            }
        }
    }

    #[test]
    fn test_extract_module_declaration() {
        // Java 9+ module declaration
        let code = r#"
module com.example.mymodule {
    requires java.base;
    requires transitive java.sql;
    exports com.example.api;
    opens com.example.internal to com.example.test;
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "module_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "com.example.mymodule");
                assert!(class.decorators.contains(&"module".to_string()));
                // Module directives should be in bases
                assert!(class.bases.iter().any(|b| b.contains("requires")));
                assert!(class.bases.iter().any(|b| b.contains("exports")));
                break;
            }
        }
    }

    #[test]
    fn test_extract_lambda_calls() {
        // Lambda expression and method reference extraction
        let code = r#"
public class Test {
    public void process(List<String> items) {
        items.forEach(s -> System.out.println(s));
        items.stream().map(String::toUpperCase).collect(Collectors.toList());
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "process");
                break;
            }
        }
    }

    #[test]
    fn test_extract_throws_clause() {
        // Throws clause extraction
        let code = r#"
public class Test {
    public void riskyOperation() throws IOException, SQLException {
        // implementation
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert_eq!(func.name, "riskyOperation");
                // Throws clause should be in decorators
                assert!(func.decorators.iter().any(|d| d.contains("throws:")));
                break;
            }
        }
    }

    #[test]
    fn test_extract_generics_with_bounds() {
        // Generic type bounds extraction
        let code = r#"
public class Repository<T extends Entity & Serializable> {
    public <U extends Comparable<U>> List<U> sort(List<U> items) {
        return items;
    }
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        for child in root.children(&mut root.walk()) {
            if child.kind() == "class_declaration" {
                let class = java.extract_class(child, code.as_bytes()).unwrap();
                assert_eq!(class.name, "Repository");
                // Type parameters should be in decorators
                assert!(class.decorators.iter().any(|d| d.contains("generic:")));
                break;
            }
        }
    }

    #[test]
    fn test_extract_wildcard_types() {
        // Wildcard type bounds (? extends T, ? super T)
        let code = r#"
public class Test {
    public void processUpper(List<? extends Number> items) {}
    public void processLower(List<? super Integer> items) {}
    public void processUnbounded(List<?> items) {}
}
"#;
        let tree = parse_java(code);
        let java = Java;

        let root = tree.root_node();
        let class_body = root.child(0).unwrap().child_by_field_name("body").unwrap();

        let mut method_count = 0;
        for child in class_body.children(&mut class_body.walk()) {
            if child.kind() == "method_declaration" {
                let func = java.extract_function(child, code.as_bytes()).unwrap();
                assert!(func.params.len() == 1);
                method_count += 1;
            }
        }
        assert_eq!(method_count, 3);
    }
}