sqry-classpath 7.1.4

JVM classpath analysis for sqry - bytecode parsing, build system resolution, and graph integration
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
//! On-demand source JAR documentation extractor.
//!
//! Extracts Javadoc/KDoc/Scaladoc comments from source JARs when requested
//! for hover information. Results are cached in an LRU cache to avoid
//! re-reading JARs.
//!
//! # Usage
//!
//! ```rust,ignore
//! use sqry_classpath::resolve::source_jars::SourceJarProvider;
//! use sqry_classpath::resolve::ClasspathEntry;
//!
//! let provider = SourceJarProvider::new(&entries, 1000);
//! let docs = provider.get_docs("com.google.common.collect.ImmutableList", jar_path);
//! ```
//!
//! # Source file location
//!
//! Given FQN `com.example.MyClass`, the source file is located at
//! `com/example/MyClass.java` within the source JAR. For Kotlin and Scala,
//! `.kt` and `.scala` extensions are tried as fallbacks.
//!
//! # Doc comment extraction
//!
//! Extracts `/** ... */` Javadoc-style blocks and converts to plain text by:
//! - Stripping leading `*` from each line
//! - Converting HTML tags (`<p>`, `<code>`, `<pre>`) to plain text equivalents
//! - Converting inline tags (`{@code ...}`, `{@link ...}`) to readable form
//! - Formatting block tags (`@param`, `@return`, `@throws`) for display

use std::collections::HashMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use log::warn;

use super::ClasspathEntry;

/// Default LRU cache capacity for extracted documentation strings.
const DEFAULT_CACHE_SIZE: usize = 1000;

/// File extensions to search in source JARs, in priority order.
const SOURCE_EXTENSIONS: &[&str] = &["java", "kt", "scala"];

/// On-demand source JAR documentation extractor.
///
/// Extracts Javadoc/KDoc/Scaladoc comments from source JARs when requested.
/// Results are cached in an LRU cache to avoid re-reading JARs.
pub struct SourceJarProvider {
    /// Map from binary JAR path to source JAR path.
    source_jar_map: HashMap<PathBuf, PathBuf>,
    /// LRU cache: cache key -> extracted documentation string.
    /// Cache key format: `"{jar_path}::{fqn}"` or `"{jar_path}::{fqn}::{member}"`.
    /// `None` values are cached to avoid re-reading JARs for missing docs.
    cache: Mutex<lru::LruCache<String, Option<String>>>,
}

impl SourceJarProvider {
    /// Create a new provider from classpath entries.
    ///
    /// Scans entries for `source_jar` paths and builds the mapping from
    /// binary JAR path to source JAR path.
    #[must_use]
    pub fn new(entries: &[ClasspathEntry], cache_size: usize) -> Self {
        let size = if cache_size == 0 {
            DEFAULT_CACHE_SIZE
        } else {
            cache_size
        };
        let mut source_jar_map = HashMap::new();
        for entry in entries {
            if let Some(ref source_jar) = entry.source_jar {
                source_jar_map.insert(entry.jar_path.clone(), source_jar.clone());
            }
        }
        Self {
            source_jar_map,
            cache: Mutex::new(lru::LruCache::new(
                std::num::NonZeroUsize::new(size).expect("cache size must be non-zero"),
            )),
        }
    }

    /// Create a new provider with the default cache size.
    #[must_use]
    pub fn with_defaults(entries: &[ClasspathEntry]) -> Self {
        Self::new(entries, DEFAULT_CACHE_SIZE)
    }

    /// Extract documentation for a class by fully qualified name.
    ///
    /// Returns the doc comment as plain text, or `None` if:
    /// - No source JAR is available for the given binary JAR
    /// - The class source file is not found in the source JAR
    /// - The source file has no doc comment before the class declaration
    pub fn get_docs(&self, fqn: &str, jar_path: &Path) -> Option<String> {
        let cache_key = format!("{}::{fqn}", jar_path.display());

        // Check cache first.
        {
            let mut cache = self.cache.lock().expect("cache lock poisoned");
            if let Some(cached) = cache.get(&cache_key) {
                return cached.clone();
            }
        }

        let result = self.extract_class_docs(fqn, jar_path);

        // Cache the result (including None for missing docs).
        {
            let mut cache = self.cache.lock().expect("cache lock poisoned");
            cache.put(cache_key, result.clone());
        }

        result
    }

    /// Extract documentation for a specific member (method/field) within a class.
    ///
    /// Searches the source file for a doc comment immediately preceding a
    /// declaration that matches `member_name`.
    pub fn get_member_docs(&self, fqn: &str, member_name: &str, jar_path: &Path) -> Option<String> {
        let cache_key = format!("{}::{fqn}::{member_name}", jar_path.display());

        // Check cache first.
        {
            let mut cache = self.cache.lock().expect("cache lock poisoned");
            if let Some(cached) = cache.get(&cache_key) {
                return cached.clone();
            }
        }

        let result = self.extract_member_docs(fqn, member_name, jar_path);

        // Cache the result (including None for missing docs).
        {
            let mut cache = self.cache.lock().expect("cache lock poisoned");
            cache.put(cache_key, result.clone());
        }

        result
    }

    /// Read the source file content for a given FQN from the source JAR.
    ///
    /// Tries `.java`, `.kt`, and `.scala` extensions in order.
    fn read_source_from_jar(&self, fqn: &str, jar_path: &Path) -> Option<String> {
        let source_jar_path = self.source_jar_map.get(jar_path)?;

        let file = match std::fs::File::open(source_jar_path) {
            Ok(f) => f,
            Err(e) => {
                warn!(
                    "Failed to open source JAR {}: {e}",
                    source_jar_path.display()
                );
                return None;
            }
        };

        let mut archive = match zip::ZipArchive::new(file) {
            Ok(a) => a,
            Err(e) => {
                warn!(
                    "Failed to read source JAR {} as ZIP: {e}",
                    source_jar_path.display()
                );
                return None;
            }
        };

        // Convert FQN to path: com.example.MyClass -> com/example/MyClass
        let base_path = fqn.replace('.', "/");

        for ext in SOURCE_EXTENSIONS {
            let entry_path = format!("{base_path}.{ext}");
            if let Ok(mut entry) = archive.by_name(&entry_path) {
                let mut content = String::new();
                if let Err(e) = entry.read_to_string(&mut content) {
                    warn!("Failed to read {entry_path} from source JAR: {e}");
                    return None;
                }
                return Some(content);
            }
        }

        None
    }

    /// Extract the class-level doc comment from a source file.
    fn extract_class_docs(&self, fqn: &str, jar_path: &Path) -> Option<String> {
        let source = self.read_source_from_jar(fqn, jar_path)?;
        let class_name = fqn.rsplit('.').next().unwrap_or(fqn);
        extract_class_doc_comment(&source, class_name)
    }

    /// Extract a member-level doc comment from a source file.
    fn extract_member_docs(&self, fqn: &str, member_name: &str, jar_path: &Path) -> Option<String> {
        let source = self.read_source_from_jar(fqn, jar_path)?;
        extract_member_doc_comment(&source, member_name)
    }
}

/// Extract the doc comment for a class declaration from source text.
///
/// Finds the last `/** ... */` block that appears before the class/interface/object
/// declaration matching `class_name`.
fn extract_class_doc_comment(source: &str, class_name: &str) -> Option<String> {
    // Find the class declaration line.
    let class_patterns = [
        format!("class {class_name}"),
        format!("interface {class_name}"),
        format!("enum {class_name}"),
        format!("object {class_name}"),
        format!("trait {class_name}"),
        format!("record {class_name}"),
    ];

    let class_pos = class_patterns
        .iter()
        .filter_map(|pat| find_declaration_position(source, pat))
        .min()?;

    // Find the doc comment preceding this position.
    extract_preceding_doc_comment(source, class_pos)
}

/// Extract the doc comment for a member (method/field) from source text.
///
/// Finds the `/** ... */` block immediately preceding the first occurrence
/// of `member_name` in a declaration context (i.e., followed by `(` for methods
/// or preceded by a type for fields).
fn extract_member_doc_comment(source: &str, member_name: &str) -> Option<String> {
    // Search for the member name in a declaration context.
    // We look for patterns like: `memberName(`, `memberName =`, `memberName;`,
    // or just the member name preceded by whitespace and a type.
    let mut search_start = 0;
    while search_start < source.len() {
        let remaining = &source[search_start..];
        let offset = remaining.find(member_name)?;
        let abs_pos = search_start + offset;

        // Verify this is a declaration context (not a reference/call inside a method body).
        // The member name must be preceded by whitespace or declaration keywords.
        if is_declaration_context(source, abs_pos, member_name)
            && let Some(doc) = extract_preceding_doc_comment(source, abs_pos)
        {
            return Some(doc);
        }

        search_start = abs_pos + member_name.len();
    }

    None
}

/// Check if the position is likely a member declaration rather than a usage.
///
/// Simple heuristic: the character before the member name (after skipping whitespace)
/// should be a type name character, a generic closing bracket, or an annotation.
fn is_declaration_context(source: &str, pos: usize, member_name: &str) -> bool {
    // Check the character before the match.
    if pos == 0 {
        return false;
    }

    let before = &source[..pos];
    let before_trimmed = before.trim_end();
    if before_trimmed.is_empty() {
        return false;
    }

    let last_char = before_trimmed.chars().next_back().unwrap_or(' ');

    // After the member name, check for method signature `(` or field assignment/terminator.
    let after_pos = pos + member_name.len();
    let after = if after_pos < source.len() {
        source[after_pos..].trim_start()
    } else {
        ""
    };

    let after_char = after.chars().next().unwrap_or(' ');

    // Declaration context: preceded by a type (letter, >, ]) and followed by ( ; = { or end.
    let valid_before = last_char.is_alphanumeric() || last_char == '>' || last_char == ']';
    let valid_after = matches!(after_char, '(' | ';' | '=' | '{' | ':' | '\n');

    valid_before && valid_after
}

/// Find the position of a declaration keyword pattern in source text.
///
/// Ensures the match is a whole word (not part of a larger identifier).
fn find_declaration_position(source: &str, pattern: &str) -> Option<usize> {
    let mut search_start = 0;
    while search_start < source.len() {
        let remaining = &source[search_start..];
        let offset = remaining.find(pattern)?;
        let abs_pos = search_start + offset;

        // Verify whole-word boundary before the pattern.
        if abs_pos > 0 {
            let prev_char = source.as_bytes()[abs_pos - 1];
            if prev_char.is_ascii_alphanumeric() || prev_char == b'_' {
                search_start = abs_pos + pattern.len();
                continue;
            }
        }

        // Verify boundary after the pattern (should be whitespace, <, (, {, etc.).
        let end_pos = abs_pos + pattern.len();
        if end_pos < source.len() {
            let next_char = source.as_bytes()[end_pos];
            if next_char.is_ascii_alphanumeric() || next_char == b'_' {
                search_start = end_pos;
                continue;
            }
        }

        return Some(abs_pos);
    }

    None
}

/// Extract the `/** ... */` doc comment that immediately precedes the given position.
///
/// Searches backwards from `pos` for the closest `*/` and then its matching `/**`.
/// Ensures there is no non-whitespace/non-annotation content between the comment
/// end and the declaration.
fn extract_preceding_doc_comment(source: &str, pos: usize) -> Option<String> {
    let before = &source[..pos];

    // Find the last `*/` before this position.
    let comment_end = before.rfind("*/")?;
    let comment_end_full = comment_end + 2;

    // Check that between the comment end and the declaration, there's only
    // whitespace and annotations.
    let between = before[comment_end_full..].trim();
    if !between.is_empty() && !is_only_annotations_modifiers_and_types(between) {
        return None;
    }

    // Find the matching `/**`.
    let before_end = &source[..=comment_end];
    let comment_start = before_end.rfind("/**")?;

    // Extract and clean the doc comment.
    let raw_comment = &source[comment_start..comment_end_full];
    Some(clean_doc_comment(raw_comment))
}

/// Check if text between a doc comment and a declaration contains only
/// annotations, access modifiers, and type signatures.
///
/// This is intentionally permissive: it allows any identifier-like tokens
/// (type names, generics, array brackets) that can appear between a doc
/// comment and the declared member name. The key exclusions are statement
/// terminators and assignment operators that would indicate intervening code.
fn is_only_annotations_modifiers_and_types(text: &str) -> bool {
    // If the text contains statement-like constructs, it's not just declarations.
    if text.contains(';') || text.contains("return ") || text.contains("throw ") {
        return false;
    }

    for token in text.split_whitespace() {
        // Allow annotations like @Override, @Deprecated, @SuppressWarnings("...")
        if token.starts_with('@') {
            continue;
        }
        // Allow annotation arguments in parentheses.
        if token.starts_with('(') || token.ends_with(')') || token.ends_with(',') {
            continue;
        }
        // Allow string literals within annotations.
        if token.starts_with('"') || token.ends_with('"') {
            continue;
        }
        // Allow identifier-like tokens (modifiers, types, generics).
        // These match: public, static, String, List<String>, int, void,
        // Map<K,V>, Optional<T>, int[], byte[], etc.
        if is_declaration_token(token) {
            continue;
        }
        return false;
    }
    true
}

/// Check if a token looks like it belongs in a declaration signature.
///
/// Allows: identifiers, generic types (`List<String>`), array types (`int[]`),
/// keywords, wildcards (`?`), bounds (`extends`, `super`).
fn is_declaration_token(token: &str) -> bool {
    token.chars().all(|c| {
        c.is_alphanumeric()
            || c == '_'
            || c == '<'
            || c == '>'
            || c == '['
            || c == ']'
            || c == '.'
            || c == ','
            || c == '?'
    })
}

/// Clean a raw `/** ... */` doc comment into plain text.
///
/// Strips the comment delimiters, leading `*` characters, and converts
/// HTML/Javadoc markup to plain text.
fn clean_doc_comment(raw: &str) -> String {
    // Remove the opening `/**` and closing `*/`.
    let content = raw
        .strip_prefix("/**")
        .unwrap_or(raw)
        .strip_suffix("*/")
        .unwrap_or(raw);

    let mut lines: Vec<String> = Vec::new();

    for line in content.lines() {
        let trimmed = line.trim();
        // Strip leading `*` (with optional space after).
        let cleaned = if let Some(rest) = trimmed.strip_prefix("* ") {
            rest
        } else if let Some(rest) = trimmed.strip_prefix('*') {
            rest
        } else {
            trimmed
        };
        lines.push(cleaned.to_string());
    }

    let joined = lines.join("\n");
    let result = convert_html_to_plain_text(&joined);

    // Trim leading/trailing blank lines and normalize whitespace.
    let trimmed = result.trim();
    normalize_blank_lines(trimmed)
}

/// Convert HTML and Javadoc inline tags to plain text.
fn convert_html_to_plain_text(text: &str) -> String {
    let mut result = text.to_string();

    // Convert {@code text} -> `text`
    result = replace_inline_tag(&result, "code");

    // Convert {@link Type#method} -> Type.method
    result = replace_link_tags(&result);

    // Convert {@literal text} -> text
    result = replace_literal_tags(&result);

    // Convert {@value ...} -> the value reference.
    result = replace_value_tags(&result);

    // Convert HTML tags.
    result = convert_html_tags(&result);

    // Convert block tags (@param, @return, @throws, etc.).
    result = convert_block_tags(&result);

    result
}

/// Replace `{@code text}` with `` `text` ``.
fn replace_inline_tag(text: &str, tag_name: &str) -> String {
    let open_pattern = format!("{{@{tag_name} ");
    let mut result = String::with_capacity(text.len());
    let mut remaining = text;

    while let Some(start) = remaining.find(&open_pattern) {
        result.push_str(&remaining[..start]);
        let after_tag = &remaining[start + open_pattern.len()..];

        if let Some(close) = find_matching_brace(after_tag) {
            let content = &after_tag[..close];
            result.push('`');
            result.push_str(content.trim());
            result.push('`');
            remaining = &after_tag[close + 1..];
        } else {
            // No matching brace; keep as-is.
            result.push_str(&remaining[start..start + open_pattern.len()]);
            remaining = after_tag;
        }
    }
    result.push_str(remaining);
    result
}

/// Find the position of the matching `}` for a `{@tag ...}` construct,
/// handling nested braces.
fn find_matching_brace(text: &str) -> Option<usize> {
    let mut depth = 0u32;
    for (i, ch) in text.char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => {
                if depth == 0 {
                    return Some(i);
                }
                depth -= 1;
            }
            _ => {}
        }
    }
    None
}

/// Replace `{@link Type#method}` with `Type.method`.
fn replace_link_tags(text: &str) -> String {
    let open_patterns = ["{@link ", "{@linkplain "];
    let mut result = text.to_string();

    for open_pattern in &open_patterns {
        let mut new_result = String::with_capacity(result.len());
        let mut remaining = result.as_str();

        while let Some(start) = remaining.find(open_pattern) {
            new_result.push_str(&remaining[..start]);
            let after_tag = &remaining[start + open_pattern.len()..];

            if let Some(close) = find_matching_brace(after_tag) {
                let content = after_tag[..close].trim();
                // If there's a display label (space-separated), use that.
                // Otherwise, convert # to .
                let display = if let Some(label_start) = content.find(' ') {
                    content[label_start + 1..].trim()
                } else {
                    content
                };
                new_result.push_str(&display.replace('#', "."));
                remaining = &after_tag[close + 1..];
            } else {
                new_result.push_str(&remaining[start..start + open_pattern.len()]);
                remaining = after_tag;
            }
        }
        new_result.push_str(remaining);
        result = new_result;
    }

    result
}

/// Replace `{@literal text}` with backtick-wrapped text.
///
/// `{@literal}` in Javadoc means "display as-is without HTML interpretation".
/// We wrap the content in backticks to protect it from HTML tag stripping
/// and to signal that it is a literal/code-like reference.
fn replace_literal_tags(text: &str) -> String {
    let open_pattern = "{@literal ";
    let mut result = String::with_capacity(text.len());
    let mut remaining = text;

    while let Some(start) = remaining.find(open_pattern) {
        result.push_str(&remaining[..start]);
        let after_tag = &remaining[start + open_pattern.len()..];

        if let Some(close) = find_matching_brace(after_tag) {
            result.push('`');
            result.push_str(after_tag[..close].trim());
            result.push('`');
            remaining = &after_tag[close + 1..];
        } else {
            result.push_str(&remaining[start..start + open_pattern.len()]);
            remaining = after_tag;
        }
    }
    result.push_str(remaining);
    result
}

/// Replace `{@value ...}` with the reference.
fn replace_value_tags(text: &str) -> String {
    let open_pattern = "{@value ";
    let mut result = String::with_capacity(text.len());
    let mut remaining = text;

    while let Some(start) = remaining.find(open_pattern) {
        result.push_str(&remaining[..start]);
        let after_tag = &remaining[start + open_pattern.len()..];

        if let Some(close) = find_matching_brace(after_tag) {
            result.push_str(after_tag[..close].trim());
            remaining = &after_tag[close + 1..];
        } else {
            result.push_str(&remaining[start..start + open_pattern.len()]);
            remaining = after_tag;
        }
    }
    result.push_str(remaining);
    result
}

/// Convert HTML tags to plain text equivalents.
fn convert_html_tags(text: &str) -> String {
    let mut result = text.to_string();

    // <p> and </p> -> newline
    result = result.replace("<p>", "\n");
    result = result.replace("</p>", "");
    result = result.replace("<P>", "\n");

    // <br> and <br/> -> newline
    result = result.replace("<br>", "\n");
    result = result.replace("<br/>", "\n");
    result = result.replace("<br />", "\n");
    result = result.replace("<BR>", "\n");

    // <code>...</code> -> `...`
    result = result.replace("<code>", "`");
    result = result.replace("</code>", "`");
    result = result.replace("<CODE>", "`");
    result = result.replace("</CODE>", "`");

    // <pre>...</pre> -> preserve content (already plain text in code blocks).
    result = result.replace("<pre>", "");
    result = result.replace("</pre>", "");
    result = result.replace("<PRE>", "");
    result = result.replace("</PRE>", "");

    // <b>/<strong> -> keep content.
    result = result.replace("<b>", "");
    result = result.replace("</b>", "");
    result = result.replace("<strong>", "");
    result = result.replace("</strong>", "");
    result = result.replace("<B>", "");
    result = result.replace("</B>", "");

    // <i>/<em> -> keep content.
    result = result.replace("<i>", "");
    result = result.replace("</i>", "");
    result = result.replace("<em>", "");
    result = result.replace("</em>", "");
    result = result.replace("<I>", "");
    result = result.replace("</I>", "");

    // <ul>/<ol>/<li> -> basic list formatting.
    result = result.replace("<ul>", "");
    result = result.replace("</ul>", "");
    result = result.replace("<ol>", "");
    result = result.replace("</ol>", "");
    result = result.replace("<li>", "\n- ");
    result = result.replace("</li>", "");

    // <tt> (teletype) -> backticks.
    result = result.replace("<tt>", "`");
    result = result.replace("</tt>", "`");

    // Strip remaining HTML tags.
    strip_remaining_html_tags(&result)
}

/// Strip any remaining HTML tags from the text.
///
/// Content inside backticks is preserved verbatim (e.g., `` `Map<K, V>` ``
/// should not have `<K, V>` stripped as an HTML tag).
fn strip_remaining_html_tags(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut in_tag = false;
    let mut in_backtick = false;

    for ch in text.chars() {
        if ch == '`' {
            in_backtick = !in_backtick;
            result.push(ch);
        } else if in_backtick {
            // Inside backticks, preserve everything verbatim.
            result.push(ch);
        } else {
            match ch {
                '<' => in_tag = true,
                '>' if in_tag => in_tag = false,
                _ if !in_tag => result.push(ch),
                _ => {}
            }
        }
    }

    result
}

/// Convert Javadoc block tags (@param, @return, @throws, etc.) to plain text.
fn convert_block_tags(text: &str) -> String {
    let mut lines: Vec<String> = Vec::new();

    for line in text.lines() {
        let trimmed = line.trim();

        if let Some(rest) = trimmed.strip_prefix("@param ") {
            let parts: Vec<&str> = rest.splitn(2, ' ').collect();
            if parts.len() == 2 {
                lines.push(format!("param {}: {}", parts[0], parts[1]));
            } else {
                lines.push(format!("param {rest}"));
            }
        } else if let Some(rest) = trimmed.strip_prefix("@return ") {
            lines.push(format!("returns: {rest}"));
        } else if let Some(rest) = trimmed.strip_prefix("@returns ") {
            lines.push(format!("returns: {rest}"));
        } else if let Some(rest) = trimmed.strip_prefix("@throws ") {
            let parts: Vec<&str> = rest.splitn(2, ' ').collect();
            if parts.len() == 2 {
                lines.push(format!("throws {}: {}", parts[0], parts[1]));
            } else {
                lines.push(format!("throws {rest}"));
            }
        } else if let Some(rest) = trimmed.strip_prefix("@exception ") {
            let parts: Vec<&str> = rest.splitn(2, ' ').collect();
            if parts.len() == 2 {
                lines.push(format!("throws {}: {}", parts[0], parts[1]));
            } else {
                lines.push(format!("throws {rest}"));
            }
        } else if let Some(rest) = trimmed.strip_prefix("@see ") {
            lines.push(format!("see: {rest}"));
        } else if let Some(rest) = trimmed.strip_prefix("@since ") {
            lines.push(format!("since: {rest}"));
        } else if let Some(rest) = trimmed.strip_prefix("@version ") {
            lines.push(format!("version: {rest}"));
        } else if let Some(rest) = trimmed.strip_prefix("@author ") {
            lines.push(format!("author: {rest}"));
        } else if trimmed.starts_with("@deprecated") {
            let rest = trimmed
                .strip_prefix("@deprecated ")
                .unwrap_or("(deprecated)");
            lines.push(format!("DEPRECATED: {rest}"));
        } else {
            lines.push(line.to_string());
        }
    }

    lines.join("\n")
}

/// Normalize consecutive blank lines to at most one.
fn normalize_blank_lines(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut prev_blank = false;

    for line in text.lines() {
        let is_blank = line.trim().is_empty();
        if is_blank {
            if !prev_blank {
                result.push('\n');
            }
            prev_blank = true;
        } else {
            if prev_blank && !result.is_empty() {
                result.push('\n');
            }
            if !result.is_empty() && !prev_blank {
                result.push('\n');
            }
            result.push_str(line);
            prev_blank = false;
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use std::io::Write;

    use super::*;

    /// Helper: create a ZIP file in memory containing the given entries.
    fn create_source_jar(entries: &[(&str, &str)]) -> Vec<u8> {
        let mut buf = Vec::new();
        {
            let mut writer = zip::ZipWriter::new(std::io::Cursor::new(&mut buf));
            let options = zip::write::SimpleFileOptions::default()
                .compression_method(zip::CompressionMethod::Stored);
            for (path, content) in entries {
                writer.start_file((*path).to_string(), options).unwrap();
                writer.write_all(content.as_bytes()).unwrap();
            }
            writer.finish().unwrap();
        }
        buf
    }

    /// Helper: write a source JAR to a temp directory and create classpath entries.
    fn setup_provider(
        dir: &tempfile::TempDir,
        entries: &[(&str, &str)],
    ) -> (SourceJarProvider, PathBuf) {
        let source_jar_path = dir.path().join("lib-sources.jar");
        let jar_data = create_source_jar(entries);
        std::fs::write(&source_jar_path, jar_data).unwrap();

        let binary_jar_path = dir.path().join("lib.jar");
        std::fs::write(&binary_jar_path, b"fake binary jar").unwrap();

        let classpath_entries = vec![ClasspathEntry {
            jar_path: binary_jar_path.clone(),
            coordinates: Some("com.example:lib:1.0".to_string()),
            is_direct: true,
            source_jar: Some(source_jar_path),
        }];

        let provider = SourceJarProvider::new(&classpath_entries, 100);
        (provider, binary_jar_path)
    }

    #[test]
    fn test_extract_javadoc_from_simple_class() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * A simple utility class for string operations.
 *
 * This class provides common string manipulation methods.
 */
public class StringUtils {
    public static String trim(String s) {
        return s.trim();
    }
}
"#;
        let (provider, jar_path) =
            setup_provider(&dir, &[("com/example/StringUtils.java", source)]);

        let docs = provider
            .get_docs("com.example.StringUtils", &jar_path)
            .unwrap();
        assert!(docs.contains("A simple utility class for string operations"));
        assert!(docs.contains("common string manipulation methods"));
    }

    #[test]
    fn test_extract_method_level_javadoc() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * String utilities.
 */
public class StringUtils {
    /**
     * Trims whitespace from both ends of a string.
     *
     * @param s the input string
     * @return the trimmed string
     */
    public static String trim(String s) {
        return s.trim();
    }
}
"#;
        let (provider, jar_path) =
            setup_provider(&dir, &[("com/example/StringUtils.java", source)]);

        let docs = provider
            .get_member_docs("com.example.StringUtils", "trim", &jar_path)
            .unwrap();
        assert!(docs.contains("Trims whitespace from both ends"));
        assert!(docs.contains("param s:"));
        assert!(docs.contains("returns:"));
    }

    #[test]
    fn test_missing_source_jar_returns_none() {
        let entries = vec![ClasspathEntry {
            jar_path: PathBuf::from("/nonexistent/lib.jar"),
            coordinates: None,
            is_direct: true,
            source_jar: None,
        }];
        let provider = SourceJarProvider::new(&entries, 100);
        let result = provider.get_docs("com.example.Foo", Path::new("/nonexistent/lib.jar"));
        assert!(result.is_none());
    }

    #[test]
    fn test_class_not_found_in_source_jar_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let source = "package com.example;\npublic class Other {}\n";
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/Other.java", source)]);

        let result = provider.get_docs("com.example.Missing", &jar_path);
        assert!(result.is_none());
    }

    #[test]
    fn test_html_to_plain_text_conversion() {
        let input = "First paragraph.<p>Second paragraph.\n<code>some code</code> and text.";
        let result = convert_html_to_plain_text(input);
        assert!(result.contains("First paragraph."));
        assert!(result.contains("Second paragraph."));
        assert!(result.contains("`some code`"));
    }

    #[test]
    fn test_code_and_link_tag_conversion() {
        let input = "Use {@code Map<K, V>} for mappings. See {@link HashMap#get the get method}.";
        let result = convert_html_to_plain_text(input);
        assert!(result.contains("`Map<K, V>`"));
        assert!(result.contains("the get method"));
    }

    #[test]
    fn test_param_return_throws_conversion() {
        let input = "@param name the user name\n@return the greeting\n@throws IllegalArgumentException if name is null";
        let result = convert_block_tags(input);
        assert!(result.contains("param name: the user name"));
        assert!(result.contains("returns: the greeting"));
        assert!(result.contains("throws IllegalArgumentException: if name is null"));
    }

    #[test]
    fn test_cache_hit_returns_same_result() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;
/**
 * Cached class docs.
 */
public class Cached {
}
"#;
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/Cached.java", source)]);

        let first = provider.get_docs("com.example.Cached", &jar_path);
        let second = provider.get_docs("com.example.Cached", &jar_path);
        assert_eq!(first, second);
        assert!(first.is_some());

        // Verify it's actually in the cache.
        let cache = provider.cache.lock().unwrap();
        assert!(!cache.is_empty());
    }

    #[test]
    fn test_lru_eviction() {
        let dir = tempfile::tempdir().unwrap();
        let mut jar_entries = Vec::new();

        // Create 5 classes.
        for i in 0..5 {
            let name = format!("Class{i}");
            let path = format!("com/example/{name}.java");
            let content =
                format!("package com.example;\n/** Doc for {name}. */\npublic class {name} {{}}\n");
            jar_entries.push((path, content));
        }

        let jar_entry_refs: Vec<(&str, &str)> = jar_entries
            .iter()
            .map(|(p, c)| (p.as_str(), c.as_str()))
            .collect();

        let source_jar_path = dir.path().join("lib-sources.jar");
        let jar_data = create_source_jar(&jar_entry_refs);
        std::fs::write(&source_jar_path, jar_data).unwrap();

        let binary_jar_path = dir.path().join("lib.jar");
        std::fs::write(&binary_jar_path, b"fake").unwrap();

        let entries = vec![ClasspathEntry {
            jar_path: binary_jar_path.clone(),
            coordinates: None,
            is_direct: true,
            source_jar: Some(source_jar_path),
        }];

        // Cache size of 3 means older entries get evicted.
        let provider = SourceJarProvider::new(&entries, 3);

        // Access 5 classes with cache size 3.
        for i in 0..5 {
            let fqn = format!("com.example.Class{i}");
            let result = provider.get_docs(&fqn, &binary_jar_path);
            assert!(result.is_some(), "Class{i} should have docs");
        }

        // Cache should have at most 3 entries.
        let cache = provider.cache.lock().unwrap();
        assert!(cache.len() <= 3, "LRU cache should evict to capacity");
    }

    #[test]
    fn test_kotlin_extension_fallback() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example

/**
 * A Kotlin data class.
 */
data class UserProfile(val name: String, val age: Int)
"#;
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/UserProfile.kt", source)]);

        let docs = provider
            .get_docs("com.example.UserProfile", &jar_path)
            .unwrap();
        assert!(docs.contains("Kotlin data class"));
    }

    #[test]
    fn test_scala_extension_fallback() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example

/**
 * A Scala case class for configuration.
 */
case class AppConfig(host: String, port: Int)
"#;
        // Scala uses the same /** */ doc comments.
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/AppConfig.scala", source)]);

        let docs = provider
            .get_docs("com.example.AppConfig", &jar_path)
            .unwrap();
        assert!(docs.contains("Scala case class for configuration"));
    }

    #[test]
    fn test_multiline_doc_comment() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * An immutable, ordered collection of elements.
 *
 * <p>This is the second paragraph with more details.
 *
 * <p>Usage example:
 * <pre>
 *   ImmutableList<String> list = ImmutableList.of("a", "b", "c");
 * </pre>
 *
 * @param <E> the element type
 * @since 2.0
 * @see java.util.List
 * @author Google
 */
public class ImmutableList<E> {
}
"#;
        let (provider, jar_path) =
            setup_provider(&dir, &[("com/example/ImmutableList.java", source)]);

        let docs = provider
            .get_docs("com.example.ImmutableList", &jar_path)
            .unwrap();
        assert!(docs.contains("immutable, ordered collection"));
        assert!(docs.contains("second paragraph"));
        assert!(docs.contains("since: 2.0"));
        assert!(docs.contains("see: java.util.List"));
        assert!(docs.contains("author: Google"));
    }

    // --- Unit tests for internal conversion functions ---

    #[test]
    fn test_clean_doc_comment_strips_delimiters() {
        let raw = "/** Simple doc. */";
        let result = clean_doc_comment(raw);
        assert_eq!(result.trim(), "Simple doc.");
    }

    #[test]
    fn test_clean_doc_comment_strips_leading_stars() {
        let raw = "/**\n * Line one.\n * Line two.\n */";
        let result = clean_doc_comment(raw);
        assert!(result.contains("Line one."));
        assert!(result.contains("Line two."));
        assert!(!result.contains("* "));
    }

    #[test]
    fn test_replace_inline_code_tag() {
        assert_eq!(replace_inline_tag("Use {@code foo}.", "code"), "Use `foo`.");
    }

    #[test]
    fn test_replace_inline_code_tag_with_generics() {
        let result = replace_inline_tag("A {@code Map<K, V>} instance.", "code");
        assert_eq!(result, "A `Map<K, V>` instance.");
    }

    #[test]
    fn test_replace_link_tag_simple() {
        let result = replace_link_tags("See {@link String}.");
        assert_eq!(result, "See String.");
    }

    #[test]
    fn test_replace_link_tag_with_member() {
        let result = replace_link_tags("See {@link String#length}.");
        assert_eq!(result, "See String.length.");
    }

    #[test]
    fn test_replace_link_tag_with_label() {
        let result = replace_link_tags("See {@link String#length the length method}.");
        assert_eq!(result, "See the length method.");
    }

    #[test]
    fn test_strip_remaining_html_tags() {
        let input = "Hello <b>world</b> and <unknown-tag>foo</unknown-tag>.";
        let result = strip_remaining_html_tags(input);
        assert_eq!(result, "Hello world and foo.");
    }

    #[test]
    fn test_normalize_blank_lines() {
        let input = "Line 1\n\n\n\nLine 2\n\nLine 3";
        let result = normalize_blank_lines(input);
        // Should have at most one blank line between content lines.
        assert!(!result.contains("\n\n\n"));
    }

    #[test]
    fn test_find_matching_brace_simple() {
        assert_eq!(find_matching_brace("text}rest"), Some(4));
    }

    #[test]
    fn test_find_matching_brace_nested() {
        assert_eq!(find_matching_brace("a{b}c}rest"), Some(5));
    }

    #[test]
    fn test_is_only_annotations_modifiers_and_types() {
        assert!(is_only_annotations_modifiers_and_types("@Override public"));
        assert!(is_only_annotations_modifiers_and_types("@Deprecated"));
        assert!(is_only_annotations_modifiers_and_types(
            "public static final"
        ));
        assert!(!is_only_annotations_modifiers_and_types("int x = 5;"));
    }

    #[test]
    fn test_deprecated_tag_conversion() {
        let input = "@deprecated Use newMethod() instead.";
        let result = convert_block_tags(input);
        assert!(result.contains("DEPRECATED: Use newMethod() instead."));
    }

    #[test]
    fn test_exception_tag_conversion() {
        let input = "@exception IOException if I/O fails";
        let result = convert_block_tags(input);
        assert!(result.contains("throws IOException: if I/O fails"));
    }

    #[test]
    fn test_literal_tag_conversion() {
        let input = "Use {@literal <T>} for generics.";
        let result = convert_html_to_plain_text(input);
        assert!(result.contains("<T>"));
    }

    #[test]
    fn test_class_with_annotations() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * Deprecated utility class.
 */
@Deprecated
@SuppressWarnings("unused")
public final class OldUtils {
}
"#;
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/OldUtils.java", source)]);

        let docs = provider
            .get_docs("com.example.OldUtils", &jar_path)
            .unwrap();
        assert!(docs.contains("Deprecated utility class"));
    }

    #[test]
    fn test_interface_doc_extraction() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * A service interface for user management.
 */
public interface UserService {
    void createUser(String name);
}
"#;
        let (provider, jar_path) =
            setup_provider(&dir, &[("com/example/UserService.java", source)]);

        let docs = provider
            .get_docs("com.example.UserService", &jar_path)
            .unwrap();
        assert!(docs.contains("service interface for user management"));
    }

    #[test]
    fn test_enum_doc_extraction() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * Represents the status of an order.
 */
public enum OrderStatus {
    PENDING, SHIPPED, DELIVERED
}
"#;
        let (provider, jar_path) =
            setup_provider(&dir, &[("com/example/OrderStatus.java", source)]);

        let docs = provider
            .get_docs("com.example.OrderStatus", &jar_path)
            .unwrap();
        assert!(docs.contains("status of an order"));
    }

    #[test]
    fn test_no_doc_comment_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

public class NoDoc {
}
"#;
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/NoDoc.java", source)]);

        let result = provider.get_docs("com.example.NoDoc", &jar_path);
        assert!(result.is_none());
    }

    #[test]
    fn test_member_not_found_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

/**
 * A class.
 */
public class MyClass {
    public void existingMethod() {}
}
"#;
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/MyClass.java", source)]);

        let result = provider.get_member_docs("com.example.MyClass", "nonExistent", &jar_path);
        assert!(result.is_none());
    }

    #[test]
    fn test_cache_none_for_missing_docs() {
        let dir = tempfile::tempdir().unwrap();
        let source = "package com.example;\npublic class NoDoc {}\n";
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/NoDoc.java", source)]);

        // First call caches None.
        let first = provider.get_docs("com.example.NoDoc", &jar_path);
        assert!(first.is_none());

        // Second call should hit cache (also None).
        let second = provider.get_docs("com.example.NoDoc", &jar_path);
        assert!(second.is_none());

        // Verify it's cached.
        let cache = provider.cache.lock().unwrap();
        let key = format!("{}::com.example.NoDoc", jar_path.display());
        assert!(cache.peek(&key).is_some());
    }

    #[test]
    fn test_with_defaults_constructor() {
        let entries = vec![];
        let provider = SourceJarProvider::with_defaults(&entries);
        // Should not panic and should have empty map.
        assert!(provider.source_jar_map.is_empty());
    }

    #[test]
    fn test_multiple_jars_mapping() {
        let dir = tempfile::tempdir().unwrap();

        // Create two source JARs with different classes.
        let source_a = "package com.a;\n/** Class A. */\npublic class A {}\n";
        let source_b = "package com.b;\n/** Class B. */\npublic class B {}\n";

        let jar_a_src = dir.path().join("a-sources.jar");
        let jar_b_src = dir.path().join("b-sources.jar");
        let jar_a_bin = dir.path().join("a.jar");
        let jar_b_bin = dir.path().join("b.jar");

        std::fs::write(&jar_a_src, create_source_jar(&[("com/a/A.java", source_a)])).unwrap();
        std::fs::write(&jar_b_src, create_source_jar(&[("com/b/B.java", source_b)])).unwrap();
        std::fs::write(&jar_a_bin, b"fake").unwrap();
        std::fs::write(&jar_b_bin, b"fake").unwrap();

        let entries = vec![
            ClasspathEntry {
                jar_path: jar_a_bin.clone(),
                coordinates: None,
                is_direct: true,
                source_jar: Some(jar_a_src),
            },
            ClasspathEntry {
                jar_path: jar_b_bin.clone(),
                coordinates: None,
                is_direct: true,
                source_jar: Some(jar_b_src),
            },
        ];

        let provider = SourceJarProvider::new(&entries, 100);

        let docs_a = provider.get_docs("com.a.A", &jar_a_bin).unwrap();
        assert!(docs_a.contains("Class A"));

        let docs_b = provider.get_docs("com.b.B", &jar_b_bin).unwrap();
        assert!(docs_b.contains("Class B"));

        // Cross-lookup should fail.
        assert!(provider.get_docs("com.a.A", &jar_b_bin).is_none());
    }

    #[test]
    fn test_field_member_docs() {
        let dir = tempfile::tempdir().unwrap();
        let source = r#"package com.example;

public class Config {
    /**
     * The maximum number of retries.
     */
    public static final int MAX_RETRIES = 3;

    /**
     * The default timeout in milliseconds.
     */
    private long timeout = 5000;
}
"#;
        let (provider, jar_path) = setup_provider(&dir, &[("com/example/Config.java", source)]);

        let docs = provider
            .get_member_docs("com.example.Config", "MAX_RETRIES", &jar_path)
            .unwrap();
        assert!(docs.contains("maximum number of retries"));

        let docs = provider
            .get_member_docs("com.example.Config", "timeout", &jar_path)
            .unwrap();
        assert!(docs.contains("default timeout in milliseconds"));
    }
}