StaticGuard is an advanced code analysis engine that bridges the gap between traditional lexical auditing and predictive AI. By combining deep-code structural analysis with trained ML models, it predicts bug probabilities, audits algorithmic complexity, and enforces architectural standards โ all before a single line of code is pushed to production.
๐ก Built as a personal project to explore how static analysis and machine learning can work together to improve real-world code quality.
=======
eca22fb321506319eb5be87a0cf3fb7ce4aa3bdf
- Predictive Risk Scoring: Get a real-time "Bug Probability" powered by a trained RandomForest model and heuristic adjustments.
- Worst-Case Complexity: Automatically identifies O(nยฒ), O(2โฟ), and exponential recursion bottlenecks.
- Architectural Guards: Real-time detection of OOP violations (LSP/ISP) and logical stability risks.
- Premium Visual Reporting: Generates a sleek, dark-modae HTML dashboard for stakeholder reviews.
Time/Space complexity, bug probability, quality score โ all at a glance.
Structured analysis report categorizing issues into Critical Fixes and Stability Risks.
OOP violations, interface mapping, memory allocation tracking, and knowledge base.
STATICGUARD ANALYSIS REPORT
--------------------------------
CRITICAL FIXES (Program won't run correctly):
Line 8 โ Missing semicolon
Line 29 โ Missing semicolon
Line 106 โ Math risk: Possible division by zero.
STABILITY RISKS (Program might freeze or crash):
Line 42 โ Infinite loop detected.
Line 192 โ Infinite loop detected.
Unreachable code detected at line 199.
Time Complexity Estimate (Worst-case):
Code Pattern โ O(n^5)
Space Complexity Estimate (Worst-case):
Memory Pattern โ O(nยฒ)
OOP Issues:
DerivedSystem.execute() incorrectly overrides parent method
Bug Risk Probability: High
Quality Score: 0/100
StaticGuard extracts structural features from source code to feed into the ML model:
- Lines of Code (LOC)
- Number of loops and nesting depth
- Variable and method count
- Conditional statements
- Recursive calls
- Exception handling patterns
Complexity is estimated using pattern-based structural analysis:
- Nested loops โ O(nยฒ), O(nยณ)
- Loop patterns โ O(n), O(log n), O(n log n)
- Recursion detection โ exponential complexity
Space complexity is inferred from array/matrix allocations, object creation patterns, and memory-heavy structures.
The system uses a modular architecture with specialized detectors:
- Syntax Detector โ identifies missing tokens and invalid constructs
- Logical Detector โ detects infinite loops and unreachable code
- OOP Detector โ checks method overriding and interface violations
- Performance Detector โ identifies inefficient patterns
Each module contributes to a unified analysis report.
- Model: RandomForest Classifier
- Input: Extracted code features (13+ metrics)
- Output: Bug probability (0โ100%)
The model identifies patterns associated with unstable or error-prone code.
Code quality score is calculated using weighted penalties:
- ๐ด Critical issues (syntax errors) โ High penalty (-10 per issue)
- ๐ Logical risks (infinite loops) โ Medium penalty (-8 per issue)
- ๐ก OOP violations โ Moderate penalty (-5 per issue)
- ๐ข Code smells โ Low penalty (-2 per issue)
Compilers typically stop at the first syntax error. StaticGuard improves on this by:
- Continuing analysis even after detecting errors
- Reporting all issues in a single run
- Providing a complete overview of code quality, not just syntax
This allows developers to fix all problems at once instead of debugging step-by-step.
Modern software development often relies on reactive debugging and manual peer reviews, which are error-prone and fail to scale. Critical architectural flaws like deeply nested loops, silent exception swallowing, and brittle OOP structures often go unnoticed until they cause production outages.
StaticGuard provides a proactive, automated layer of intelligence to catch these risks at the source.
- Automated code review before deployment
- Detecting performance bottlenecks in large codebases
- Educational tool for learning clean coding practices
- Pre-commit code quality validation
| Category | What It Does |
|---|---|
| ๐ง ML Bug Prediction | Uses a trained RandomForest model and heuristic adjustments to quantify risk from 13+ code metrics |
| ๐ Complexity Auditing | Detects time (O(nยฒ), O(n log n)) and space complexity via structural analysis |
| Catches syntax errors, infinite loops, unreachable code, division by zero | |
| ๐๏ธ OOP Enforcement | Flags incorrect method overrides, partial interface implementations |
| ๐ก๏ธ Clean Code | Identifies magic numbers, unused variables, empty catch blocks |
| ๐ HTML Dashboard | Generates a professional dark-mode report with Chart.js visualizations |
โโโโโโโโโโโโโโโโโโโ
โ Java Source โ
โ Code Input โ
โโโโโโโโโโฌโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Lexical Analyzerโ โโโ Tokenization
โ & Parser โ
โโโโโโโโโโฌโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Feature โ โโโ LOC, Nesting, Loops, Variables
โ Extractor โ
โโโโโโโโโโฌโโโโโโโโโ
โผ
โโโโโโโดโโโโโโ
โผ โผ
โโโโโโโโโโ โโโโโโโโโโ
โ Static โ โ ML โ โโโ RandomForest Bug Prediction
โAnalysisโ โ Bridge โ
โโโโโฌโโโโโ โโโโโฌโโโโโ
โโโโโโโฌโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Report Generator โ โโโ HTML Dashboard + Console
โโโโโโโโโโโโโโโโโโโ
- Core Logic: Java 17 (Parallel Analysis Orchestration)
- Intelligence: Python (Scikit-Learn, RandomForest, Pandas)
- Reporting: HTML5, Chart.js, Vanilla CSS
- Data Layer: Google Gson
StaticGuard/
โโโ src/main/java/com/staticguard/
โ โโโ analysis/ # Detectors, complexity analyzers, suggestion engine
โ โโโ core/ # Lexer, tokenizer, interfaces, exceptions
โ โโโ ml/ # Feature extraction & Python ML bridge
โ โโโ orchestrator/ # Parallel analysis task management
โ โโโ output/ # Console & HTML report generators
โโโ ml_module/ # Python ML model (predict.py, model.pkl)
โโโ lib/ # Required JAR dependencies
โโโ sample/ # Diverse test cases for validation
โโโ results/ # Output directory for HTML Dashboards
- JDK 17+ (Ensure
javaandjavacare in your PATH) - Python 3.10+ (Required for ML inference)
pip install pandas scikit-learn# Generate a quoted file list (handles spaces in paths)
$files = Get-ChildItem -Path "src/main/java" -Recurse -Filter "*.java" | ForEach-Object { '"{0}"' -f ($_.FullName -replace '\\', '/') }
# Compile
javac -cp "lib/*" -d target/classes $filesVia Python Wrapper:
python run_analysis.py sample/ChaosTestFile.javaVia Direct Java:
java -cp "target/classes;lib/*" com.staticguard.Main sample/ChaosTestFile.javastart results/report.html- IDE Plugins: Real-time feedback within VS Code and IntelliJ.
- CI/CD Integration: Automatically block PRs that exceed a defined risk threshold.
- Multi-Language Support: Expanding analysis to C++ and Python.
- RCA with SHAP: Explainable AI to show exactly why a file was flagged as risky.
- Complexity estimation is approximate (pattern-based, not formal proof)
- ML accuracy depends on training data quality
- Not a full compiler replacement โ focuses on quality insights
StaticGuard is designed to improve code quality by combining static analysis with intelligent predictions. It goes beyond traditional compilers by providing a holistic view of code health.
๐ก Built to make code safer, cleaner, and more efficient.



