generated from Umutayb/pickleib
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReflectionUtilities.java
676 lines (640 loc) · 28.4 KB
/
ReflectionUtilities.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package utils.reflection;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import utils.mapping.MappingUtilities;
import utils.Printer;
import utils.StringUtilities;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class ReflectionUtilities {
static Printer log = new Printer(ReflectionUtilities.class);
static ObjectMapper mapper = MappingUtilities.Json.mapper;
/**
* Iteratively invokes a specified method or conditional function until a condition is met
* or a timeout is reached.
*
* @param timeoutInSeconds The time limit (in seconds) for the iteration.
* @param parent The class containing the method to be invoked. (Deprecated: Use {@link ReflectionUtilities#iterativeConditionalInvocation(int, ConditionalFunction)})
* @param methodName The name of the method to invoke. (Deprecated: Use {@link ReflectionUtilities#iterativeConditionalInvocation(int, ConditionalFunction)})
* @param args The arguments to pass to the method when invoked. (Deprecated: Use {@link ReflectionUtilities#iterativeConditionalInvocation(int, ConditionalFunction)}
* @param <T> The type of the parent class.
* @return True if the condition is met within the specified timeout; otherwise, false.
* @throws RuntimeException if an exception occurs during method invocation.
* @deprecated Since version 1.9.7, use {@link ReflectionUtilities#iterativeConditionalInvocation(int, ConditionalFunction)} instead
*/
@Deprecated(since = "1.9.7")
public static <T> boolean iterativeConditionalInvocation(
int timeoutInSeconds,
Class<T> parent,
String methodName,
Object... args) {
boolean condition;
long startingTime = System.currentTimeMillis();
int interval = (int) Math.pow(timeoutInSeconds, 0.5);
log.info("Iterating at " + interval + " second intervals.");
try {
do {
Method method = getMethod(methodName, parent);
method.setAccessible(true);
condition = Boolean.parseBoolean(String.valueOf(method.invoke(parent, args)));
if (condition) break;
TimeUnit.SECONDS.sleep(interval);
}
while (!((System.currentTimeMillis() - startingTime) / 1000 > timeoutInSeconds));
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException |
InterruptedException exception) {
throw new RuntimeException(exception);
}
return condition;
}
/**
* Iteratively invokes a specified method on a class and checks a condition until the condition is met
* or a timeout is reached.
* <p>
* Use this method when you have a specific {@link ConditionalFunction} that encapsulates the desired
* condition-checking logic, and you want to repeatedly execute it until the condition is met
* or a specified timeout is reached.
* <p>
* Example usage:
* <pre>{@code
* class Test1 {
* public static void main(String[] args) {
* int a = 2;
* int b = 1;
* int timeout = 30;
* iterativeConditionalInvocation(timeout, 5, () -> {return a - b > 0;});
* }
* }
*
* //OR:
* class Test2 {
* public static void main(String[] args) {
* int a = 2;
* int b = 1;
* int timeout = 30;
* iterativeConditionalInvocation(timeout, 5, () -> conditionalMethod(a, b));
* }
*
* public static boolean conditionalMethod(int a, int b) {
* return a - b < 0;
* }
* }
* }
* }</pre>
*
* @param timeoutInSeconds The time limit (in seconds) for the iteration.
* @return True if the condition is met within the specified timeout; otherwise, false.
* @throws RuntimeException if an exception occurs during method invocation.
*/
public static boolean iterativeConditionalInvocation(
int timeoutInSeconds,
int interval,
ConditionalFunction conditionalFunction
) {
boolean condition;
long startingTime = System.currentTimeMillis();
log.info("Iterating at " + interval + " second intervals.");
try {
int counter = 0;
do{
counter++;
log.info("Iteration #" + counter);
condition = conditionalFunction.execute();
if (condition) break;
TimeUnit.SECONDS.sleep(interval);
}
while (!((System.currentTimeMillis() - startingTime) / 1000 > timeoutInSeconds));
} catch (InterruptedException exception) {
throw new RuntimeException(exception);
}
return condition;
}
/**
* Iteratively invokes a specified method on a class and checks a condition until the condition is met
* or a timeout is reached.
*
* Use this method when you have a specific {@link ConditionalFunction} that encapsulates the desired
* condition-checking logic, and you want to repeatedly execute it until the condition is met
* or a specified timeout is reached.
*
* Example usage:
* <pre>{@code
* class Test1 {
* public static void main(String[] args) {
* int a = 2;
* int b = 1;
* int timeout = 30;
* iterativeConditionalInvocation(timeout, () -> {return a - b > 0;});
* }
* }
*
* //OR:
* class Test2 {
* public static void main(String[] args) {
* int a = 2;
* int b = 1;
* int timeout = 30;
* iterativeConditionalInvocation(timeout, () -> conditionalMethod(a, b));
* }
*
* public static boolean conditionalMethod(int a, int b) {
* return a - b < 0;
* }
* }
* }
* }</pre>
*
*
* @param timeoutInSeconds The time limit (in seconds) for the iteration.
* @return True if the condition is met within the specified timeout; otherwise, false.
* @throws RuntimeException if an exception occurs during method invocation.
*/
public static boolean iterativeConditionalInvocation(
int timeoutInSeconds,
ConditionalFunction conditionalFunction
) {
return iterativeConditionalInvocation(
timeoutInSeconds,
timeoutInSeconds / (int) Math.pow(timeoutInSeconds, 0.5),
conditionalFunction
);
}
/**
* Compares two objects and throws an AssertionError if they are not equal.
* This method is useful for testing purposes.
*
* @param expected the expected object
* @param actual the actual object
* @param exceptions the list of exceptions to ignore during comparison
* @throws AssertionError if the objects are not equal
*/
public static <T> void compareObjects(T expected, T actual, String... exceptions) {
try {
String expectedString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
String actualString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(actual);
JsonObject expectedJson = JsonParser.parseString(expectedString).getAsJsonObject();
JsonObject actualJson = JsonParser.parseString(actualString).getAsJsonObject();
compareJson(expectedJson, actualJson, exceptions);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* Compares two JSON objects and throws an assertion error if they do not match.
*
* @param expectedJson The expected JSON object.
* @param actualJson The actual JSON object.
* @param exceptions Optional field names to exclude from comparison.
* @throws AssertionError if the JSON objects do not match.
*/
public static void compareJson(JsonObject expectedJson, JsonObject actualJson, String... exceptions) {
Set<String> keySet = expectedJson.keySet().stream().filter(key -> !Arrays.asList(exceptions).contains(key))
.collect(Collectors.toSet());
for (String fieldName : keySet) {
boolean skip = Arrays.asList(exceptions).contains(fieldName);
boolean isNull = expectedJson.get(fieldName) == null;
boolean valueNull = expectedJson.get(fieldName).toString().equals("null");
boolean arrayIsEmpty = expectedJson.get(fieldName).toString().equals("[]");
boolean isObject = expectedJson.get(fieldName).isJsonObject();
boolean isArray = expectedJson.get(fieldName).isJsonArray();
boolean isPrimitive = expectedJson.get(fieldName).isJsonPrimitive();
if (!skip) {
if (!isNull && !valueNull && !arrayIsEmpty) {
if (isObject) {
compareJson(
expectedJson.get(fieldName).getAsJsonObject(),
actualJson.get(fieldName).getAsJsonObject(),
exceptions
);
} else if (isArray) {
compareJsonArray(
expectedJson.get(fieldName).getAsJsonArray(),
actualJson.get(fieldName).getAsJsonArray(),
exceptions
);
} else if (isPrimitive)
Assert.assertEquals("Values of the '" + fieldName + "' fields do not match!",
expectedJson.get(fieldName),
actualJson.get(fieldName)
);
else
throw new RuntimeException("Could not determine field (" + expectedJson.get(fieldName) + ") type!");
log.success("Match: " + fieldName + " -> " + actualJson.get(fieldName));
} else {
Assert.assertEquals("Values of the '" + fieldName + "' fields do not match!",
expectedJson.get(fieldName),
actualJson.get(fieldName)
);
log.success("Match: " + fieldName + " -> " + actualJson.get(fieldName));
}
}
}
}
/**
* Compares two JSON arrays and throws an Assertion Error if they are not identical.
*
* @param expectedJson the expected JSON array
* @param actualJson the actual JSON array to be compared with the expected JSON array
* @param exceptions optional list of JSON object keys to be excluded from the comparison
* @throws AssertionError if the arrays are not identical
*/
public static void compareJsonArray(JsonArray expectedJson, JsonArray actualJson, String... exceptions) {
log.info("Comparing json arrays...");
for (int index = 0; index <= expectedJson.size() - 1; index++) {
if (expectedJson.get(index).isJsonObject()) {
compareJson(
expectedJson.get(index).getAsJsonObject(),
actualJson.get(index).getAsJsonObject(),
exceptions
);
} else if (expectedJson.get(index).isJsonArray()) {
compareJsonArray(
expectedJson.get(index).getAsJsonArray(),
actualJson.get(index).getAsJsonArray(),
exceptions
);
} else
Assert.assertEquals("Array elements do not match!",
expectedJson.get(index),
actualJson.get(index)
);
}
log.success("Json arrays are identical!");
}
/**
* Compares two objects and returns a boolean indicating whether they match.
* This method is useful for testing purposes.
*
* @param expected the expected object
* @param actual the actual object
* @param exceptions the list of exceptions to ignore during comparison
* @return true if the objects match, false otherwise
*/
public static boolean objectsMatch(Object expected, Object actual, String... exceptions) {
try {
String expectedString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
String actualString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(actual);
JsonObject expectedJson = JsonParser.parseString(expectedString).getAsJsonObject();
JsonObject actualJson = JsonParser.parseString(actualString).getAsJsonObject();
compareJson(expectedJson, actualJson, exceptions);
} catch (AssertionError | JsonProcessingException error) {
log.warning(error.getMessage());
return false;
}
log.success("All fields match!");
return true;
}
/**
* Returns a Map of all accessible methods of the given object.
*
* @param object the object whose methods are to be retrieved
* @return a Map of method names to Method objects
*/
public static Map<String, Method> getMethods(Object object) {
Map<String, Method> methodMap = new HashMap<>();
for (Method method : object.getClass().getDeclaredMethods()) {
method.setAccessible(true);
methodMap.put(method.getName(), method);
}
return methodMap;
}
/**
* Returns a Method that represents the method with the specified name in the given object's class.
*
* @param methodName the name of the method to retrieve
* @param object the object whose class contains the method to retrieve
* @return the Method object that represents the method with the specified name
* @throws NoSuchMethodException if no method with the specified name could be located in the given object's class
*/
public static <T> Method getMethod(String methodName, Class<T> object) throws NoSuchMethodException {
for (Method method : object.getDeclaredMethods()) {
method.setAccessible(true);
if (method.getName().equals(methodName)) return method;
}
throw new NoSuchMethodException(
"No method named " + methodName + " could be located in class called" + object.getName()
);
}
/**
* This method retrieves the value of a field with a given field name and input class.
*
* @param fieldName The name of the field to retrieve the value from.
* @param inputClass The input class that contains the field to retrieve the value from.
* @return The value of the field with the given field name and input class.
* @throws RuntimeException If the field cannot be accessed or does not exist.
*/
public static <T> Object getFieldValue(String fieldName, Class<T> inputClass) {
try {
Field field = inputClass.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(inputClass);
} catch (IllegalAccessException | NoSuchFieldException exception) {
throw new RuntimeException(exception);
}
}
/**
* This method retrieves the value of a field with a given field name and input object instance.
*
* @param fieldName The name of the field to retrieve the value from.
* @param inputObject The input object that contains the field to retrieve the value from.
* @return The value of the field with the given field name and input object.
* @throws RuntimeException If the field cannot be accessed or does not exist.
*/
public static Object getField(String fieldName, Object inputObject) {
try {
Field field = inputObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(inputObject);
} catch (IllegalAccessException | NoSuchFieldException exception) {
throw new RuntimeException(exception);
}
}
/**
* This method set the value to a field of object with a given field name.
*
* @param object The object in which field the value is set.
* @param fieldName The name of the field to set the value.
* @param fieldValue The value to set the field of object.
* @throws RuntimeException If the field cannot be accessed or does not exist.
*/
public static void setField(Object object, String fieldName, Object fieldValue) {
try {
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, fieldValue);
} catch (IllegalAccessException | NoSuchFieldException exception) {
throw new RuntimeException(exception);
}
}
/**
* This method retrieves all the fields and their values in a given input class.
*
* @param inputClass The input class to retrieve the fields and their values from.
* @return A map containing the names and values of all the fields in the input class.
* @throws RuntimeException If any of the fields cannot be accessed.
*/
public static <T> Map<String, Object> getFields(Class<T> inputClass) {
Map<String, Object> fieldMap = new HashMap<>();
try {
for (Field field : inputClass.getDeclaredFields()) {
field.setAccessible(true);
fieldMap.put(field.getName(), field.get(inputClass));
}
} catch (IllegalAccessException exception) {
throw new RuntimeException(exception);
}
return fieldMap;
}
/**
* This method retrieves all the fields and their values in a given input object instance.
*
* @param inputObject The input object instance to retrieve the fields and their values from.
* @return A map containing the names and values of all the fields in the input class.
* @throws RuntimeException If any of the fields cannot be accessed.
*/
public static Map<String, Object> getFields(Object inputObject) {
Map<String, Object> fieldMap = new HashMap<>();
try {
for (Field field : inputObject.getClass().getDeclaredFields()) {
field.setAccessible(true);
fieldMap.put(field.getName(), field.get(inputObject));
}
} catch (IllegalAccessException exception) {
throw new RuntimeException(exception);
}
return fieldMap;
}
/**
* Prints the values of all fields of the given object.
*
* @param object the object whose fields are to be printed
*/
public static void printObjectFields(Object object) {
List<Field> fields = List.of(object.getClass().getDeclaredFields());
StringBuilder output = new StringBuilder();
try {
for (Field field : fields) {
field.setAccessible(true);
String fieldName = StringUtilities.firstLetterCapped(field.getName());
output.append("\n").append(fieldName).append(" : ").append(field.get(object));
}
log.important("\nFields: " + output);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Prints the values of all getter methods of the given object.
*
* @param object the object whose getter methods are to be printed
*/
public static void printModelGetterValues(Object object) {
Method[] methods = object.getClass().getDeclaredMethods();
StringBuilder output = new StringBuilder();
try {
for (Method method : methods)
if (method.getName().contains("get")) {
String fieldName = StringUtilities.firstLetterCapped(method.getName().replaceAll("get", ""));
output.append("\n").append(fieldName).append(" : ").append(method.invoke(object));
}
log.important("\nFields: " + output);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Acquires a json array from a given field
*
* @param field target field
* @return generated array
* @throws ClassNotFoundException throws if class not found
* @throws NoSuchFieldException throws if file not found
*/
public static JsonArray getJsonArray(Field field, boolean primitive, String... exceptions) throws ClassNotFoundException, NoSuchFieldException {
JsonArray array = new JsonArray();
if (!primitive) {
List<JsonObject> list = List.of(
getJsonObject(
Class.forName(
((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0].getTypeName()
),
new JsonObject(),
exceptions
)
);
for (JsonObject jsonObject : list) array.add(jsonObject);
} else {
List<String> list = List.of(getTypeName(field));
for (String jsonObject : list) array.add(jsonObject);
}
return array;
}
/**
* Returns a JsonObject representation of a given class instance, based on the provided JsonObject and
* optional list of field exceptions.
*
* @param clazz The class of the object to be created.
* @param json The JsonObject to be used as the basis for the object.
* @param exceptions An optional list of field names to be excluded from the object creation.
* @return A JsonObject representation of the class instance.
* @throws NoSuchFieldException If one of the provided field exceptions does not exist in the class.
* @throws ClassNotFoundException If the provided class name cannot be found.
*/
public static <T> JsonObject getJsonObject(Class<T> clazz, JsonObject json, String... exceptions) throws NoSuchFieldException, ClassNotFoundException {
List<Field> fields = List.of(clazz.getDeclaredFields());
for (Field field : fields) {
field.setAccessible(true);
if (Arrays.stream(exceptions).noneMatch(exception -> exception.equals(field.getName()))) {
boolean isMember = field.getType().isMemberClass();
boolean isList = fieldIsOfType(field, "List");
if (isMember)
json.add(field.getName(), getJsonObject(field.getType(), new JsonObject(), exceptions));
else if (isList)
json.add(field.getName(), getJsonArray(field, isPrimitive(field)));
else
json.addProperty(field.getName(), field.getType().getName());
}
}
return json;
}
/**
* Returns a JsonObject representation of a given class instance, based on the provided JsonObject and
* optional list of field exceptions.
*
* @param object The class of the object to be created.
* @param json The JsonObject to be used as the basis for the object.
* @param exceptions An optional list of field names to be excluded from the object creation.
* @return A JsonObject representation of the class instance.
* @throws NoSuchFieldException If one of the provided field exceptions does not exist in the class.
* @throws ClassNotFoundException If the provided class name cannot be found.
*/
public static <T> JsonObject getJsonFromObject(T object, JsonObject json, String... exceptions) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
List<Field> fields = List.of(object.getClass().getDeclaredFields());
for (Field field : fields) {
field.setAccessible(true);
if (Arrays.stream(exceptions).noneMatch(exception -> exception.equals(field.getName()))) {
boolean isMember = field.getType().isMemberClass();
boolean isList = fieldIsOfType(field, "List");
if (isMember)
json.add(field.getName(), getJsonFromObject(field, new JsonObject(), exceptions));
else if (isList)
json.add(field.getName(), getJsonArray(field, isPrimitive(field)));
else
json.add(field.getName(), (JsonElement) field.get(object));
}
}
return json;
}
/**
* Verifies type of given field
*
* @param field target field
* @param expectedType expected field type
* @return true or false
*/
public static boolean fieldIsOfType(Field field, String expectedType) {
return field.getType().getTypeName().contains(expectedType);
}
/**
* Verifies type of given object
*
* @param object target object
* @param type expected object type
* @return true or false
*/
public static <Type> boolean isOfType(Type object, Class<Type> type) {
try {
String jsonString = MappingUtilities.Json.getJsonStringFor(object);
return isOfType(jsonString, type);
} catch (IllegalArgumentException e) {
return false;
}
}
/**
* Verifies type of given object
*
* @param objectString expected object string
* @param type expected object type
* @return true or false
*/
public static <Type> boolean isOfType(String objectString, Class<Type> type) {
try {
MappingUtilities.Json.fromJsonString(objectString, type);
return true;
} catch (JsonProcessingException | IllegalArgumentException e) {
return false;
}
}
/**
* Acquires the type of given field
*
* @param field target field
* @return field type
*/
public static String getTypeName(Field field) {
ParameterizedType type = (ParameterizedType) field.getGenericType();
return type.getActualTypeArguments()[0].getTypeName();
}
/**
* Checks a given field type is primitive
*
* @param field target field
* @return a boolean
*/
public static boolean isPrimitive(Field field) {
return switch (getTypeName(field)) {
case "java.lang.Integer",
"java.lang.Boolean",
"java.lang.Char",
"java.lang.Double",
"java.lang.Long",
"java.lang.Short",
"java.lang.Byte",
"java.lang.String" -> true;
default -> false;
};
}
/**
* Verifies a given list as a member of a class
*
* @param clazz target class
* @param field target field
* @param <T> type of the given class
* @return a boolean
*/
public static <T> boolean isMemberList(Class<T> clazz, Field field) {
List<Field> fields = List.of(clazz.getFields());
return fields.stream().anyMatch(
subField -> subField.getGenericType().getTypeName().equals(field.getGenericType().getTypeName())
);
}
/**
* Retrieves the name of the method that called the current method.
*
* @return A string representing the name of the calling method.
*/
public static String getPreviousMethodName() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
return stackTrace.length > 2 ? stackTrace[2].getMethodName() : null;
}
/**
* Retrieves the names of all declared fields in the given class, excluding synthetic fields such as "this$0".
*
* @param clazz the class whose field names are to be retrieved
* @return an array of field names declared in the specified class
* @throws NullPointerException if {@code clazz} is null
*/
public static String[] getAllFieldNames(Class<?> clazz) {
return Arrays.stream(clazz.getDeclaredFields())
.map(Field::getName)
.filter(name -> !name.equals("this$0")) // Excludes synthetic fields like inner class references
.toArray(String[]::new);
}
}