forked from aswiezowski/JavaEntityGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_gen.py
215 lines (160 loc) · 6.38 KB
/
entity_gen.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import argparse
import re
from functools import reduce
# Usage: python ./entity_gen.py --equals --toString --hashCode --get --set --builder --class ClassName propertyType:propertyName ...
parser = argparse.ArgumentParser(description='Generate class with properties')
parser.add_argument('properties', metavar='Type:Name', nargs='+', help='properties to be used')
parser.add_argument('--class', dest="className", default="Entity")
parser.add_argument('--constructor', action='store_true')
parser.add_argument('--get', action='store_true')
parser.add_argument('--set', action='store_true')
parser.add_argument('--equals', action='store_true')
parser.add_argument('--hashCode', action='store_true')
parser.add_argument('--toString', action='store_true')
args = parser.parse_args()
def createEntity(args):
buildedString = ""
buildedString += getImports()
buildedString += "\n\n"
buildedString += getClassDefinition(args.className)
buildedString += "\n\n"
buildedString += getPropertiesDefinition(args.properties, not args.set)
if args.constructor:
buildedString += getConstructor(args.className, args.properties)
buildedString += "\n\n"
if args.get:
buildedString += getGetFunctions(args.properties)
if args.set:
buildedString += getSetFunctions(args.properties)
if args.equals:
buildedString += getEquals(args.className, args.properties)
buildedString += "\n"
if args.hashCode:
buildedString += getHashCode(args.properties)
buildedString += "\n"
if args.toString:
buildedString += getToString(args.properties)
buildedString += "\n"
buildedString += getClassClose()
return buildedString
def getType(arg):
return arg.split(":")[0]
def getName(arg):
return arg.split(":")[1]
def indent(num):
return " " * 4 * num
def getImports():
return """import java.util.Objects;
import com.google.common.base.MoreObjects;"""
def getClassDefinition(className):
return "public final class " + className + "{"
def getPropertiesDefinition(properites, isFinal):
str = ""
finalModifier = ""
if isFinal:
finalModifier = "final "
for prop in properites:
str += indent(1) + "private " + finalModifier + getType(prop) + " " + getName(prop) + ";\n\n"
return str
def getConstructor(className, properties):
is_first = True
str = indent(1) + "public " + className + "("
for prop in properties:
if(is_first):
str += getType(prop) + " " + getName(prop)
is_first = False
else:
str += ", " + getType(prop) + " " + getName(prop)
str += ") {\n"
for prop in properties:
str += indent(2) + "this." + getName(prop) + " = " + getName(prop) + ";\n"
str += indent(1) + "}"
return str
def getGetFunctions(properites):
str = ""
for prop in args.properties:
str += indent(1) + "public final " + getType(prop) + " get" + formatVariableName(
prop) + "(){\n" + " return " + getName(prop) + ";\n" + indent(1) + "}\n\n"
return str
def getSetFunctions(properties):
str = ""
for prop in properties:
str += indent(1) + "public final void set" + formatVariableName(prop) + "(" + getType(
prop) + " p" + formatVariableName(prop) + "){\n" + indent(2) + getName(prop) + " = p" + formatVariableName(
prop) + ";\n" + indent(1) + "}\n\n"
return str
def formatVariableName(name):
return getName(name)[0].upper() + getName(name)[1:]
def getEquals(className, properties):
objName = className.lower()
str = indent(1) + """@Override
public boolean equals(Object object){
if(object == this){
return true;
}\n"""
str += indent(2) + "if(object instanceof " + args.className + "){\n" + indent(
3) + args.className + " " + objName + " = (" + args.className + ") object;\n"
str += indent(3) + "if(" + reduce(lambda x, y: x + " && " + y, map(
lambda prop: "Objects.equals(" + getName(prop) + ", " + objName + "." + getName(prop) + ")",
properties)) + ") {\n"
str += """ return true;
}
}
return false;
}"""
return str
def getHashCode(properties):
str = indent(1) + """@Override
public int hashCode(){\n"""
str += indent(2) + "return Objects.hash(" + reduce(lambda x, y: x + ", " + y,
map(lambda x: getName(x), properties)) + ");\n" + indent(1) + "}"
return str
def getToString(properties):
str = indent(1) + """@Override
public String toString(){\n"""
str += indent(2) + "return MoreObjects.toStringHelper(this)" + ''.join(
map(lambda x: "\n" + indent(3) + ".add(\"" + x + "\", " + x + ")",
map(lambda x: getName(x), properties))) + ".toString();\n" + " }"
return str
def getBuilderCreator():
return indent(1) + """public static Builder builder(){
return new Builder();
}\n"""
def getPropertiesForBuilder(properties):
str = ""
for prop in properties:
propertyType = getType(prop)
str += indent(2) + "private " + propertyType + " " + getName(prop) + getInitialization(propertyType) + ";\n"
return str
def getInitialization(propertyType):
match = re.search("^([^<]*)<([^>]*)>", propertyType)
if match:
return " = " + getDefaultInitialization(match.group(1))
else:
return ""
def getDefaultInitialization(genericType):
if genericType == "Optional":
return "Optional.empty()"
if genericType == "Set":
return "new HashSet<>()"
if genericType == "List":
return "new ArrayList<>()"
if genericType == "Map":
return "new HashMap<>()"
return "new " + genericType + "<>()"
def getSettersForBuilder(properties):
str = ""
for prop in properties:
str += indent(2) + "public Builder " + getName(prop) + "(" + getType(prop) + " p" + formatVariableName(
prop) + "){\n"
str += indent(3) + getName(prop) + " = p" + formatVariableName(prop) + ";\n"
str += indent(3) + "return this;\n"
str += indent(2) + "}\n\n"
return str
def getBuilderExecutor():
str = indent(2) + "public " + args.className + " build(){\n"
str += indent(3) + "return new " + args.className + "(this);\n" + indent(2)
return str + "}"
def getClassClose():
return "}"
print(createEntity(args))