-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPropertiesExtractor.java
53 lines (44 loc) · 1.89 KB
/
PropertiesExtractor.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
package by.andd3dfx.common;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Write a function that accept one argument of object type and print all values of own properties (that are not
* inherited from prototype) except functions. If some properties are objects, then the function should print values of
* the object’s keys. {key1: ‘A’, key2: ‘B’, key3: {deepKey1: ‘C’, deepKey2: ‘D’}, key4: function(){}} -> ‘A B C D’.
*/
public class PropertiesExtractor {
public static String extract(Object obj) throws IllegalAccessException {
List<String> result = new ArrayList<>();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isSynthetic() || isStaticField(field)) {
continue;
}
Object value = extractValue(field, obj);
if (value == null) {
continue;
}
if (isPrimitiveOrString(value)) {
result.add(String.valueOf(value));
} else {
result.add(extract(value));
}
}
return result.stream().collect(Collectors.joining(" "));
}
private static Object extractValue(Field field, Object obj) throws IllegalAccessException {
boolean accessibleFlag = field.canAccess(obj);
field.setAccessible(true);
Object value = field.get(obj);
field.setAccessible(accessibleFlag);
return value;
}
private static boolean isStaticField(Field field) {
return java.lang.reflect.Modifier.isStatic(field.getModifiers());
}
private static boolean isPrimitiveOrString(Object obj) {
return obj.getClass().isPrimitive() || obj.getClass().getName().equals("java.lang.String");
}
}