Skip to content

This tool analyzes Java codebases and converts them to equivalent Node.js/TypeScript code using AI assistance. It extracts structured knowledge from Java files and generates corresponding Node.js implementations while preserving business logic and architectural patterns.

Notifications You must be signed in to change notification settings

shainjadhav/java_to_node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Java to Node.js Code Converter

This tool analyzes Java codebases and converts them to equivalent Node.js/TypeScript code using AI assistance. It extracts structured knowledge from Java files and generates corresponding Node.js implementations while preserving business logic and architectural patterns.

Features

  1. Java Code Analysis

    • Abstract Syntax Tree (AST) based Java code parsing using javalang
    • Intelligent file categorization (Controller, Service, Entity, etc.)
    • Structured metadata extraction
    • Relationship and dependency analysis
  2. Knowledge Extraction

    • Project architecture and patterns
    • Module and class relationships
    • Method signatures with complexity analysis
    • Endpoint and API mapping
    • Field constraints and validations
  3. Code Conversion

    • Spring Boot to Express/NestJS conversion
    • JPA entities to Mongoose/Sequelize models
    • REST controllers to TypeScript endpoints
    • Service layer business logic preservation
    • Dependency injection support

Project Structure

java_to_node/
├── src/
│   ├── code_reader.py       # Java file scanning and categorization
│   ├── knowledge_extractor.py  # AST parsing and info extraction
│   ├── code_converter.py    # Java to Node.js conversion
│   ├── llm_service.py      # LLM integration
│   ├── mock_llm_service.py # Mock LLM for testing
│   ├── config.py          # Configuration
│   └── main.py           # Entry point
├── examples/              # Generated code examples and knowledge output
│   ├── app.ts            # Generated Express/NestJS application
│   ├── customer.model.ts # Generated Mongoose model
│   ├── customer.controller.ts # Generated REST endpoints
│   ├── customer.service.ts   # Generated service layer
│   ├── database.config.ts    # Generated DB configuration
│   ├── knowledge_output.json # Detailed knowledge extraction
│   └── knowledge_output_simple.json # Simplified knowledge output
├── tests/
│   ├── test_code_converter.py
│   ├── test_code_reader.py
│   ├── test_knowledge_extractor.py
│   └── resources/
│       └── java_sample/    # Sample Java files for testing
└── requirements.txt

Installation

  1. Clone the repository:

    git clone [repository-url]
    cd java-to-node
  2. Create a Python virtual environment:

    python -m venv venv
    source venv/bin/activate  # Linux/Mac
    venv\Scripts\activate     # Windows
  3. Install dependencies:

    pip install -r requirements.txt
  4. Generate example outputs:

    # Generate using OpenAI (requires API key)
    python generate_files.py
    
    # Or generate using mock LLM (no API key needed)
    python generate_files.py --mock

    This will create converted TypeScript files and knowledge JSON files in the examples/ directory.

  5. Set up your OpenAI API key:

    # Linux/Mac
    export OPENAI_API_KEY='your-api-key'
    
    # Windows PowerShell
    $env:OPENAI_API_KEY='your-api-key'

Usage

  1. Basic Usage:
from src.code_reader import CodeReader
from src.knowledge_extractor import KnowledgeExtractor
from src.code_converter import CodeConverter

# Initialize components
reader = CodeReader()
extractor = KnowledgeExtractor()
converter = CodeConverter()

# Process Java files
java_files = reader.scan_java_files("path/to/java/project")
knowledge_base = extractor.extract_knowledge("path/to/java/project")

# Convert to Node.js
output_dir = converter.convert_codebase(knowledge_base, "output/path")
  1. Using Mock LLM Service: The project includes a mock LLM service for development and testing without requiring an OpenAI API key:
from src.mock_llm_service import MockLLMService
from src.llm_service import LLMService

# Initialize with mock service
llm = MockLLMService()  # For testing/offline development
# llm = LLMService()    # For production with OpenAI

# Use mock service in converter
converter = CodeConverter(llm_service=llm)

The mock service provides predefined responses for common code conversion patterns and supports:

  • Full CRUD operations
  • Controller/Service layer generation
  • Model definitions
  • Database configurations
  • TypeScript type definitions
  1. Running Tests:
python -m pytest tests/

Output Format

The tool produces both structured knowledge output (JSON) and converted Node.js files:

Knowledge Output Files

The tool generates two types of JSON files containing extracted knowledge:

  1. knowledge_output.json - Detailed Analysis

    • Complete project architecture
    • In-depth module relationships
    • Detailed method analysis
    • Full type information
    • Code patterns and complexity
    • Dependencies and interactions
    • Validation rules and constraints
  2. knowledge_output_simple.json - Simplified Overview

    • High-level project structure
    • Core module listing
    • Essential method signatures
    • Basic type information
    • Primary relationships

Example structure:

{
  "projectOverview": "Customer management system with CRUD operations",
  "modules": [
    {
      "name": "CustomerService",
      "description": "Service layer for customer operations",
      "methods": [
        {
          "name": "getCustomerById",
          "signature": "public Customer getCustomerById(int id)",
          "description": "Retrieves customer data by ID",
          "complexity": "Low",
          "params": [
            {
              "name": "id",
              "type": "Long",
              "description": "Customer identifier"
            }
          ],
          "return_type": "Customer"
        }
      ]
    }
  ]
}

Generated Files

  • customer.model.ts - MongoDB/Sequelize model
  • customer.controller.ts - REST API endpoints
  • customer.service.ts - Business logic
  • app.ts - Application setup
  • database.config.ts - Database configuration
  • knowledge_output_simple.json - Simple JSON output
  • knowledge_output.json - Complete JSON output

Token Limit Management

The tool handles LLM token limits through several strategies:

  1. Selective Extraction:

    • Only essential code segments are sent to the LLM
    • AST-based parsing minimizes token usage
    • Key patterns are detected without LLM calls
  2. Chunking and Context Management:

    • Large files are processed in manageable chunks
    • Context preservation between chunks
    • Smart batching of related code segments
  3. Caching and Patterns:

    • Common patterns are cached
    • Reusable code templates
    • Local processing where possible
  4. Response Processing:

    • Responses are trimmed to essentials
    • Structured output format
    • Efficient content aggregation

Assumptions and Limitations

  1. Java Codebase Structure:

    • Assumes Spring Boot project layout
    • Expects clear separation of concerns:
      • Controllers for REST endpoints
      • Services for business logic
      • Entities for data models
  2. Supported Patterns:

    • Standard Spring annotations (@RestController, @Service, etc.)
    • JPA/Hibernate annotations
    • Basic dependency injection
    • REST endpoints with standard mappings
    • CRUD operations
  3. Conversion Limitations:

    • Complex Spring features may not map 1:1
    • Advanced JPA features need review
    • Custom annotations require manual handling
    • Transaction management differences
    • Java-specific patterns may need alternatives
  4. General Constraints:

    • Large files may hit token limits
    • Generated code needs review
    • Some manual adjustments may be needed
    • Performance optimizations post-conversion
    • Environment-specific configurations

About

This tool analyzes Java codebases and converts them to equivalent Node.js/TypeScript code using AI assistance. It extracts structured knowledge from Java files and generates corresponding Node.js implementations while preserving business logic and architectural patterns.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published