|
18 | 18 | import org.slf4j.Logger; |
19 | 19 | import org.slf4j.LoggerFactory; |
20 | 20 |
|
| 21 | +import java.lang.reflect.Field; |
21 | 22 | import java.util.Map; |
22 | 23 | import java.util.Objects; |
23 | 24 | import java.util.Optional; |
|
29 | 30 | import java.util.stream.Stream; |
30 | 31 | import java.util.stream.StreamSupport; |
31 | 32 |
|
32 | | -import static org.joor.Reflect.on; |
33 | | - |
34 | 33 | /** |
35 | 34 | * @author charlie (Dmitry Baev). |
36 | 35 | */ |
@@ -88,9 +87,48 @@ private static String extractProperties(final Object object, final String[] part |
88 | 87 | .map(child -> extractProperties(child, parts, index)) |
89 | 88 | .collect(JOINER); |
90 | 89 | } |
91 | | - final Object child = on(object).get(parts[index]); |
| 90 | + final Object child = extractChild(object, parts[index]); |
92 | 91 | return extractProperties(child, parts, index + 1); |
93 | 92 | } |
94 | 93 | return ObjectUtils.toString(object); |
95 | 94 | } |
| 95 | + |
| 96 | + private static Object extractChild(final Object object, final String part) { |
| 97 | + final Class<?> type = object == null ? Object.class : object.getClass(); |
| 98 | + try { |
| 99 | + return extractField(object, part, type); |
| 100 | + } catch (ReflectiveOperationException e) { |
| 101 | + throw new IllegalStateException("Unable to extract " + part + " value from " + type.getName(), e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @SuppressWarnings("PMD.EmptyCatchBlock") |
| 106 | + private static Object extractField(final Object object, final String part, final Class<?> type) |
| 107 | + throws ReflectiveOperationException { |
| 108 | + try { |
| 109 | + final Field field = type.getField(part); |
| 110 | + return fieldValue(object, field); |
| 111 | + } catch (NoSuchFieldException e) { |
| 112 | + Class<?> t = type; |
| 113 | + while (t != null) { |
| 114 | + try { |
| 115 | + final Field declaredField = t.getDeclaredField(part); |
| 116 | + return fieldValue(object, declaredField); |
| 117 | + } catch (NoSuchFieldException ignore) { |
| 118 | + // Ignore |
| 119 | + } |
| 120 | + t = t.getSuperclass(); |
| 121 | + } |
| 122 | + throw e; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static Object fieldValue(final Object object, final Field field) throws IllegalAccessException { |
| 127 | + try { |
| 128 | + return field.get(object); |
| 129 | + } catch (IllegalAccessException e) { |
| 130 | + field.setAccessible(true); |
| 131 | + return field.get(object); |
| 132 | + } |
| 133 | + } |
96 | 134 | } |
0 commit comments