Repository navigation
Expand file tree
/
Copy pathJavaClassUpdater.java
More file actions
385 lines (334 loc) · 12.5 KB
/
Copy pathJavaClassUpdater.java
File metadata and controls
385 lines (334 loc) · 12.5 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (c) 2010, ReportMill Software. All rights reserved.
*/
package javakit.resolver;
import snap.util.ArrayUtils;
import java.lang.reflect.*;
import java.util.*;
/**
* This class updates a JavaClass from resolver.
*/
public class JavaClassUpdater {
// The JavaClass
protected JavaClass _javaClass;
// The Resolver that produced this decl
protected Resolver _resolver;
// The real class (if available)
private Class<?> _realClass;
/**
* Constructor.
*/
public JavaClassUpdater(JavaClass aClass)
{
_javaClass = aClass;
_resolver = aClass._resolver;
}
/**
* Reloads the class using real class from resolver classloader. Returns whether the class changed during reload.
*/
public boolean reloadClass() throws SecurityException
{
// Get current values
JavaClass[] oldInterfaces = _javaClass.getInterfaces();
JavaTypeVariable[] oldTypeParams = _javaClass.getTypeParameters();
JavaClass[] oldInnerClasses = _javaClass.getDeclaredClasses();
JavaField[] oldFields = _javaClass.getDeclaredFields();
JavaMethod[] oldMethods = _javaClass.getDeclaredMethods();
JavaConstructor[] oldConstrs = _javaClass.getDeclaredConstructors();
// Get real class
_realClass = getRealClassImpl();
if (_realClass == null) {
System.err.println("JavaClass: Failed to load class: " + _javaClass.getClassName());
return false;
}
// Declare return value for whether class changed
boolean classChanged = false;
// Update modifiers
int oldMods = _javaClass._mods;
int newMods = _javaClass._mods = getModifiers();
if (oldMods != newMods)
classChanged = true;
// Update superclass
String superClassName = getSuperClassName();
if (!Objects.equals(superClassName, _javaClass._superClassName)) {
_javaClass._superClassName = superClassName;
_javaClass._superClass = null;
classChanged = true;
}
// Update interfaces
JavaClass[] newInterfaces = _javaClass._interfaces = getInterfaces();
if (!Arrays.equals(oldInterfaces, newInterfaces))
classChanged = true;
// Update type parameters
JavaTypeVariable[] newTypeParams = _javaClass._typeParameters = getTypeParameters();
if (!Arrays.equals(oldTypeParams, newTypeParams))
classChanged = true;
// Update inner classes
JavaClass[] newInnerClasses = _javaClass._innerClasses = getDeclaredClasses();
if (!Arrays.equals(oldInnerClasses, newInnerClasses))
classChanged = true;
// Update fields
JavaField[] newFields = _javaClass._fields = getMergedFields(oldFields);
if (newFields != oldFields)
classChanged = true;
// Update methods
JavaMethod[] newMethods = _javaClass._methods = getMergedMethods(oldMethods);
if (newMethods != oldMethods)
classChanged = true;
// Update constructors
JavaConstructor[] newConstrs = _javaClass._constructors = getMergedConstructors(oldConstrs);
if (newConstrs != oldConstrs)
classChanged = true;
// Return
return classChanged;
}
/**
* Returns the real class.
*/
protected Class<?> getRealClassImpl()
{
String className = _javaClass.getClassName();
Class<?> realClass = _resolver.getClassForName(className);
if (realClass == null) {
//System.err.println("JavaClassUpdater.getRealClassImpl: Can't find real class: " + className);
JavaClass superClass = _javaClass.getSuperClass();
realClass = superClass != null ? superClass.getRealClass() : Object.class;
}
return realClass;
}
/**
* Returns the modifiers.
*/
protected int getModifiers() { return _realClass.getModifiers(); }
/**
* Returns the super class name.
*/
protected String getSuperClassName()
{
Class<?> superClass = _realClass.getSuperclass();
return superClass != null ? superClass.getName() : null;
}
/**
* Returns the generic superclass.
*/
protected JavaType getGenericSuperclass()
{
Class<?> realClass = getRealClassImpl();
Type genericSuperClass = realClass.getGenericSuperclass();
return genericSuperClass != null ? _resolver.getJavaTypeForType(genericSuperClass) : null;
}
/**
* Returns interfaces.
*/
protected JavaClass[] getInterfaces()
{
Class<?> realClass = getRealClassImpl();
Class<?>[] interfaces = realClass.getInterfaces();
return ArrayUtils.map(interfaces, _javaClass::getJavaClassForClass, JavaClass.class);
}
/**
* Returns interfaces.
*/
protected JavaType[] getGenericInterfaces()
{
Class<?> realClass = getRealClassImpl();
Type[] interfaces = realClass.getGenericInterfaces();
return ArrayUtils.map(interfaces, _resolver::getJavaTypeForType, JavaType.class);
}
/**
* Returns JavaTypeVariable array for given class type parameters.
*/
protected JavaTypeVariable[] getTypeParameters()
{
Class<?> realClass = getRealClassImpl();
TypeVariable<?>[] typeVariables = realClass.getTypeParameters();
return ArrayUtils.map(typeVariables, tvar -> new JavaTypeVariable(_resolver, _javaClass, tvar), JavaTypeVariable.class);
}
/**
* Returns JavaClass array of declared inner classes for given class.
*/
protected JavaClass[] getDeclaredClasses()
{
// Get Inner Classes
Class<?> realClass = getRealClassImpl();
Class<?>[] innerClasses;
try { innerClasses = realClass.getDeclaredClasses(); }
catch (Throwable e) {
System.err.println("JavaClassUpdater.getDeclaredClasses: Can't get declared classes: " + e);
return new JavaClass[0];
}
// Add JavaDecl for each inner class
return ArrayUtils.map(innerClasses, _resolver::getJavaClassForClass, JavaClass.class);
}
/**
* Returns JavaField array for given class.
*/
protected JavaField[] getDeclaredFields()
{
Class<?> realClass = getRealClassImpl();
Field[] fields;
try { fields = realClass.getDeclaredFields(); }
catch (Throwable e) { e.printStackTrace(); return new JavaField[0]; }
return ArrayUtils.map(fields, field -> new JavaField(_resolver, _javaClass, field), JavaField.class);
}
/**
* Returns JavaMethod array for given class.
*/
protected JavaMethod[] getDeclaredMethods()
{
Class<?> realClass = getRealClassImpl();
Method[] methods;
try { methods = realClass.getDeclaredMethods(); }
catch (Throwable e) { e.printStackTrace(); return new JavaMethod[0]; }
return ArrayUtils.mapNonNull(methods, this::getJavaMethodForMethod, JavaMethod.class);
}
/**
* Returns a JavaMethod for given Method.
*/
private JavaMethod getJavaMethodForMethod(Method aMethod)
{
if (aMethod.isSynthetic())
return null;
return new JavaMethod(_resolver, _javaClass, aMethod);
}
/**
* Returns JavaConstructor array for given class.
*/
protected JavaConstructor[] getDeclaredConstructors()
{
Class<?> realClass = getRealClassImpl();
Constructor<?>[] constrs;
try { constrs = realClass.getDeclaredConstructors(); }
catch (Throwable e) { e.printStackTrace(); return new JavaConstructor[0]; }
return ArrayUtils.mapNonNull(constrs, this::getJavaConstructorForConstructor, JavaConstructor.class);
}
/**
* Returns a JavaConstructor for given Constructor.
*/
private JavaConstructor getJavaConstructorForConstructor(Constructor<?> aConstr)
{
if (aConstr.isSynthetic())
return null;
return new JavaConstructor(_resolver, _javaClass, aConstr);
}
/**
* Returns the enum constants.
*/
public Object[] getEnumConstants()
{
Class<?> realClass = getRealClassImpl();
return realClass.getEnumConstants();
}
/**
* Merges an array of old fields and new fields.
*/
protected JavaField[] getMergedFields(JavaField[] oldFields)
{
JavaField[] newFields = getDeclaredFields();
boolean didChange = newFields.length != oldFields.length;
// Iterate over new fields and merge with old
for (int i = 0; i < newFields.length; i++) {
JavaField newField = newFields[i];
JavaField oldField = ArrayUtils.findMatch(oldFields, field -> field.equals(newField));
if (oldField != null) {
if (oldField.mergeField(newField))
didChange = true;
newFields[i] = oldField;
}
else didChange = true;
}
// Return
return didChange ? newFields : oldFields;
}
/**
* Merges an array of old methods with new methods.
*/
protected JavaMethod[] getMergedMethods(JavaMethod[] oldMethods)
{
JavaMethod[] newMethods = getDeclaredMethods();
boolean didChange = newMethods.length != oldMethods.length;
// Iterate over new fields and merge with old
for (int i = 0; i < newMethods.length; i++) {
JavaMethod newMethod = newMethods[i];
JavaMethod oldMethod = ArrayUtils.findMatch(oldMethods, method -> method.equals(newMethod));
if (oldMethod != null) {
if (mergeMethod(oldMethod, newMethod))
didChange = true;
newMethods[i] = oldMethod;
}
else didChange = true;
}
// Return
return didChange ? newMethods : oldMethods;
}
/**
* Merges an array of old constructors with new constructors.
*/
protected JavaConstructor[] getMergedConstructors(JavaConstructor[] oldConstructors)
{
JavaConstructor[] newConstructors = getDeclaredConstructors();
boolean didChange = newConstructors.length != oldConstructors.length;
// Iterate over new constructors and merge with old
for (int i = 0; i < newConstructors.length; i++) {
JavaConstructor newConstr = newConstructors[i];
JavaConstructor oldConstr = ArrayUtils.findMatch(oldConstructors, constr -> constr.equals(newConstr));
if (oldConstr != null) {
if (mergeConstructor(oldConstr, newConstr))
didChange = true;
newConstructors[i] = oldConstr;
}
else didChange = true;
}
// Return
return didChange ? newConstructors : oldConstructors;
}
/**
* Merges the given new method into this method.
*/
private static boolean mergeMethod(JavaMethod oldMethod, JavaMethod newMethod)
{
// Update modifiers
boolean didChange = mergeExecutable(oldMethod, newMethod);
// Update return type
String oldMethodReturnTypeName = oldMethod.getGenericReturnType().getName();
String newMethodReturnTypeName = newMethod.getGenericReturnType().getName();
if (!oldMethodReturnTypeName.equals(newMethodReturnTypeName)) {
oldMethod._genericReturnType = newMethod.getGenericReturnType();
oldMethod._evalType = null;
didChange = true;
}
// Update Method
if (newMethod._method != null)
oldMethod._method = newMethod._method;
// Return
return didChange;
}
/**
* Merges the given new constructor into this constructor.
*/
private static boolean mergeConstructor(JavaConstructor oldConstr, JavaConstructor newConstr)
{
// Update modifiers
boolean didChange = mergeExecutable(oldConstr, newConstr);
// Update Constructor
if (newConstr._constructor != null)
oldConstr._constructor = newConstr._constructor;
// Return
return didChange;
}
/**
* Merges the given new constructor into this constructor.
*/
private static boolean mergeExecutable(JavaExecutable oldExec, JavaExecutable newExec)
{
// Update modifiers
boolean didChange = false;
if (newExec._mods != oldExec._mods) {
oldExec._mods = newExec._mods;
didChange = true;
}
// Update generic parameter types (these can change as long as base class is same)
// Return
return didChange;
}
}