codanna 0.9.19

Code Intelligence for Large Language Models
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
//! Java language behavior implementation
//!
//! Provides language-specific behaviors for Java.
//!
//! TODO: Implement methods after exploring actual Java AST with tree-sitter.

use crate::parsing::{
    Import, InheritanceResolver, LanguageBehavior, ResolutionScope,
    behavior_state::{BehaviorState, StatefulBehavior},
    paths::strip_extension,
};
use crate::symbol::ScopeContext;
use crate::{FileId, Symbol, SymbolKind, Visibility};
use std::path::{Path, PathBuf};

/// Behavior handler for Java language
#[derive(Clone)]
pub struct JavaBehavior {
    state: BehaviorState,
}

impl JavaBehavior {
    pub fn new() -> Self {
        Self {
            state: BehaviorState::new(),
        }
    }
}

impl Default for JavaBehavior {
    fn default() -> Self {
        Self::new()
    }
}

impl StatefulBehavior for JavaBehavior {
    fn state(&self) -> &BehaviorState {
        &self.state
    }
}

impl LanguageBehavior for JavaBehavior {
    fn language_id(&self) -> crate::parsing::registry::LanguageId {
        crate::parsing::registry::LanguageId::new("java")
    }

    /// Format module path for Java packages using dot notation
    fn format_module_path(&self, base_path: &str, symbol_name: &str) -> String {
        if base_path.is_empty() {
            symbol_name.to_string()
        } else if symbol_name == "<file>" {
            base_path.to_string()
        } else {
            format!("{base_path}.{symbol_name}")
        }
    }

    /// Parse visibility from signature
    ///
    /// Java visibility levels:
    /// - public: accessible everywhere
    /// - protected: accessible in same package + subclasses
    /// - package-private: accessible in same package only (default when no modifier)
    /// - private: accessible only within class
    fn parse_visibility(&self, signature: &str) -> Visibility {
        let trimmed = signature.trim();

        if trimmed.contains("private") {
            Visibility::Private
        } else if trimmed.contains("protected") {
            Visibility::Module // Protected in Java
        } else if trimmed.contains("public") {
            Visibility::Public
        } else {
            Visibility::Crate // Package-private (Java default when no modifier)
        }
    }

    /// Get module separator for Java (dot notation)
    fn module_separator(&self) -> &'static str {
        "." // Java uses dot separation for packages
    }

    fn format_path_as_module(&self, components: &[&str]) -> Option<String> {
        if components.is_empty() {
            None
        } else {
            Some(components.join("."))
        }
    }

    /// Java supports interfaces (similar to traits)
    fn supports_traits(&self) -> bool {
        true
    }

    /// Java defines methods within class bodies (not as inherent impls)
    fn supports_inherent_methods(&self) -> bool {
        false
    }

    /// Get tree-sitter language
    fn get_language(&self) -> tree_sitter::Language {
        tree_sitter_java::LANGUAGE.into()
    }

    /// Validate node kind (tree-sitter ABI compatibility)
    fn validate_node_kind(&self, _node_kind: &str) -> bool {
        true
    }

    /// Get ABI version for Java grammar
    fn get_abi_version(&self) -> usize {
        15 // Java grammar uses ABI-15
    }

    /// Normalize caller name for better matching
    /// TODO: Implement if needed for Java-specific normalization
    fn normalize_caller_name(&self, name: &str, _file_id: crate::FileId) -> String {
        name.to_string()
    }

    /// Configure symbol with module path and other metadata
    ///
    /// This method is called for every symbol during parsing to set:
    /// - module_path: The package path (e.g., "com.example")
    /// - visibility: Parsed from the signature (public, protected, private, package-private)
    fn configure_symbol(&self, symbol: &mut Symbol, module_path: Option<&str>) {
        // Set module path (package)
        if let Some(path) = module_path {
            symbol.module_path = Some(path.to_string().into());
        }

        // Visibility is already set by parser's determine_visibility() during symbol extraction
        // Do not overwrite it here, as the signature may not contain modifier keywords
    }

    /// Check if import matches symbol (trait implementation)
    ///
    /// Java imports include the symbol name: `import com.example.Person;`
    /// Must split into module (`com.example`) and symbol (`Person`) for matching.
    fn import_matches_symbol(
        &self,
        import_path: &str,
        symbol_module_path: &str,
        importing_module: Option<&str>,
    ) -> bool {
        // Strip "static " prefix if present
        let import_path = import_path.strip_prefix("static ").unwrap_or(import_path);

        // Handle wildcard imports: import com.example.*
        if let Some(base) = import_path.strip_suffix(".*") {
            // Wildcard matches symbols in that exact package (not nested)
            return symbol_module_path == base;
        }

        // For single-type imports: import com.example.Person
        // Split into package and class name
        if let Some((import_package, _import_class)) = import_path.rsplit_once('.') {
            // Match if packages are the same
            if import_package == symbol_module_path {
                return true;
            }
        }

        // Same package: symbols in same package don't need imports
        if let Some(current_pkg) = importing_module {
            if current_pkg == symbol_module_path {
                return true;
            }
        }

        false
    }

    /// Convert file path to Java package path
    ///
    /// Uses cached resolution rules to map file paths to Java packages.
    /// Requires that JavaProvider.rebuild_cache() has been called with project settings.
    // Rule-based resolution: uses pre-computed mappings from JavaProvider.rebuild_cache()
    fn module_path_from_file(
        &self,
        file_path: &Path,
        project_root: &Path,
        extensions: &[&str],
    ) -> Option<String> {
        use crate::project_resolver::persist::ResolutionPersistence;
        use std::cell::RefCell;
        use std::time::{Duration, Instant};

        // Validate file has a valid Java extension
        let file_name = file_path.to_str()?;
        let _ = strip_extension(file_name, extensions);

        // Thread-local cache with 1-second TTL (per TypeScript pattern)
        thread_local! {
            static RULES_CACHE: RefCell<Option<(Instant, crate::project_resolver::persist::ResolutionIndex)>> = const { RefCell::new(None) };
        }

        RULES_CACHE.with(|cache| {
            let mut cache_ref = cache.borrow_mut();

            // Check if cache needs reload (>1 second old or empty)
            let needs_reload = cache_ref
                .as_ref()
                .map(|(ts, _)| ts.elapsed() >= Duration::from_secs(1))
                .unwrap_or(true);

            // Load from disk if needed
            if needs_reload {
                let persistence =
                    ResolutionPersistence::new(std::path::Path::new(crate::init::local_dir_name()));
                if let Ok(index) = persistence.load("java") {
                    *cache_ref = Some((Instant::now(), index));
                } else {
                    return None;
                }
            }

            // Get rules for this file
            if let Some((_, ref index)) = *cache_ref {
                // Canonicalize file path for matching
                let canon_file = file_path.canonicalize().ok()?;

                // Find config that applies to this file
                let config_path = index.get_config_for_file(&canon_file)?;
                let rules = index.rules.get(config_path)?;

                // Extract package from file path using source roots
                for root_path in rules.paths.keys() {
                    let root = std::path::Path::new(root_path);

                    // Canonicalize root path if it exists (runtime resolution)
                    let canon_root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

                    if let Ok(relative) = canon_file.strip_prefix(&canon_root) {
                        // Convert path to package: com/example/Foo.java → com.example
                        // Use parent() to get directory (strips file name including extension)
                        let package_path = relative
                            .parent()?
                            .to_string_lossy()
                            .replace(['/', '\\'], ".");

                        return Some(package_path);
                    }
                }
            }

            // Fallback: use project_root-based resolution
            let relative = file_path.strip_prefix(project_root).ok()?;
            let package_path = relative
                .parent()?
                .to_string_lossy()
                .replace(['/', '\\'], ".");

            Some(package_path)
        })
    }

    /// Get module path for a file (delegates to BehaviorState)
    ///
    /// This override is CRITICAL for same-package symbol resolution.
    /// Without it, the default implementation returns None and same-package
    /// symbols won't be added to the resolution context.
    fn get_module_path_for_file(&self, file_id: FileId) -> Option<String> {
        self.state.get_module_path(file_id)
    }

    /// Get file path for a FileId (delegates to BehaviorState)
    ///
    /// Required for load_project_rules_for_file to work.
    fn get_file_path(&self, file_id: FileId) -> Option<PathBuf> {
        self.state.get_file_path(file_id)
    }

    /// Build resolution context for parallel pipeline (no Tantivy).
    ///
    /// Java doesn't have path aliases like TypeScript/JavaScript.
    /// Imports are package-based (e.g., `import com.example.MyClass`).
    ///
    /// CRITICAL: Symbols from pipeline have `module_path: None`.
    /// We compute module_path on-the-fly from `symbol.file_path` using rules.
    fn build_resolution_context_with_pipeline_cache(
        &self,
        file_id: FileId,
        imports: &[crate::parsing::Import],
        cache: &dyn crate::parsing::PipelineSymbolCache,
        extensions: &[&str],
    ) -> (
        Box<dyn crate::parsing::ResolutionScope>,
        Vec<crate::parsing::Import>,
    ) {
        use crate::parsing::ScopeLevel;
        use crate::parsing::resolution::{ImportBinding, ImportOrigin};
        use std::path::PathBuf;

        let mut context = super::JavaResolutionContext::new(file_id);

        // Helper to compute module_path from file_path using rules
        // Rules contain source roots; module_path_from_file extracts package
        let compute_module_path = |file_path: &str| -> Option<String> {
            let path = PathBuf::from(file_path);
            // project_root is unused by Java's module_path_from_file (uses rules instead)
            self.module_path_from_file(&path, &PathBuf::new(), extensions)
        };

        // Compute importing module from current file
        let importing_module = cache
            .symbols_in_file(file_id)
            .first()
            .and_then(|id| cache.get(*id))
            .and_then(|sym| compute_module_path(&sym.file_path));

        // Java imports are already fully qualified (com.example.MyClass)
        let mut enhanced_imports = Vec::with_capacity(imports.len());

        for import in imports {
            // Extract the class name from the import path
            // e.g., "com.example.MyClass" -> "MyClass"
            let local_name = import.alias.clone().unwrap_or_else(|| {
                import
                    .path
                    .rsplit('.')
                    .next()
                    .unwrap_or(&import.path)
                    .to_string()
            });

            // The target module is the package (everything before the class name)
            // e.g., "com.example.MyClass" -> "com.example"
            let target_module = if let Some(pos) = import.path.rfind('.') {
                import.path[..pos].to_string()
            } else {
                import.path.clone()
            };

            // Collect enhanced import (Java imports are already canonical)
            enhanced_imports.push(crate::parsing::Import {
                path: target_module.clone(),
                file_id: import.file_id,
                alias: import.alias.clone(),
                is_glob: import.is_glob,
                is_type_only: import.is_type_only,
            });

            // Look up candidates by class name and match computed module_path
            let mut resolved_symbol: Option<crate::SymbolId> = None;
            let candidates = cache.lookup_candidates(&local_name);
            for id in candidates {
                if let Some(symbol) = cache.get(id) {
                    // Compute module_path from file_path using rules
                    if let Some(computed_module) = compute_module_path(&symbol.file_path) {
                        if computed_module == target_module {
                            resolved_symbol = Some(id);
                            break;
                        }
                    }
                }
            }

            // Determine origin
            let origin = if resolved_symbol.is_some() {
                ImportOrigin::Internal
            } else {
                ImportOrigin::External
            };

            // Register binding
            context.register_import_binding(ImportBinding {
                import: import.clone(),
                exposed_name: local_name.clone(),
                origin,
                resolved_symbol,
            });

            if let (ImportOrigin::Internal, Some(symbol_id)) = (origin, resolved_symbol) {
                context.add_symbol(local_name.clone(), symbol_id, ScopeLevel::Global);
            }
        }

        // Populate context with enhanced imports
        context.populate_imports(&enhanced_imports);

        // Add local symbols from this file with computed module_path
        for sym_id in cache.symbols_in_file(file_id) {
            if let Some(symbol) = cache.get(sym_id) {
                if self.is_resolvable_symbol(&symbol) {
                    context.add_symbol(symbol.name.to_string(), symbol.id, ScopeLevel::Module);
                    // Compute module_path from file_path using rules
                    if let Some(computed_module) = compute_module_path(&symbol.file_path) {
                        context.add_symbol(computed_module, symbol.id, ScopeLevel::Global);
                    }
                }
            }
        }

        // Same-package symbols: symbols with computed same module_path get Package scope
        if let Some(ref current_pkg) = importing_module {
            for sym_id in cache.symbols_in_file(file_id) {
                if let Some(symbol) = cache.get(sym_id) {
                    if let Some(computed_module) = compute_module_path(&symbol.file_path) {
                        if &computed_module == current_pkg {
                            context.add_symbol(
                                symbol.name.to_string(),
                                symbol.id,
                                ScopeLevel::Package,
                            );
                        }
                    }
                }
            }
        }

        (Box::new(context), enhanced_imports)
    }

    /// Register file with behavior state (trait override)
    ///
    /// This override is CRITICAL - without it, module paths won't be stored
    /// and same-package resolution won't work.
    fn register_file(&self, path: PathBuf, file_id: FileId, module_path: String) {
        self.state.register_file(path, file_id, module_path);
    }
}

impl JavaBehavior {
    // =========================================================================
    // ADDITIONAL BEHAVIOR METHODS (matching Kotlin's API)
    // =========================================================================

    /// Create resolution context for this file
    pub fn create_resolution_context(&self, file_id: FileId) -> Box<dyn ResolutionScope> {
        Box::new(super::JavaResolutionContext::new(file_id))
    }

    /// Create inheritance resolver
    pub fn create_inheritance_resolver(&self) -> Box<dyn InheritanceResolver> {
        Box::new(super::JavaInheritanceResolver::new())
    }

    /// Register file in behavior state
    pub fn register_file(&self, path: PathBuf, file_id: FileId, module_path: String) {
        self.state.register_file(path, file_id, module_path);
    }

    /// Add import to behavior state
    pub fn add_import(&self, import: Import) {
        self.state.add_import(import);
    }

    /// Get all imports for a file
    pub fn get_imports_for_file(&self, file_id: FileId) -> Vec<Import> {
        self.state.get_imports_for_file(file_id)
    }

    /// Get module path for a file
    pub fn get_module_path_for_file(&self, file_id: FileId) -> Option<String> {
        self.state.get_module_path(file_id)
    }

    /// Register expression types for a file
    /// TODO: Implement if Java needs type inference tracking
    pub fn register_expression_types(&self, _file_id: FileId, _entries: &[(String, String)]) {
        // TODO: Implement if needed for Java type inference
    }

    /// Initialize resolution context with imports and file state
    ///
    /// This method:
    /// 1. Retrieves imports for this file from BehaviorState
    /// 2. Populates the resolution context with those imports
    /// 3. Adds package-level symbols to enable package-private access
    pub fn initialize_resolution_context(
        &self,
        context: &mut dyn ResolutionScope,
        file_id: FileId,
    ) {
        // Downcast to JavaResolutionContext to access Java-specific methods
        if context
            .as_any_mut()
            .downcast_mut::<super::JavaResolutionContext>()
            .is_some()
        {
            // Get imports for this file from BehaviorState
            let imports = self.state.get_imports_for_file(file_id);

            // Populate imports into the context
            context.populate_imports(&imports);

            // Note: Package-level symbols will be added by the indexer
            // when it processes all files in the same package
        }
    }

    /// Check if import matches symbol (legacy - not used by resolution system)
    #[allow(dead_code)]
    fn import_matches_symbol_legacy(
        &self,
        import_path: &str,
        symbol: &Symbol,
        current_package: Option<&str>,
    ) -> bool {
        // Get symbol's module path
        let symbol_module_path = match symbol.module_path {
            Some(ref path) => path.as_ref(),
            None => return false,
        };

        // Strip "static " prefix if present (static imports encoded in path)
        let import_path = import_path.strip_prefix("static ").unwrap_or(import_path);

        // Build full symbol path: package.ClassName
        // For nested classes: package.OuterClass.InnerClass
        let symbol_full_path = if symbol_module_path.is_empty() {
            symbol.name.as_ref()
        } else {
            // Symbol name might already be qualified, check if module_path ends with class name
            if symbol_module_path.ends_with(symbol.name.as_ref()) {
                symbol_module_path
            } else {
                // Need to construct: module_path.name
                return self.check_import_match(
                    import_path,
                    symbol_module_path,
                    &symbol.name,
                    current_package,
                );
            }
        };

        // Exact match: import com.example.MyClass
        if import_path == symbol_full_path {
            return true;
        }

        // Wildcard imports: import com.example.*
        if let Some(base) = import_path.strip_suffix(".*") {
            // Check if symbol is in this package
            if let Some(stripped) = symbol_full_path.strip_prefix(base) {
                if let Some(remainder) = stripped.strip_prefix('.') {
                    // Only match direct children (no more dots)
                    return !remainder.contains('.');
                }
            }
        }

        // Same package: no import needed if in same package
        if let Some(current_pkg) = current_package {
            if let Some((symbol_pkg, _)) = symbol_full_path.rsplit_once('.') {
                if current_pkg == symbol_pkg {
                    return true;
                }
            }
        }

        false
    }

    /// Helper to check import match when constructing qualified name
    fn check_import_match(
        &self,
        import_path: &str,
        module_path: &str,
        symbol_name: &str,
        current_package: Option<&str>,
    ) -> bool {
        // Construct full path: module_path.symbol_name
        let full_path = format!("{module_path}.{symbol_name}");

        // Exact match
        if import_path == full_path {
            return true;
        }

        // Wildcard match
        if let Some(base) = import_path.strip_suffix(".*") {
            if module_path == base {
                return true;
            }
        }

        // Same package
        if let Some(current_pkg) = current_package {
            if module_path == current_pkg {
                return true;
            }
        }

        false
    }

    /// Check if symbol is resolvable (has enough information)
    ///
    /// Filters which symbols can participate in resolution based on:
    /// 1. Symbol kind (classes, methods, fields, etc. are resolvable)
    /// 2. Scope context (excludes local variables and parameters)
    pub fn is_resolvable_symbol(&self, symbol: &Symbol) -> bool {
        use crate::symbol::ScopeContext;

        // Java resolves classes, methods, fields, interfaces, enums, etc.
        let resolvable_kind = matches!(
            symbol.kind,
            SymbolKind::Function
                | SymbolKind::Class
                | SymbolKind::Interface
                | SymbolKind::Method
                | SymbolKind::Field
                | SymbolKind::Enum
                | SymbolKind::Constant
        );

        if !resolvable_kind {
            return false;
        }

        // Check scope context - exclude local variables and parameters
        if let Some(ref scope_context) = symbol.scope_context {
            matches!(
                scope_context,
                ScopeContext::Module
                    | ScopeContext::Global
                    | ScopeContext::ClassMember { .. }
                    | ScopeContext::Package
            )
        } else {
            true // No scope context = resolvable
        }
    }

    /// Check if symbol is visible from another file
    ///
    /// Implements Java visibility rules:
    /// - Public: visible everywhere
    /// - Protected: visible in same package + subclasses
    /// - Package-private (default): visible in same package only
    /// - Private: not visible outside class/file
    ///
    /// Check if symbol is visible from another file (backward compatible)
    ///
    /// Delegates to is_symbol_visible_from_context() without inheritance context.
    /// For full inheritance-based protected checks, use is_symbol_visible_from_context().
    pub fn is_symbol_visible_from_file(&self, symbol: &Symbol, from_file: FileId) -> bool {
        // Delegate to enhanced method without inheritance context
        // This makes protected symbols permissive for cross-package access
        let resolver = super::JavaInheritanceResolver::new();
        self.is_symbol_visible_from_context(symbol, from_file, None, &resolver)
    }

    /// Check if a symbol and a file are in the same package
    ///
    /// Compares the symbol's module_path with the package of the given file.
    /// Returns true if both are in the same Java package.
    fn is_same_package(&self, symbol: &Symbol, file_id: FileId) -> bool {
        // Get the package of the file asking for access
        let from_package = self.state.get_module_path(file_id);

        // Get the package of the symbol
        let symbol_package = symbol.module_path.as_deref();

        // Compare packages
        match (symbol_package, from_package.as_deref()) {
            (Some(sym_pkg), Some(from_pkg)) => sym_pkg == from_pkg,
            (None, None) => true, // Both in default package
            _ => false,           // One has package, other doesn't
        }
    }

    /// Get the fully qualified class name containing this symbol
    ///
    /// Extracts the class name from ClassMember scope context and combines
    /// with module_path (package) to form fully qualified name.
    ///
    /// Returns:
    /// - For methods/fields: "com.example.MyClass" or "com.example.Outer.Inner"
    /// - For default package: "MyClass"
    /// - For non-members: None
    fn get_containing_class(&self, symbol: &Symbol) -> Option<String> {
        if let Some(ScopeContext::ClassMember {
            class_name: Some(class),
        }) = &symbol.scope_context
        {
            // Combine package + class for fully qualified name
            if let Some(pkg) = &symbol.module_path {
                if pkg.is_empty() {
                    return Some(class.to_string()); // Default package
                }
                return Some(format!("{pkg}.{class}")); // Qualified
            }
            return Some(class.to_string()); // No package info
        }
        None
    }

    /// Check if symbol is visible from another file with full inheritance support
    ///
    /// Enhanced visibility check that supports:
    /// - Same package access
    /// - Inheritance-based protected access (requires inheritance resolver)
    ///
    /// Parameters:
    /// - `symbol`: The symbol being accessed
    /// - `from_file`: The file attempting access
    /// - `accessing_class`: Optional fully qualified class name doing the access
    /// - `inheritance`: Inheritance resolver for subclass checks
    pub fn is_symbol_visible_from_context(
        &self,
        symbol: &Symbol,
        from_file: FileId,
        accessing_class: Option<&str>,
        inheritance: &dyn InheritanceResolver,
    ) -> bool {
        // Same file: always visible
        if symbol.file_id == from_file {
            return true;
        }

        // Check visibility modifiers
        match symbol.visibility {
            Visibility::Private => false, // Private symbols are class-scoped only
            Visibility::Crate => {
                // Package-private (Java default) - same package only
                self.is_same_package(symbol, from_file)
            }
            Visibility::Module => {
                // Protected - accessible in same package + subclasses

                // Same package: always grants access
                if self.is_same_package(symbol, from_file) {
                    return true;
                }

                // Cross-package: check inheritance if context available
                if let Some(accessing) = accessing_class {
                    if let Some(containing) = self.get_containing_class(symbol) {
                        return inheritance.is_subtype(accessing, &containing);
                    }
                }

                // No context for inheritance check: be permissive
                // The compiler will catch actual violations
                true
            }
            Visibility::Public => true, // Public symbols visible everywhere
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{FileId, Range, SymbolId};

    #[test]
    fn test_protected_visibility_same_package() {
        let behavior = JavaBehavior::new();

        // Create a protected method in com.example.Parent
        let mut symbol = Symbol::new(
            SymbolId(1),
            "protectedMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 10,
                start_column: 5,
                end_line: 12,
                end_column: 6,
            },
        );
        symbol.module_path = Some("com.example".to_string().into());
        symbol.visibility = Visibility::Module; // Protected
        symbol.scope_context = Some(ScopeContext::ClassMember {
            class_name: Some("Parent".to_string().into()),
        });

        // Register file in same package
        behavior.register_file(
            "com/example/Child.java".into(),
            FileId(2),
            "com.example".to_string(),
        );

        // Test: Same package access should succeed
        let resolver = super::super::JavaInheritanceResolver::new();
        assert!(
            behavior.is_symbol_visible_from_context(
                &symbol,
                FileId(2),
                Some("com.example.Child"),
                &resolver
            ),
            "Protected symbol should be visible in same package"
        );
    }

    #[test]
    fn test_protected_visibility_cross_package_with_inheritance() {
        let behavior = JavaBehavior::new();

        // Create a protected method in com.example.Parent
        let mut symbol = Symbol::new(
            SymbolId(1),
            "protectedMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 10,
                start_column: 5,
                end_line: 12,
                end_column: 6,
            },
        );
        symbol.module_path = Some("com.example".to_string().into());
        symbol.visibility = Visibility::Module; // Protected
        symbol.scope_context = Some(ScopeContext::ClassMember {
            class_name: Some("Parent".to_string().into()),
        });

        // Register file in different package
        behavior.register_file(
            "com/other/Child.java".into(),
            FileId(2),
            "com.other".to_string(),
        );

        // Setup inheritance: Child extends Parent
        let mut resolver = super::super::JavaInheritanceResolver::new();
        resolver.add_inheritance(
            "com.other.Child".to_string(),
            "com.example.Parent".to_string(),
            "extends",
        );

        // Test: Cross-package access should succeed for subclass
        assert!(
            behavior.is_symbol_visible_from_context(
                &symbol,
                FileId(2),
                Some("com.other.Child"),
                &resolver
            ),
            "Protected symbol should be visible to subclass in different package"
        );
    }

    #[test]
    fn test_protected_visibility_cross_package_without_inheritance() {
        let behavior = JavaBehavior::new();

        // Create a protected method in com.example.Parent
        let mut symbol = Symbol::new(
            SymbolId(1),
            "protectedMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 10,
                start_column: 5,
                end_line: 12,
                end_column: 6,
            },
        );
        symbol.module_path = Some("com.example".to_string().into());
        symbol.visibility = Visibility::Module; // Protected
        symbol.scope_context = Some(ScopeContext::ClassMember {
            class_name: Some("Parent".to_string().into()),
        });

        // Register file in different package
        behavior.register_file(
            "com/other/Unrelated.java".into(),
            FileId(2),
            "com.other".to_string(),
        );

        // No inheritance relationship
        let resolver = super::super::JavaInheritanceResolver::new();

        // Test: Cross-package access should FAIL when no inheritance exists
        // We have full context - both classes known, so we can check inheritance
        // Since Unrelated does NOT extend Parent, access should be denied
        assert!(
            !behavior.is_symbol_visible_from_context(
                &symbol,
                FileId(2),
                Some("com.other.Unrelated"),
                &resolver
            ),
            "Protected symbol should NOT be visible to non-subclass in different package"
        );
    }

    #[test]
    fn test_get_containing_class() {
        let behavior = JavaBehavior::new();

        // Method in com.example.MyClass
        let mut symbol = Symbol::new(
            SymbolId(1),
            "myMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 10,
                start_column: 5,
                end_line: 12,
                end_column: 6,
            },
        );
        symbol.module_path = Some("com.example".to_string().into());
        symbol.scope_context = Some(ScopeContext::ClassMember {
            class_name: Some("MyClass".to_string().into()),
        });

        // Test: Should return fully qualified class name
        assert_eq!(
            behavior.get_containing_class(&symbol),
            Some("com.example.MyClass".to_string()),
            "Should combine package and class name"
        );

        // Method in default package
        let mut symbol2 = Symbol::new(
            SymbolId(2),
            "method2",
            SymbolKind::Method,
            FileId(2),
            Range {
                start_line: 5,
                start_column: 5,
                end_line: 7,
                end_column: 6,
            },
        );
        symbol2.module_path = Some("".to_string().into()); // Empty package
        symbol2.scope_context = Some(ScopeContext::ClassMember {
            class_name: Some("SimpleClass".to_string().into()),
        });

        // Test: Default package should return simple name
        assert_eq!(
            behavior.get_containing_class(&symbol2),
            Some("SimpleClass".to_string()),
            "Should return simple name for default package"
        );
    }

    #[test]
    fn test_module_path_from_file_uses_provider() {
        use crate::project_resolver::provider::ProjectResolutionProvider;
        use std::fs;
        use tempfile::TempDir;

        // TDD: Given a Java file and cached project configuration
        let temp_dir = TempDir::new().unwrap();
        let pom_path = temp_dir.path().join("pom.xml");

        let pom_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
</project>"#;

        fs::write(&pom_path, pom_content).unwrap();

        // Create settings and build provider cache
        let mut settings = crate::config::Settings::default();
        let java_settings = settings
            .languages
            .get_mut("java")
            .expect("java language config should exist");
        java_settings.enabled = true;
        java_settings.config_files = vec![pom_path.clone()];

        // Save original directory to restore later
        let original_dir = std::env::current_dir().unwrap();

        // Build cache (simulate indexer startup)
        std::env::set_current_dir(&temp_dir).unwrap();
        fs::create_dir_all(temp_dir.path().join(crate::init::local_dir_name())).unwrap();

        let provider = crate::project_resolver::providers::JavaProvider::new();
        provider.rebuild_cache(&settings).unwrap();

        // Create Java file
        let java_file = temp_dir.path().join("src/main/java/org/example/App.java");
        fs::create_dir_all(java_file.parent().unwrap()).unwrap();
        fs::write(&java_file, "package org.example; public class App {}").unwrap();

        // When calling module_path_from_file() (must run while cwd is still temp_dir)
        let behavior = JavaBehavior::new();
        let extensions = &["java"];
        let module_path = behavior.module_path_from_file(&java_file, temp_dir.path(), extensions);

        // Then it should return the package from JavaProvider
        assert_eq!(
            module_path,
            Some("org.example".to_string()),
            "Should use JavaProvider to determine package path"
        );

        // Restore original directory
        std::env::set_current_dir(&original_dir).unwrap();
    }

    #[test]
    fn test_configure_symbol_sets_visibility() {
        let behavior = JavaBehavior::new();

        // Test public visibility
        let mut symbol = Symbol::new(
            SymbolId(1),
            "publicMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 10,
                start_column: 5,
                end_line: 12,
                end_column: 6,
            },
        );
        symbol.signature = Some("public void publicMethod()".to_string().into());
        symbol.visibility = Visibility::Public; // Set by parser's determine_visibility()
        behavior.configure_symbol(&mut symbol, Some("com.example"));

        assert_eq!(symbol.visibility, Visibility::Public);
        assert_eq!(symbol.module_path, Some("com.example".to_string().into()));

        // Test protected visibility
        let mut symbol2 = Symbol::new(
            SymbolId(2),
            "protectedMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 15,
                start_column: 5,
                end_line: 17,
                end_column: 6,
            },
        );
        symbol2.signature = Some("protected void protectedMethod()".to_string().into());
        symbol2.visibility = Visibility::Module; // Set by parser's determine_visibility()
        behavior.configure_symbol(&mut symbol2, Some("com.example"));

        assert_eq!(symbol2.visibility, Visibility::Module);

        // Test private visibility
        let mut symbol3 = Symbol::new(
            SymbolId(3),
            "privateMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 20,
                start_column: 5,
                end_line: 22,
                end_column: 6,
            },
        );
        symbol3.signature = Some("private void privateMethod()".to_string().into());
        symbol3.visibility = Visibility::Private; // Set by parser's determine_visibility()
        behavior.configure_symbol(&mut symbol3, Some("com.example"));

        assert_eq!(symbol3.visibility, Visibility::Private);

        // Test package-private (no modifier)
        let mut symbol4 = Symbol::new(
            SymbolId(4),
            "packageMethod",
            SymbolKind::Method,
            FileId(1),
            Range {
                start_line: 25,
                start_column: 5,
                end_line: 27,
                end_column: 6,
            },
        );
        symbol4.signature = Some("void packageMethod()".to_string().into());
        symbol4.visibility = Visibility::Crate; // Set by parser's determine_visibility()
        behavior.configure_symbol(&mut symbol4, Some("com.example"));

        assert_eq!(symbol4.visibility, Visibility::Crate); // Package-private
    }

    #[test]
    fn test_import_matches_symbol_trait_impl() {
        let behavior = JavaBehavior::new();

        // Test case from Spring PetClinic:
        // Import: org.springframework.samples.petclinic.model.Person
        // Symbol module_path: org.springframework.samples.petclinic.model
        assert!(
            behavior.import_matches_symbol(
                "org.springframework.samples.petclinic.model.Person",
                "org.springframework.samples.petclinic.model",
                Some("org.springframework.samples.petclinic.owner")
            ),
            "Single-type import should match symbol in imported package"
        );

        // Wildcard import: org.springframework.samples.petclinic.model.*
        assert!(
            behavior.import_matches_symbol(
                "org.springframework.samples.petclinic.model.*",
                "org.springframework.samples.petclinic.model",
                Some("org.springframework.samples.petclinic.owner")
            ),
            "Wildcard import should match symbols in that package"
        );

        // Same package - no import needed
        assert!(
            behavior.import_matches_symbol(
                "org.springframework.samples.petclinic.owner.Owner",
                "org.springframework.samples.petclinic.owner",
                Some("org.springframework.samples.petclinic.owner")
            ),
            "Symbols in same package should match"
        );

        // Should not match different package
        assert!(
            !behavior.import_matches_symbol("com.example.Foo", "com.other.Bar", None),
            "Different packages should not match"
        );
    }

    #[test]
    #[ignore = "Run with: cargo test test_module_path_from_file_with_real_cache -- --ignored"]
    fn test_module_path_from_file_with_real_cache() {
        use std::path::PathBuf;

        // This test uses the actual cache file generated by the indexer
        // Cache should exist at: .codanna-test/index/resolvers/java_resolution.json

        let behavior = JavaBehavior::new();

        // Test with Spring PetClinic Owner.java
        let owner_path = PathBuf::from(
            "test_monorepos/spring-petclinic/src/main/java/org/springframework/samples/petclinic/owner/Owner.java",
        );

        let extensions = &["java"];
        let module_path = behavior.module_path_from_file(&owner_path, Path::new("."), extensions);

        // Should return package path, not file path
        assert_eq!(
            module_path,
            Some("org.springframework.samples.petclinic.owner".to_string()),
            "Should extract package path from file path using cache"
        );
    }
}