forked from JanitSri/JavaCodeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_generator.py
More file actions
201 lines (156 loc) · 7.1 KB
/
Copy pathjava_generator.py
File metadata and controls
201 lines (156 loc) · 7.1 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import re
from generators.code_generator import CodeGeneratorInterface
class JavaCodeGenerator(CodeGeneratorInterface):
"""
Generate Java code
Parameters:
syntax_tree: syntax_tree of the drawio file
file_path: path for the code files to be written to
"""
def __init__(self, syntax_tree, file_path):
self.__syntax_tree = syntax_tree
self.file_path = file_path.strip('/')
self.__classes = list()
self.__properties = list()
self.__methods = list()
self.__files = list()
def generate_code(self):
"""
Use the syntax tree to generate code files for the UML class diagrams
"""
print("<<< GENERATING CODE FILES FROM SYNTAX TREE >>>")
try:
for _, _class in self.__syntax_tree.items():
file = ""
inheritance = ""
if len(_class['relationships']['extends']) > 0:
inheritance += "extends "
inheritance += ",".join([self.__syntax_tree[r]['name'] for r in _class['relationships']['extends']]).strip(",")
implementation = ""
if len(_class['relationships']['implements']) > 0:
implementation += "implements "
implementation += ",".join([self.__syntax_tree[r]['name'] for r in _class['relationships']['implements']]).strip(",")
interface_methods = list()
self.get_interface_methods(_class['relationships']['implements'], interface_methods)
file += self.generate_classes(_class['type'], _class['name'], inheritance, implementation)
file += "\n"
file += self.generate_properties(_class['properties'])
file += "\n"
file += self.generate_methods(_class['methods'], _class['properties'], _class['type'], interface_methods)
file += "}\n"
self.__files.append([_class['name'], file])
self.generate_files()
except Exception as e:
print(f"JavaCodeGenerator.generate_code ERROR: {e}")
def generate_classes(self, class_type, class_name, extends, implements):
"""
Generate the class header
Parameters:
class_type: type of class; 'class', 'abstract', 'interface'
class_name: name of class
extends: the classes extended by this class
implements: the interfaces implemented by this class
Returns:
class_header: class header string
"""
type_of_class = "public class" if class_type == "class" else class_type
type_of_class = class_type + " class" if class_type == "abstract" else type_of_class
class_header = f"{type_of_class} {class_name} {extends} {implements}" + " {\n"
class_header = re.sub(' +', ' ', class_header)
self.__classes.append(class_header)
return class_header
def get_classes(self):
"""
Getter for classes
"""
return self.__classes
def generate_properties(self, properties):
"""
Generate properties for the class
Parameters:
properties: dictionary of properties
Returns:
properties_string: string of the properties
"""
properties_string = ""
for _, _property_value in properties.items():
p = f"\t{_property_value['access']} {_property_value['type']} {_property_value['name']};\n"
self.__properties.append(p)
properties_string += p
return properties_string
def get_properties(self):
"""
Getter for properties
"""
return self.__properties
def generate_methods(self, methods, properties, class_type, interface_methods):
"""
Generate methods for the class
Parameters:
methods: dictionary of methods
properties: dictionary of properties
class_type: type of current class
interface_method: methods of implemented interfaces
Returns:
methods_string: string of the methods
"""
methods_string = ""
for _, method_value in methods.items():
m = f"\t{method_value['access']} {method_value['return_type']} {method_value['name']}() {{}}\n";
methods_string += m + "\n"
self.__methods.append(m)
# getter and setter methods
if class_type == "class" or class_type == "abstract":
for _, _property_value in properties.items():
if _property_value['access'] == "private":
getter = (f"\tpublic {_property_value['type']} get{_property_value['name'][0].upper() + _property_value['name'][1:]}()"
f" {{\n \t\treturn this.{_property_value['name']}; \n\t}}\n");
methods_string += getter + "\n"
self.__methods.append(getter)
setter = (f"\tpublic void set{_property_value['name'][0].upper() + _property_value['name'][1:]}({_property_value['type']} {_property_value['name']})"
f" {{\n \t\tthis.{_property_value['name']} = {_property_value['name']}; \n\t}}\n")
methods_string += setter + "\n"
self.__methods.append(setter)
for interface_method in interface_methods:
comment = "// ***requires implementation***"
m = (f"\t {interface_method['access']} {interface_method['return_type']} {interface_method['name']}()"
f" {{\n \t\t{comment} \n\t}}\n")
methods_string += m + "\n"
self.__methods.append(m)
return methods_string
def get_methods(self):
"""
Getter for the methods
"""
return self.__methods
def get_interface_methods(self, implements, interface_list):
"""
Get the interface methods that require implementation
Parameters:
implements: list of interfaces
interface_list: list of interface methods
"""
for i in implements:
interface_obj = self.__syntax_tree[i]
interface_list += interface_obj['methods'].values()
self.get_interface_methods(interface_obj['relationships']['implements'], interface_list)
def generate_files(self):
"""
Write generated code to file
Returns:
boolean: True if successful, False if unsuccessful
"""
print(f"<<< WRITING FILES TO {self.file_path} >>>")
try:
for file in self.get_files():
file_name = file[0] + ".java"
file_contents = file[1]
with open(self.file_path + f"/{file_name}", "w") as f:
f.write(file_contents)
except Exception as e:
print(f"JavaCodeGenerator.generate_files ERROR: {e}")
def get_files(self):
"""
Getter for the files
"""
return self.__files