This project contains a Python script to automatically convert Java Plain Old Java Object (POJO) files into TypeScript classes. The script processes Java files within a specified directory, extracts class and variable definitions, and generates corresponding TypeScript classes with optional or public properties.
- Converts Java POJOs to TypeScript classes.
- Handles
public
andprivate
classes:public
classes are converted to TypeScript.private
classes are skipped.
- Supports common Java types like
String
,int
,BigDecimal
, andbyte[]
with appropriate TypeScript mappings. - Renames classes ending with
VO
to end withModel
in TypeScript. - Generates TypeScript files in kebab-case (e.g.,
UserDataVO.java
->user-data.model.ts
). - Marks
public
variables as non-optional in TypeScript andprivate
variables as optional.
Java Type | TypeScript Type |
---|---|
int , Integer |
number |
BigDecimal |
number |
String |
string |
byte[] |
Uint8Array |
List<T> , Array<T> |
Array<T> |
- Make sure you have Python installed.
- Clone or download this project.
-
Place your Java files in a directory named
./entity
within the project root. -
Run the script:
python convert_java_to_ts.py
-
Converted TypeScript files will be generated in the
./models
directory.
convert_all_java_to_ts.py
: This Python script reads all.java
files in the./entity
directory, processes them, and outputs TypeScript files in./models
.
- File Conversion:
- Reads Java files in the
./entity
directory. - Checks each file for a
public
orprivate
class. - Converts
public
classes to TypeScript and skipsprivate
classes.
- Reads Java files in the
- Class Renaming:
- If a class name ends with
VO
, it is converted to end withModel
in TypeScript. - Converts Java files to kebab-case
.model.ts
files.
- If a class name ends with
- Property Conversion:
- Extracts
public
andprivate
fields, mapping Java types to TypeScript equivalents. - Public fields are converted to non-optional properties in TypeScript, while private fields are made optional.
- Extracts
java
public class UserDataVO {
public String userName = null;
private BigDecimal balance;
public byte[] profileImage = null;
}
The script generates a TypeScript file user-data.model.ts:
typescript
export class UserDataModel {
userName: string;
balance?: number;
profileImage: Uint8Array;
}
License This project is open-source and available under the Apache License 2.0