Skip to main content

boundary_java/
lib.rs

1use std::path::Path;
2
3use anyhow::{Context, Result};
4use tree_sitter::{Language, Parser, Query, QueryCursor, StreamingIterator};
5
6use boundary_core::analyzer::{LanguageAnalyzer, ParsedFile};
7use boundary_core::types::*;
8
9/// Java language analyzer using tree-sitter.
10pub struct JavaAnalyzer {
11    language: Language,
12    interface_query: Query,
13    class_query: Query,
14    import_query: Query,
15    annotation_query: Query,
16}
17
18impl JavaAnalyzer {
19    pub fn new() -> Result<Self> {
20        let language: Language = tree_sitter_java::LANGUAGE.into();
21
22        let interface_query = Query::new(
23            &language,
24            r#"
25            (interface_declaration
26              name: (identifier) @name
27              body: (interface_body
28                (method_declaration
29                  name: (identifier) @method)*))
30            "#,
31        )
32        .context("failed to compile interface query")?;
33
34        let class_query = Query::new(
35            &language,
36            r#"
37            (class_declaration
38              name: (identifier) @name
39              interfaces: (super_interfaces
40                (type_list
41                  (type_identifier) @implements))?
42              body: (class_body))
43            "#,
44        )
45        .context("failed to compile class query")?;
46
47        let import_query = Query::new(
48            &language,
49            r#"
50            (import_declaration
51              (scoped_identifier) @path)
52            "#,
53        )
54        .context("failed to compile import query")?;
55
56        // Annotation on class declarations for classification hints
57        let annotation_query = Query::new(
58            &language,
59            r#"
60            (class_declaration
61              (modifiers
62                (marker_annotation
63                  name: (identifier) @annotation))
64              name: (identifier) @class_name)
65            "#,
66        )
67        .context("failed to compile annotation query")?;
68
69        Ok(Self {
70            language,
71            interface_query,
72            class_query,
73            import_query,
74            annotation_query,
75        })
76    }
77}
78
79impl LanguageAnalyzer for JavaAnalyzer {
80    fn language(&self) -> &'static str {
81        "java"
82    }
83
84    fn file_extensions(&self) -> &[&str] {
85        &["java"]
86    }
87
88    fn parse_file(&self, path: &Path, content: &str) -> Result<ParsedFile> {
89        let mut parser = Parser::new();
90        parser
91            .set_language(&self.language)
92            .context("failed to set Java language")?;
93        let tree = parser
94            .parse(content, None)
95            .context("failed to parse Java file")?;
96        Ok(ParsedFile {
97            path: path.to_path_buf(),
98            tree,
99            content: content.to_string(),
100        })
101    }
102
103    fn extract_components(&self, parsed: &ParsedFile) -> Vec<Component> {
104        let mut components = Vec::new();
105        let package_path = derive_package_path(&parsed.path);
106
107        // Extract interfaces (ports)
108        extract_interfaces(
109            &self.interface_query,
110            parsed,
111            &package_path,
112            &mut components,
113        );
114
115        // Extract classes
116        extract_classes(&self.class_query, parsed, &package_path, &mut components);
117
118        // Enrich with annotation info
119        enrich_with_annotations(
120            &self.annotation_query,
121            parsed,
122            &package_path,
123            &mut components,
124        );
125
126        components
127    }
128
129    fn extract_dependencies(&self, parsed: &ParsedFile) -> Vec<Dependency> {
130        let mut deps = Vec::new();
131        let package_path = derive_package_path(&parsed.path);
132        let from_id = ComponentId::new(&package_path, "<file>");
133
134        let mut cursor = QueryCursor::new();
135        let path_idx = self
136            .import_query
137            .capture_names()
138            .iter()
139            .position(|n| *n == "path")
140            .unwrap_or(0);
141
142        let mut matches = cursor.matches(
143            &self.import_query,
144            parsed.tree.root_node(),
145            parsed.content.as_bytes(),
146        );
147
148        while let Some(m) = matches.next() {
149            for capture in m.captures {
150                if capture.index as usize == path_idx {
151                    let node = capture.node;
152                    let import_path = node_text(node, &parsed.content);
153
154                    // Skip java.lang.* and standard library
155                    if import_path.starts_with("java.") || import_path.starts_with("javax.") {
156                        continue;
157                    }
158
159                    let to_id = ComponentId::new(&import_path, "<class>");
160
161                    deps.push(Dependency {
162                        from: from_id.clone(),
163                        to: to_id,
164                        kind: DependencyKind::Import,
165                        location: SourceLocation {
166                            file: parsed.path.clone(),
167                            line: node.start_position().row + 1,
168                            column: node.start_position().column + 1,
169                        },
170                        import_path: Some(import_path),
171                    });
172                }
173            }
174        }
175
176        deps
177    }
178}
179
180fn extract_interfaces(
181    query: &Query,
182    parsed: &ParsedFile,
183    package_path: &str,
184    components: &mut Vec<Component>,
185) {
186    let mut cursor = QueryCursor::new();
187    let name_idx = query
188        .capture_names()
189        .iter()
190        .position(|n| *n == "name")
191        .unwrap_or(0);
192    let method_idx = query.capture_names().iter().position(|n| *n == "method");
193
194    let mut matches = cursor.matches(query, parsed.tree.root_node(), parsed.content.as_bytes());
195
196    while let Some(m) = matches.next() {
197        let mut name = String::new();
198        let mut methods = Vec::new();
199        let mut start_row = 0;
200        let mut start_col = 0;
201
202        for capture in m.captures {
203            if capture.index as usize == name_idx {
204                name = node_text(capture.node, &parsed.content);
205                start_row = capture.node.start_position().row;
206                start_col = capture.node.start_position().column;
207            } else if Some(capture.index as usize) == method_idx {
208                methods.push(MethodInfo {
209                    name: node_text(capture.node, &parsed.content),
210                    parameters: String::new(),
211                    return_type: String::new(),
212                });
213            }
214        }
215
216        if name.is_empty() {
217            continue;
218        }
219
220        components.push(Component {
221            id: ComponentId::new(package_path, &name),
222            name: name.clone(),
223            kind: ComponentKind::Port(PortInfo { name, methods }),
224            layer: None,
225            location: SourceLocation {
226                file: parsed.path.clone(),
227                line: start_row + 1,
228                column: start_col + 1,
229            },
230            is_cross_cutting: false,
231            architecture_mode: ArchitectureMode::default(),
232        });
233    }
234}
235
236fn extract_classes(
237    query: &Query,
238    parsed: &ParsedFile,
239    package_path: &str,
240    components: &mut Vec<Component>,
241) {
242    let mut cursor = QueryCursor::new();
243    let name_idx = query
244        .capture_names()
245        .iter()
246        .position(|n| *n == "name")
247        .unwrap_or(0);
248    let implements_idx = query
249        .capture_names()
250        .iter()
251        .position(|n| *n == "implements");
252
253    let mut matches = cursor.matches(query, parsed.tree.root_node(), parsed.content.as_bytes());
254
255    while let Some(m) = matches.next() {
256        let mut name = String::new();
257        let mut implements = Vec::new();
258        let mut start_row = 0;
259        let mut start_col = 0;
260
261        for capture in m.captures {
262            if capture.index as usize == name_idx {
263                name = node_text(capture.node, &parsed.content);
264                start_row = capture.node.start_position().row;
265                start_col = capture.node.start_position().column;
266            } else if Some(capture.index as usize) == implements_idx {
267                implements.push(node_text(capture.node, &parsed.content));
268            }
269        }
270
271        if name.is_empty() {
272            continue;
273        }
274
275        let kind = classify_class_kind(&name, &implements);
276
277        components.push(Component {
278            id: ComponentId::new(package_path, &name),
279            name: name.clone(),
280            kind,
281            layer: None,
282            location: SourceLocation {
283                file: parsed.path.clone(),
284                line: start_row + 1,
285                column: start_col + 1,
286            },
287            is_cross_cutting: false,
288            architecture_mode: ArchitectureMode::default(),
289        });
290    }
291}
292
293/// Enrich class components with annotation-based classification.
294fn enrich_with_annotations(
295    query: &Query,
296    parsed: &ParsedFile,
297    package_path: &str,
298    components: &mut [Component],
299) {
300    let mut cursor = QueryCursor::new();
301    let annotation_idx = query
302        .capture_names()
303        .iter()
304        .position(|n| *n == "annotation");
305    let class_name_idx = query
306        .capture_names()
307        .iter()
308        .position(|n| *n == "class_name");
309
310    let mut matches = cursor.matches(query, parsed.tree.root_node(), parsed.content.as_bytes());
311
312    while let Some(m) = matches.next() {
313        let mut annotation = String::new();
314        let mut class_name = String::new();
315
316        for capture in m.captures {
317            if Some(capture.index as usize) == annotation_idx {
318                annotation = node_text(capture.node, &parsed.content);
319            }
320            if Some(capture.index as usize) == class_name_idx {
321                class_name = node_text(capture.node, &parsed.content);
322            }
323        }
324
325        if class_name.is_empty() || annotation.is_empty() {
326            continue;
327        }
328
329        let id = ComponentId::new(package_path, &class_name);
330        if let Some(comp) = components.iter_mut().find(|c| c.id == id) {
331            match annotation.as_str() {
332                "Repository" => {
333                    comp.kind = ComponentKind::Repository;
334                }
335                "Service" => {
336                    comp.kind = ComponentKind::Service;
337                }
338                "Controller" | "RestController" => {
339                    comp.kind = ComponentKind::Adapter(AdapterInfo {
340                        name: class_name,
341                        implements: vec![],
342                        confidence: AdapterConfidence::default(),
343                    });
344                }
345                _ => {}
346            }
347        }
348    }
349}
350
351/// Classify a class by its name suffix heuristic and implements clause.
352fn classify_class_kind(name: &str, implements: &[String]) -> ComponentKind {
353    let lower = name.to_lowercase();
354    if lower.ends_with("repository") || lower.ends_with("repo") {
355        ComponentKind::Repository
356    } else if lower.ends_with("service") || lower.ends_with("svc") {
357        ComponentKind::Service
358    } else if lower.ends_with("handler") || lower.ends_with("controller") {
359        ComponentKind::Adapter(AdapterInfo {
360            name: name.to_string(),
361            implements: implements.to_vec(),
362            confidence: AdapterConfidence::default(),
363        })
364    } else if lower.ends_with("usecase") || lower.ends_with("interactor") {
365        ComponentKind::UseCase
366    } else if !implements.is_empty() {
367        ComponentKind::Adapter(AdapterInfo {
368            name: name.to_string(),
369            implements: implements.to_vec(),
370            confidence: AdapterConfidence::default(),
371        })
372    } else {
373        ComponentKind::Entity(EntityInfo {
374            name: name.to_string(),
375            fields: vec![],
376            methods: Vec::new(),
377            is_active_record: false,
378            is_anemic_domain_model: false,
379        })
380    }
381}
382
383/// Extract text from a tree-sitter node.
384fn node_text(node: tree_sitter::Node, source: &str) -> String {
385    source[node.byte_range()].to_string()
386}
387
388/// Derive a package path from a file path.
389fn derive_package_path(path: &Path) -> String {
390    path.parent()
391        .map(|p| p.to_string_lossy().replace('\\', "/"))
392        .unwrap_or_default()
393}
394
395#[cfg(test)]
396mod tests {
397    use super::*;
398    use std::path::PathBuf;
399
400    #[test]
401    fn test_parse_java_interface() {
402        let analyzer = JavaAnalyzer::new().unwrap();
403        let content = r#"
404package com.example.domain.user;
405
406public interface UserRepository {
407    void save(User user);
408    User findById(String id);
409}
410"#;
411        let path = PathBuf::from("src/main/java/com/example/domain/user/UserRepository.java");
412        let parsed = analyzer.parse_file(&path, content).unwrap();
413        let components = analyzer.extract_components(&parsed);
414
415        let repo = components.iter().find(|c| c.name == "UserRepository");
416        assert!(repo.is_some(), "should find UserRepository interface");
417        assert!(matches!(repo.unwrap().kind, ComponentKind::Port(_)));
418
419        if let ComponentKind::Port(ref info) = repo.unwrap().kind {
420            assert!(info.methods.iter().any(|m| m.name == "save"));
421            assert!(info.methods.iter().any(|m| m.name == "findById"));
422        }
423    }
424
425    #[test]
426    fn test_parse_java_class_with_implements() {
427        let analyzer = JavaAnalyzer::new().unwrap();
428        let content = r#"
429package com.example.infrastructure.postgres;
430
431public class PostgresUserRepository implements UserRepository {
432    private final DataSource dataSource;
433
434    public PostgresUserRepository(DataSource dataSource) {
435        this.dataSource = dataSource;
436    }
437
438    public void save(User user) {
439        // save implementation
440    }
441
442    public User findById(String id) {
443        return null;
444    }
445}
446"#;
447        let path = PathBuf::from(
448            "src/main/java/com/example/infrastructure/postgres/PostgresUserRepository.java",
449        );
450        let parsed = analyzer.parse_file(&path, content).unwrap();
451        let components = analyzer.extract_components(&parsed);
452
453        let repo = components
454            .iter()
455            .find(|c| c.name == "PostgresUserRepository");
456        assert!(repo.is_some(), "should find PostgresUserRepository");
457        // Name-based classification should match "Repository"
458        assert!(matches!(repo.unwrap().kind, ComponentKind::Repository));
459    }
460
461    #[test]
462    fn test_extract_imports() {
463        let analyzer = JavaAnalyzer::new().unwrap();
464        let content = r#"
465package com.example.application;
466
467import java.util.List;
468import com.example.domain.user.User;
469import com.example.domain.user.UserRepository;
470"#;
471        let path = PathBuf::from("src/main/java/com/example/application/UserService.java");
472        let parsed = analyzer.parse_file(&path, content).unwrap();
473        let deps = analyzer.extract_dependencies(&parsed);
474
475        // Should skip java.* imports
476        let paths: Vec<&str> = deps
477            .iter()
478            .filter_map(|d| d.import_path.as_deref())
479            .collect();
480        assert!(!paths.iter().any(|p| p.starts_with("java.")));
481        assert!(paths.iter().any(|p| p.contains("domain.user.User")));
482        assert!(paths
483            .iter()
484            .any(|p| p.contains("domain.user.UserRepository")));
485    }
486
487    #[test]
488    fn test_annotation_classification() {
489        let analyzer = JavaAnalyzer::new().unwrap();
490        let content = r#"
491package com.example.application;
492
493@Service
494public class UserService {
495    private final UserRepository repo;
496
497    public UserService(UserRepository repo) {
498        this.repo = repo;
499    }
500}
501"#;
502        let path = PathBuf::from("src/main/java/com/example/application/UserService.java");
503        let parsed = analyzer.parse_file(&path, content).unwrap();
504        let components = analyzer.extract_components(&parsed);
505
506        let svc = components.iter().find(|c| c.name == "UserService");
507        assert!(svc.is_some(), "should find UserService");
508        assert!(
509            matches!(svc.unwrap().kind, ComponentKind::Service),
510            "should be classified as Service by annotation"
511        );
512    }
513
514    #[test]
515    fn test_controller_annotation() {
516        let analyzer = JavaAnalyzer::new().unwrap();
517        let content = r#"
518package com.example.presentation;
519
520@Controller
521public class UserController {
522    public void getUser() {}
523}
524"#;
525        let path = PathBuf::from("src/main/java/com/example/presentation/UserController.java");
526        let parsed = analyzer.parse_file(&path, content).unwrap();
527        let components = analyzer.extract_components(&parsed);
528
529        let ctrl = components.iter().find(|c| c.name == "UserController");
530        assert!(ctrl.is_some(), "should find UserController");
531        assert!(
532            matches!(ctrl.unwrap().kind, ComponentKind::Adapter(_)),
533            "should be classified as Adapter by @Controller annotation"
534        );
535    }
536
537    #[test]
538    fn test_entity_class() {
539        let analyzer = JavaAnalyzer::new().unwrap();
540        let content = r#"
541package com.example.domain.user;
542
543public class User {
544    private String id;
545    private String name;
546    private String email;
547}
548"#;
549        let path = PathBuf::from("src/main/java/com/example/domain/user/User.java");
550        let parsed = analyzer.parse_file(&path, content).unwrap();
551        let components = analyzer.extract_components(&parsed);
552
553        let user = components.iter().find(|c| c.name == "User");
554        assert!(user.is_some(), "should find User");
555        assert!(matches!(user.unwrap().kind, ComponentKind::Entity(_)));
556    }
557}