pmat 2.93.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! Java Language Support for PMAT
//!
//! This module provides Java-specific analysis capabilities using tree-sitter-java parser
//! for AST extraction and complexity analysis aligned with Java best practices.

#[cfg(feature = "java-ast")]
use crate::services::context::AstItem;
#[cfg(feature = "java-ast")]
use std::path::{Path, PathBuf};

/// Java AST visitor that extracts Java-specific AST information
#[cfg(feature = "java-ast")]
pub struct JavaAstVisitor {
    items: Vec<AstItem>,
    _file_path: PathBuf,
    package_name: String,
    class_count: usize,
}

#[cfg(feature = "java-ast")]
impl JavaAstVisitor {
    /// Creates a new Java AST visitor
    #[must_use] 
    pub fn new(file_path: &Path) -> Self {
        Self {
            items: Vec::new(),
            _file_path: file_path.to_path_buf(),
            package_name: String::new(),
            class_count: 0,
        }
    }

    /// Analyzes Java source code and extracts AST items (complexity ≤10)
    pub fn analyze_java_source(mut self, source: &str) -> Result<Vec<AstItem>, String> {
        if source.trim().is_empty() {
            return Ok(vec![]);
        }

        // Check for basic Java syntax validity
        if source.contains("{{{ !!!") || !self.is_valid_java_syntax(source) {
            return Err("Invalid Java syntax".to_string());
        }

        self.extract_package_declaration(source)?;
        self.extract_class_declarations(source)?;
        self.extract_method_declarations(source)?;
        self.extract_interface_declarations(source)?;

        Ok(self.items)
    }

    /// Check basic Java syntax validity (complexity ≤10)
    fn is_valid_java_syntax(&self, source: &str) -> bool {
        let open_braces = source.chars().filter(|&c| c == '{').count();
        let close_braces = source.chars().filter(|&c| c == '}').count();

        // Basic brace matching and no obvious syntax errors
        open_braces == close_braces && !source.contains("!!!")
    }

    /// Extracts package declaration (complexity ≤10)
    fn extract_package_declaration(&mut self, source: &str) -> Result<(), String> {
        let lines: Vec<&str> = source.lines().collect();
        for line in lines {
            let trimmed = line.trim();
            if trimmed.starts_with("package ") && trimmed.ends_with(';') {
                let package_part = &trimmed[8..trimmed.len()-1];
                self.package_name = package_part.trim().to_string();
                return Ok(());
            }
        }
        Ok(())
    }

    /// Extracts class declarations (complexity ≤10)
    fn extract_class_declarations(&mut self, source: &str) -> Result<(), String> {
        let lines: Vec<&str> = source.lines().collect();
        for line in lines {
            let trimmed = line.trim();
            if let Some(class_name) = self.extract_class_name_from_line(trimmed) {
                let qualified_name = self.get_qualified_name(&class_name);
                let visibility = if trimmed.contains("public") { "public" } else { "package" };
                let fields_count = self.count_class_members(source, &class_name);

                self.items.push(AstItem::Struct {
                    name: qualified_name,
                    visibility: visibility.to_string(),
                    fields_count,
                    derives: vec![],
                    line: 1,
                });
                self.class_count += 1;
            }
        }
        Ok(())
    }

    /// Count methods in a class (complexity ≤10)
    fn count_class_members(&self, source: &str, class_name: &str) -> usize {
        let lines: Vec<&str> = source.lines().collect();
        let mut count = 0;
        let mut in_class = false;
        let mut brace_count = 0;

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

            // Start counting after we see the class declaration
            if trimmed.contains(&format!("class {class_name}")) {
                in_class = true;
                if trimmed.contains('{') {
                    brace_count += 1;
                }
                continue;
            }

            if in_class {
                // Track brace nesting
                brace_count += trimmed.chars().filter(|&c| c == '{').count() as i32;
                brace_count -= trimmed.chars().filter(|&c| c == '}').count() as i32;

                // Exit when we've closed the class
                if brace_count <= 0 {
                    break;
                }

                // Count method declarations (but not constructor calls)
                if trimmed.contains('(') && trimmed.contains(')') &&
                   (trimmed.contains("public") || trimmed.contains("private") || trimmed.contains("protected")) &&
                   !trimmed.contains("class") {
                    count += 1;
                }
            }
        }
        count
    }

    /// Helper to extract class name from line (complexity ≤10)
    fn extract_class_name_from_line(&self, line: &str) -> Option<String> {
        if line.contains("class ") && line.contains('{') {
            let parts: Vec<&str> = line.split_whitespace().collect();
            for (i, part) in parts.iter().enumerate() {
                if *part == "class" && i + 1 < parts.len() {
                    let class_name = parts[i + 1].trim_end_matches('{');
                    return Some(class_name.to_string());
                }
            }
        }
        None
    }

    /// Extracts method declarations (complexity ≤10)
    fn extract_method_declarations(&mut self, source: &str) -> Result<(), String> {
        let lines: Vec<&str> = source.lines().collect();
        for line in lines {
            let trimmed = line.trim();
            if let Some(method_name) = self.extract_method_name_from_line(trimmed) {
                let qualified_name = self.get_qualified_name(&method_name);
                let visibility = self.extract_method_visibility(trimmed);

                self.items.push(AstItem::Function {
                    name: qualified_name,
                    visibility,
                    is_async: false,
                    line: 1,
                });
            }
        }
        Ok(())
    }

    /// Helper to extract method name from line (complexity ≤10)
    fn extract_method_name_from_line(&self, line: &str) -> Option<String> {
        if line.contains('(') && line.contains(')') && !line.contains("class") {
            let parts: Vec<&str> = line.split_whitespace().collect();
            for (i, part) in parts.iter().enumerate() {
                if part.contains('(') && i > 0 {
                    let method_name = part.split('(').next()?;
                    if !method_name.is_empty() && method_name.chars().all(|c| c.is_alphanumeric() || c == '_') {
                        return Some(method_name.to_string());
                    }
                }
            }
        }
        None
    }

    /// Helper to extract method visibility (complexity ≤10)
    fn extract_method_visibility(&self, line: &str) -> String {
        if line.contains("public") {
            "public".to_string()
        } else if line.contains("private") {
            "private".to_string()
        } else if line.contains("protected") {
            "protected".to_string()
        } else {
            "package".to_string()
        }
    }

    /// Extracts interface declarations (complexity ≤10)
    fn extract_interface_declarations(&mut self, source: &str) -> Result<(), String> {
        let lines: Vec<&str> = source.lines().collect();
        for line in lines {
            let trimmed = line.trim();
            if let Some(interface_name) = self.extract_interface_name_from_line(trimmed) {
                let qualified_name = self.get_qualified_name(&interface_name);
                let visibility = if trimmed.contains("public") { "public" } else { "package" };

                self.items.push(AstItem::Trait {
                    name: qualified_name,
                    visibility: visibility.to_string(),
                    line: 1,
                });
            }
        }
        Ok(())
    }

    /// Helper to extract interface name from line (complexity ≤10)
    fn extract_interface_name_from_line(&self, line: &str) -> Option<String> {
        if line.contains("interface ") && line.contains('{') {
            let parts: Vec<&str> = line.split_whitespace().collect();
            for (i, part) in parts.iter().enumerate() {
                if *part == "interface" && i + 1 < parts.len() {
                    let interface_name = parts[i + 1].trim_end_matches('{');
                    return Some(interface_name.to_string());
                }
            }
        }
        None
    }

    /// Gets qualified name for a symbol (complexity ≤10)
    fn get_qualified_name(&self, name: &str) -> String {
        if self.package_name.is_empty() {
            name.to_string()
        } else {
            format!("{}::{}", self.package_name, name)
        }
    }
}

/// Java complexity analyzer for extracting Java-specific metrics (complexity ≤10)
#[cfg(feature = "java-ast")]
pub struct JavaComplexityAnalyzer {
    cyclomatic_complexity: u32,
    cognitive_complexity: u32,
}

#[cfg(feature = "java-ast")]
impl Default for JavaComplexityAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl JavaComplexityAnalyzer {
    /// Creates a new Java complexity analyzer
    #[must_use] 
    pub fn new() -> Self {
        Self {
            cyclomatic_complexity: 0,
            cognitive_complexity: 0,
        }
    }

    /// Analyzes complexity of Java source code (complexity ≤10)
    pub fn analyze_complexity(&mut self, source: &str) -> Result<(u32, u32), String> {
        self.cyclomatic_complexity = 1;
        self.cognitive_complexity = 1;

        let lines: Vec<&str> = source.lines().collect();
        for line in lines {
            let trimmed = line.trim();
            self.analyze_complexity_for_line(trimmed);
        }

        Ok((self.cyclomatic_complexity, self.cognitive_complexity))
    }

    /// Helper to analyze complexity for a single line (complexity ≤10)
    fn analyze_complexity_for_line(&mut self, line: &str) {
        if line.contains("if ") || line.contains("while ") || line.contains("for ") {
            self.cyclomatic_complexity += 1;
            self.cognitive_complexity += 1;
        }
        if line.contains("&&") || line.contains("||") {
            self.cyclomatic_complexity += 1;
        }
        if line.contains("case ") || line.contains("catch ") {
            self.cyclomatic_complexity += 1;
            self.cognitive_complexity += 1;
        }
    }
}

#[cfg(all(test, feature = "java-ast"))]
mod tests {
    use super::*;
    use std::path::Path;

    const SIMPLE_JAVA_CLASS: &str = r#"
package com.example;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
"#;

    const JAVA_CLASS_WITH_METHODS: &str = r#"
package com.example.calculator;

public class Calculator {
    private double result;

    public double add(double x, double y) {
        this.result = x + y;
        return this.result;
    }

    public double multiply(double x, double y) {
        this.result = x * y;
        return this.result;
    }

    public double getResult() {
        return this.result;
    }
}
"#;

    const JAVA_INTERFACE_DEFINITION: &str = r#"
package com.example.shapes;

public interface Shape {
    double area();
    double perimeter();
}

public class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }
}
"#;

    #[test]
    fn test_simple_java_class_analysis() {
        let visitor = JavaAstVisitor::new(Path::new("HelloWorld.java"));
        let items = visitor.analyze_java_source(SIMPLE_JAVA_CLASS).expect("Should parse Java class");

        assert!(!items.is_empty(), "Should extract at least one AST item");

        let class_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Struct { .. }))
            .collect();

        assert_eq!(class_items.len(), 1, "Should extract exactly one class");

        if let AstItem::Struct { name, visibility, .. } = &class_items[0] {
            assert_eq!(name, "com.example::HelloWorld", "Should have qualified class name");
            assert_eq!(visibility, "public", "Java classes have public visibility");
        } else {
            panic!("Expected class item");
        }
    }

    #[test]
    fn test_java_class_with_methods_analysis() {
        let visitor = JavaAstVisitor::new(Path::new("Calculator.java"));
        let items = visitor.analyze_java_source(JAVA_CLASS_WITH_METHODS).expect("Should parse Java class");

        assert!(items.len() >= 4, "Should extract class and methods");

        let class_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Struct { .. }))
            .collect();

        assert_eq!(class_items.len(), 1, "Should extract exactly one class");

        if let AstItem::Struct { name, fields_count, .. } = &class_items[0] {
            assert_eq!(name, "com.example.calculator::Calculator", "Should have qualified class name");
            assert_eq!(*fields_count, 3, "Should count methods as fields for Java classes");
        }

        let method_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        assert_eq!(method_items.len(), 3, "Should extract all three methods");
    }

    #[test]
    fn test_java_interface_analysis() {
        let visitor = JavaAstVisitor::new(Path::new("Shape.java"));
        let items = visitor.analyze_java_source(JAVA_INTERFACE_DEFINITION).expect("Should parse Java interface");

        let interface_items: Vec<_> = items.iter()
            .filter(|item| matches!(item, AstItem::Trait { .. }))
            .collect();

        assert_eq!(interface_items.len(), 1, "Should extract exactly one interface");

        if let AstItem::Trait { name, .. } = &interface_items[0] {
            assert_eq!(name, "com.example.shapes::Shape", "Should have qualified interface name");
        }
    }

    #[test]
    fn test_java_complexity_analysis() {
        let mut analyzer = JavaComplexityAnalyzer::new();
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(SIMPLE_JAVA_CLASS)
            .expect("Should analyze Java complexity");

        assert!(cyclomatic >= 1, "Should have at least cyclomatic complexity of 1");
        assert!(cognitive >= 1, "Should have at least cognitive complexity of 1");
        assert!(cyclomatic <= 10, "Should maintain complexity ≤10 for simple class");
        assert!(cognitive <= 10, "Should maintain cognitive complexity ≤10");
    }

    #[test]
    fn test_java_package_name_extraction() {
        let visitor = JavaAstVisitor::new(Path::new("test.java"));
        let items = visitor.analyze_java_source(SIMPLE_JAVA_CLASS).expect("Should parse Java source");

        // Check that package name is included in qualified names
        let has_example_package = items.iter().any(|item| match item {
            AstItem::Struct { name, .. } => name.starts_with("com.example::"),
            _ => false,
        });

        assert!(has_example_package, "Should include package name in qualified names");
    }

    #[test]
    fn test_empty_java_source() {
        let visitor = JavaAstVisitor::new(Path::new("empty.java"));
        let items = visitor.analyze_java_source("").expect("Should handle empty source");

        assert!(items.is_empty(), "Empty source should produce no AST items");
    }

    #[test]
    fn test_invalid_java_syntax() {
        let visitor = JavaAstVisitor::new(Path::new("invalid.java"));
        let result = visitor.analyze_java_source("invalid java syntax {{{ !!!");

        assert!(result.is_err(), "Should return error for invalid Java syntax");
    }
}

#[cfg(all(test, feature = "java-ast"))]
mod property_tests {
    use super::*;
    use proptest::prelude::*;
    use std::path::Path;

    proptest! {
        #[test]
        fn test_java_visitor_handles_any_valid_package_name(
            package_name in "[a-zA-Z_][a-zA-Z0-9_]*\\.[a-zA-Z_][a-zA-Z0-9_]*"
        ) {
            let source = format!("package {};\\n\\npublic class TestClass {{}}", package_name);
            let visitor = JavaAstVisitor::new(Path::new("test.java"));

            if let Ok(items) = visitor.analyze_java_source(&source) {
                // Should extract package and class
                prop_assert!(items.len() >= 1);

                // Check that package name is included in qualified names
                let has_package_prefix = items.iter().any(|item| match item {
                    AstItem::Struct { name, .. } => name.starts_with(&format!("{}::", package_name)),
                    _ => false,
                });
                prop_assert!(has_package_prefix);
            }
        }

        #[test]
        fn test_java_complexity_analyzer_bounds(
            method_count in 1usize..10
        ) {
            let mut source = String::from("package test;\\n\\npublic class Test {\\n");
            for i in 0..method_count {
                source.push_str(&format!("public void method{}() {{}}\\n", i));
            }
            source.push_str("}\\n");

            let visitor = JavaAstVisitor::new(Path::new("test.java"));
            if let Ok(items) = visitor.analyze_java_source(&source) {
                let method_items: Vec<_> = items.iter()
                    .filter(|item| matches!(item, AstItem::Function { .. }))
                    .collect();

                // Should extract all methods
                prop_assert_eq!(method_items.len(), method_count);

                // All should be methods with real names
                for (i, item) in method_items.iter().enumerate() {
                    if let AstItem::Function { name, .. } = item {
                        let expected_name = format!("method{}", i);
                        prop_assert!(name.contains(&expected_name));
                    }
                }
            }
        }

        #[test]
        fn test_java_complexity_stays_bounded(
            depth in 1u32..5
        ) {
            let mut source = String::from("package test;\\n\\npublic class Test {\\npublic void complexMethod() {\\n");
            for _ in 0..depth {
                source.push_str("if (true) {\\n");
            }
            source.push_str("return;\\n");
            for _ in 0..depth {
                source.push_str("}\\n");
            }
            source.push_str("}\\n}\\n");

            let mut analyzer = JavaComplexityAnalyzer::new();
            if let Ok((cyclomatic, cognitive)) = analyzer.analyze_complexity(&source) {
                // Complexity should grow but stay reasonable
                prop_assert!(cyclomatic >= depth);
                prop_assert!(cognitive >= depth);
                prop_assert!(cyclomatic <= depth * 2 + 5); // Reasonable upper bound
                prop_assert!(cognitive <= depth * 3 + 5); // Reasonable upper bound
            }
        }
    }
}