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.
-
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
-
Knowledge Extraction
- Project architecture and patterns
- Module and class relationships
- Method signatures with complexity analysis
- Endpoint and API mapping
- Field constraints and validations
-
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
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
-
Clone the repository:
git clone [repository-url] cd java-to-node
-
Create a Python virtual environment:
python -m venv venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows
-
Install dependencies:
pip install -r requirements.txt
-
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. -
Set up your OpenAI API key:
# Linux/Mac export OPENAI_API_KEY='your-api-key' # Windows PowerShell $env:OPENAI_API_KEY='your-api-key'
- 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")
- 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
- Running Tests:
python -m pytest tests/
The tool produces both structured knowledge output (JSON) and converted Node.js files:
The tool generates two types of JSON files containing extracted knowledge:
-
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
-
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"
}
]
}
]
}
customer.model.ts
- MongoDB/Sequelize modelcustomer.controller.ts
- REST API endpointscustomer.service.ts
- Business logicapp.ts
- Application setupdatabase.config.ts
- Database configurationknowledge_output_simple.json
- Simple JSON outputknowledge_output.json
- Complete JSON output
The tool handles LLM token limits through several strategies:
-
Selective Extraction:
- Only essential code segments are sent to the LLM
- AST-based parsing minimizes token usage
- Key patterns are detected without LLM calls
-
Chunking and Context Management:
- Large files are processed in manageable chunks
- Context preservation between chunks
- Smart batching of related code segments
-
Caching and Patterns:
- Common patterns are cached
- Reusable code templates
- Local processing where possible
-
Response Processing:
- Responses are trimmed to essentials
- Structured output format
- Efficient content aggregation
-
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
-
Supported Patterns:
- Standard Spring annotations (@RestController, @Service, etc.)
- JPA/Hibernate annotations
- Basic dependency injection
- REST endpoints with standard mappings
- CRUD operations
-
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
-
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