Skip to content

Latest commit

ย 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›ก๏ธ StaticGuard

Java Python HTML CSS JavaScript RandomForest

Intelligent Static Analysis Meets Machine Learning.

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.


๐Ÿ”ฅ StaticGuard in Action

<<<<<<< HEAD Dashboard

=======

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.

๐Ÿ“ธ Output Showcase

Dashboard Metrics

Time/Space complexity, bug probability, quality score โ€” all at a glance.

Dashboard Metrics

Comprehensive Findings

Structured analysis report categorizing issues into Critical Fixes and Stability Risks.

Findings Report

Deep Analysis Details

OOP violations, interface mapping, memory allocation tracking, and knowledge base.

Analysis Details

Console Output (Sample)

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

๐Ÿง  Feature Engineering (ML Input)

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 Analysis Approach

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.


โš™๏ธ Static Analysis Engine

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.


๐Ÿค– Machine Learning Model

  • 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.


๐Ÿ“Š Quality Scoring Logic

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)

๐Ÿง  How StaticGuard Differs from a Compiler

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.


๐Ÿšฉ The Problem

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.


๐Ÿ’ก Real-World Use Cases

  • Automated code review before deployment
  • Detecting performance bottlenecks in large codebases
  • Educational tool for learning clean coding practices
  • Pre-commit code quality validation

๐Ÿš€ Key Features

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
โš ๏ธ Issue Detection 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

โš™๏ธ How It Works

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  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
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Tech Stack

  • Core Logic: Java 17 (Parallel Analysis Orchestration)
  • Intelligence: Python (Scikit-Learn, RandomForest, Pandas)
  • Reporting: HTML5, Chart.js, Vanilla CSS
  • Data Layer: Google Gson

๐Ÿ“‚ Project Structure

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

โšก Setup & Usage

Prerequisites

  • JDK 17+ (Ensure java and javac are in your PATH)
  • Python 3.10+ (Required for ML inference)

1. Install ML Dependencies

pip install pandas scikit-learn

2. Build the Project (Windows/PowerShell)

# 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 $files

3. Run Analysis

Via Python Wrapper:

python run_analysis.py sample/ChaosTestFile.java

Via Direct Java:

java -cp "target/classes;lib/*" com.staticguard.Main sample/ChaosTestFile.java

4. View Results

start results/report.html

๐Ÿ”ฎ Future Roadmap

  • 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.

โš ๏ธ Limitations

  • 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

๐Ÿ“Œ Conclusion

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages