-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_java_to_ts.py
109 lines (91 loc) · 4.45 KB
/
convert_java_to_ts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import re
def java_type_to_typescript(java_type):
"""Map Java types to TypeScript types, including handling of generics, VO-to-Model conversion, BigDecimal, and byte[]."""
type_mappings = {
"int": "number",
"Integer": "number",
"float": "number",
"Float": "number",
"double": "number",
"Double": "number",
"long": "number",
"Long": "number",
"short": "number",
"Short": "number",
"byte": "number",
"Byte": "number",
"boolean": "boolean",
"Boolean": "boolean",
"char": "string",
"String": "string",
"BigDecimal": "number", # BigDecimal mapped to number
"byte[]": "Uint8Array" # byte[] mapped to Uint8Array for binary data
}
# Check for generic types like List<AnimalVO> or Array<AnimalVO>
generic_match = re.match(r"(List|Array)<(\w+)>", java_type)
if generic_match:
# Get the generic element type (e.g., AnimalVO)
element_type = generic_match.group(2)
# If the element type ends with VO, convert it to Model
if element_type.endswith("VO"):
element_type = element_type[:-2] + "Model"
# Return as Array<ModifiedType>
ts_type = f"Array<{element_type}>"
return ts_type
# If the type is a standalone VO (not in a generic), convert to Model
if java_type.endswith("VO"):
java_type = java_type[:-2] + "Model"
# Return the mapped type or default to the same name if not found in mappings
return type_mappings.get(java_type, java_type)
def to_kebab_case(name):
"""Convert PascalCase or camelCase to kebab-case."""
return re.sub(r'(?<!^)(?=[A-Z])', '-', name).lower()
def convert_java_file_to_ts(java_file_path, ts_file_path):
"""Convert a single Java POJO file to a TypeScript class with optional fields, without a constructor."""
with open(java_file_path, 'r') as java_file:
java_content = java_file.read()
# Extract the class name
class_name_match = re.search(r'public class\s+(\w+)', java_content)
if not class_name_match:
print(f"No class definition found in {java_file_path}. Skipping file.")
return
class_name = class_name_match.group(1)
# Rename class if it ends with VO (e.g., GoatVO -> GoatModel)
if class_name.endswith("VO"):
class_name = class_name[:-2] + "Model"
# Extract properties (support both public and private access modifiers, and handle optional initializations)
properties = re.findall(r'(public|private)\s+([\w\[\]<>]+)\s+(\w+)(?:\s*=\s*[^;]+)?;', java_content)
# Create the TypeScript class content with optional fields and public modifiers
ts_content = f"export class {class_name} {{\n"
# Property declarations with access modifiers (public or optional)
for access_modifier, java_type, prop_name in properties:
ts_type = java_type_to_typescript(java_type)
if access_modifier == "public":
ts_content += f" {prop_name}: {ts_type};\n" # public in TypeScript
else:
ts_content += f" {prop_name}?: {ts_type};\n" # private in TypeScript (optional)
ts_content += "}\n"
# Save to TypeScript file
with open(ts_file_path, 'w') as ts_file:
ts_file.write(ts_content)
print(f"Converted {java_file_path} to {ts_file_path}")
def convert_all_java_to_ts(entity_dir, models_dir):
"""Convert all Java POJO files in the entity_dir to TypeScript classes in models_dir."""
if not os.path.exists(models_dir):
os.makedirs(models_dir) # Create the models directory if it doesn't exist
for filename in os.listdir(entity_dir):
if filename.endswith(".java"):
java_file_path = os.path.join(entity_dir, filename)
# Remove VO suffix from filename if it exists, then convert to kebab-case and add .model.ts
base_name = filename.replace(".java", "")
if base_name.endswith("VO"):
base_name = base_name[:-2] # Remove "VO" suffix
ts_file_name = to_kebab_case(base_name) + ".model.ts"
ts_file_path = os.path.join(models_dir, ts_file_name)
convert_java_file_to_ts(java_file_path, ts_file_path)
# Define directories
entity_dir = './entity' # Directory containing Java files
models_dir = './models' # Directory to save TypeScript files
# Convert all Java files in the entity directory to TypeScript files in the models directory
convert_all_java_to_ts(entity_dir, models_dir)