1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.tapas.evidence.utility;
20
21 import java.lang.reflect.Field;
22
23
24
25
26
27 public final class FieldUtils {
28
29 private FieldUtils() {
30 }
31
32
33
34
35
36
37
38
39 @SuppressWarnings("rawtypes")
40 public static Field[] getAllFields(final Class objectClass, final Field[] fields) throws ClassNotFoundException {
41 final Field[] totalFields = getFields(objectClass, fields);
42 final Class superClass = objectClass.getSuperclass();
43
44 Field[] finalFieldsArray;
45 Class clazz = null;
46
47 clazz = Class.forName("java.lang.Object");
48
49 if (superClass != null && !superClass.equals(clazz)) {
50 finalFieldsArray = getAllFields(superClass, totalFields);
51 } else {
52 finalFieldsArray = totalFields;
53 }
54
55 return finalFieldsArray;
56 }
57
58
59
60
61
62
63
64 @SuppressWarnings("rawtypes")
65 public static Field[] getFields(final Class objectClass, final Field[] fields) {
66 final Field[] newFields = objectClass.getDeclaredFields();
67
68 int fieldsSize = 0;
69 int newFieldsSize = 0;
70
71 if (fields != null) {
72 fieldsSize = fields.length;
73 }
74
75 if (newFields != null) {
76 newFieldsSize = newFields.length;
77 }
78
79 final Field[] totalFields = new Field[fieldsSize + newFieldsSize];
80
81 if (fieldsSize > 0) {
82 System.arraycopy(fields, 0, totalFields, 0, fieldsSize);
83 }
84
85 if (newFieldsSize > 0) {
86 System.arraycopy(newFields, 0, totalFields, fieldsSize, newFieldsSize);
87 }
88 return totalFields;
89 }
90
91 public static void setField(final Object object, final String fieldName, final Object value)
92 throws NoSuchFieldException, IllegalAccessException {
93 final Field field = object.getClass().getDeclaredField(fieldName);
94 field.setAccessible(true);
95 field.set(object, value);
96 }
97 }