-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLombokUtility.java
295 lines (294 loc) · 11.2 KB
/
LombokUtility.java
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import java.lang.reflect.*;
import org.lombok.annotations.*;
import javassist.*;
/**
** Author - Saksham Solanki
Start Date - 03 Oct 2021
Last Modified - 06 Oct 2021
*/
class LombokUtility
{
enum MethodType
{
SETTERS,
GETTERS,
SETTERS_AND_GETTERS
}
public static Boolean checkIfMethodAlreadyExist(Field field,String className,MethodType methodType) throws Exception
{
Class c = Class.forName(className);
if(methodType==methodType.SETTERS)
{
Boolean isMethodExist = false;
for(Method method : c.getMethods())
{
String setterMethod = "set"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length());
if(method.getName().equals(setterMethod))
{
isMethodExist = true;
break;
}
}
return isMethodExist;
}
//if getters exist
if(methodType==methodType.GETTERS)
{
Boolean isMethodExist = false;
for(Method method : c.getMethods())
{
String getterMethod = "get"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length());
if(method.getName().equals(getterMethod))
{
isMethodExist = true;
break;
}
}
return isMethodExist;
}
return false;
}
public static void generateMethodByType(Field field,String className,MethodType methodType) throws Exception
{
if(methodType==methodType.SETTERS)
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
//check if method doesn't exist before
if(!checkIfMethodAlreadyExist(field,className,MethodType.SETTERS))
{
String setterMethod = "public void set"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"("+field.getType().toString()
.substring(6)+" "+field.getName()+") {this."+field.getName()+"="+field.getName()+";"+"}";
CtMethod newsettermethod = CtNewMethod.make(setterMethod,ctclass);
ctclass.addMethod(newsettermethod);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}
if(methodType==methodType.GETTERS)
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
//check if getters already exist or not
if(!checkIfMethodAlreadyExist(field,className,MethodType.GETTERS))
{
String getterMethod = "public "+field.getType().toString()
.substring(6)+" get"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"() {return this."+field.getName()+";"+"}";
CtMethod newgettermethod = CtNewMethod.make(getterMethod,ctclass);
ctclass.addMethod(newgettermethod);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}
if(methodType==methodType.SETTERS_AND_GETTERS)
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
if(!checkIfMethodAlreadyExist(field,className,MethodType.SETTERS))
{
String setterMethod = "public void set"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"("+field.getType().toString()
.substring(6)+" "+field.getName()+") {this."+field.getName()+"="+field.getName()+";"+"}";
CtMethod newsettermethod = CtNewMethod.make(setterMethod,ctclass);
ctclass.addMethod(newsettermethod);
}
if(!checkIfMethodAlreadyExist(field,className,MethodType.GETTERS))
{
String getterMethod = "public "+field.getType().toString()
.substring(6)+" get"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"() {return this."+field.getName()+";"+"}";
CtMethod newgettermethod = CtNewMethod.make(getterMethod,ctclass);
ctclass.addMethod(newgettermethod);
}
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}
public static void generateMethodsByType(Field[] fields,String className,MethodType methodType) throws Exception
{
if(methodType==methodType.SETTERS)
{
//Generating Setters
for(Field field : fields)
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
//check if method doesn't exist before
if(!checkIfMethodAlreadyExist(field,className,MethodType.SETTERS))
{
String setterMethod = "public void set"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"("+field.getType().toString()
.substring(6)+" "+field.getName()+") {this."+field.getName()+"="+field.getName()+";"+"}";
CtMethod newsettermethod = CtNewMethod.make(setterMethod,ctclass);
ctclass.addMethod(newsettermethod);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}
}
if(methodType==methodType.GETTERS)
{
//Generating Getters
for(Field field : fields)
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
//check if getters already exist or not
if(!checkIfMethodAlreadyExist(field,className,MethodType.GETTERS))
{
String getterMethod = "public "+field.getType().toString()
.substring(6)+" get"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"() {return this."+field.getName()+";"+"}";
CtMethod newgettermethod = CtNewMethod.make(getterMethod,ctclass);
ctclass.addMethod(newgettermethod);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}
}
if(methodType==methodType.SETTERS_AND_GETTERS)
{
//Generating both Setters & Getters
for(Field field : fields)
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
if(!checkIfMethodAlreadyExist(field,className,MethodType.SETTERS))
{
String setterMethod = "public void set"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"("+field.getType().toString()
.substring(6)+" "+field.getName()+") {this."+field.getName()+"="+field.getName()+";"+"}";
CtMethod newsettermethod = CtNewMethod.make(setterMethod,ctclass);
ctclass.addMethod(newsettermethod);
}
if(!checkIfMethodAlreadyExist(field,className,MethodType.GETTERS))
{
String getterMethod = "public "+field.getType().toString()
.substring(6)+" get"+field.getName()
.substring(0,1).toUpperCase()+field.getName()
.substring(1,field.getName().length())+"() {return this."+field.getName()+";"+"}";
CtMethod newgettermethod = CtNewMethod.make(getterMethod,ctclass);
ctclass.addMethod(newgettermethod);
}
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}
}
public static void generateSetters(Field[] fields,String className) throws Exception
{
MethodType methodTypeSetter = MethodType.SETTERS;
generateMethodsByType(fields,className,methodTypeSetter);
}
public static void generateGetters(Field[] fields,String className) throws Exception
{
MethodType methodTypeGetter = MethodType.GETTERS;
generateMethodsByType(fields,className,methodTypeGetter);
}
public static void generateSettersAndGetters(Field[] fields,String className) throws Exception
{
MethodType methodTypeSetterAndGetter = MethodType.SETTERS_AND_GETTERS;
generateMethodsByType(fields,className,methodTypeSetterAndGetter);
}
public static void generateSetterAndGetter(Field field,String className) throws Exception
{
MethodType methodTypeSetterAndGetter = MethodType.SETTERS_AND_GETTERS;
generateMethodByType(field,className,methodTypeSetterAndGetter);
}
public static void generateGetter(Field field,String className) throws Exception
{
MethodType methodTypeGetter = MethodType.GETTERS;
generateMethodByType(field,className,methodTypeGetter);
}
public static void generateSetter(Field field,String className) throws Exception
{
MethodType methodTypeSetter = MethodType.SETTERS;
generateMethodByType(field,className,methodTypeSetter);
}
public static void generateNoArgumentConstructor(String className) throws Exception
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("public "+className+"(Object o){super();}");
CtConstructor constructor = CtNewConstructor.make(stringBuilder.toString(),ctclass);
ctclass.addConstructor(constructor);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
public static void generateAllArgumentsConstructor(Field[] fields,Constructor[] constructors,String className) throws Exception
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
int x = 0;
//Make StringBuilder from all the fields
//Before that first we have to check whether all args constructor exists or not
for(Constructor c : constructors)
{
if(c.getParameterTypes().length==fields.length)
{
//check if all param are matches if yes then throw an exception
x=0;
Class[] parameterTypes = c.getParameterTypes();
for(Class parameterType : parameterTypes)
{
for(Field field : fields)
{
if(parameterType.getName().equals(field.getName())) x++;
}
}
if(x==fields.length)
{
//all args Constructor already present
throw new Exception("All arguments constructor already present");
}
}
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("public "+className+"(");
for(x=0;x<fields.length;x++)
{
if(x==fields.length-1)
{
stringBuilder.append(fields[x].getType().toString().substring(6)+" "+fields[x].getName());
break;
}
else stringBuilder.append(fields[x].getType().toString().substring(6)+" "+fields[x].getName()+",");
}
stringBuilder.append(")\r\n");
stringBuilder.append("{\r\n");
for(x=0;x<fields.length;x++)
{
stringBuilder.append("this."+fields[x].getName()+" = "+fields[x].getName()+";"+"\r\n");
}
stringBuilder.append("}");
CtConstructor constructor = CtNewConstructor.make(stringBuilder.toString(),ctclass);
ctclass.addConstructor(constructor);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
public static void generateToString(String className) throws Exception
{
ClassPool cp=ClassPool.getDefault();
CtClass ctclass = ClassPool.getDefault().get(className);
//check if method doesn't exist before
String toStringMethod = "public String toString(){ return getClass().getName()+\"@\"+Integer.toHexString(hashCode());}";
CtMethod newToStringMethod = CtNewMethod.make(toStringMethod,ctclass);
ctclass.addMethod(newToStringMethod);
ctclass.writeFile();
if(ctclass.isFrozen()) ctclass.defrost();
}
}