|
15 | 15 | import static java.lang.String.format; |
16 | 16 | import static org.assertj.core.util.Preconditions.checkArgument; |
17 | 17 |
|
18 | | -import org.assertj.core.util.VisibleForTesting; |
19 | | - |
20 | 18 | import java.util.Map; |
21 | 19 |
|
| 20 | +import org.assertj.core.util.VisibleForTesting; |
| 21 | + |
22 | 22 | public class PropertyOrFieldSupport { |
23 | 23 | private static final String SEPARATOR = "."; |
24 | 24 | private PropertySupport propertySupport; |
@@ -60,28 +60,30 @@ public Object getValueOf(String propertyOrFieldName, Object input) { |
60 | 60 | return getSimpleValue(propertyOrFieldName, input); |
61 | 61 | } |
62 | 62 |
|
63 | | - public Object getSimpleValue(String propertyOrFieldName, Object input) { |
64 | | - // first check if input object is a map |
65 | | - if (input instanceof Map) { |
66 | | - Map<?, ?> map = (Map<?, ?>) input; |
67 | | - return map.get(propertyOrFieldName); |
68 | | - } |
69 | | - |
70 | | - // then try to get given property values from objects, then try fields |
| 63 | + public Object getSimpleValue(String name, Object input) { |
| 64 | + // try to get name as a property, then try as a field, then try as a map key |
71 | 65 | try { |
72 | | - return propertySupport.propertyValueOf(propertyOrFieldName, Object.class, input); |
| 66 | + return propertySupport.propertyValueOf(name, Object.class, input); |
73 | 67 | } catch (IntrospectionError propertyIntrospectionError) { |
74 | | - // no luck with properties, let's try fields |
| 68 | + // no luck as a property, let's try as a field |
75 | 69 | try { |
76 | | - return fieldSupport.fieldValue(propertyOrFieldName, Object.class, input); |
| 70 | + return fieldSupport.fieldValue(name, Object.class, input); |
77 | 71 | } catch (IntrospectionError fieldIntrospectionError) { |
78 | | - // no field nor property found with given name, it is considered as an error |
| 72 | + // neither field nor property found with given name |
| 73 | + |
| 74 | + // if the input object is a map, try name as a map key |
| 75 | + if (input instanceof Map) { |
| 76 | + Map<?, ?> map = (Map<?, ?>) input; |
| 77 | + return map.get(name); |
| 78 | + } |
| 79 | + |
| 80 | + // no value found with given name, it is considered as an error |
79 | 81 | String message = format("%nCan't find any field or property with name '%s'.%n" + |
80 | 82 | "Error when introspecting properties was :%n" + |
81 | 83 | "- %s %n" + |
82 | 84 | "Error when introspecting fields was :%n" + |
83 | 85 | "- %s", |
84 | | - propertyOrFieldName, propertyIntrospectionError.getMessage(), |
| 86 | + name, propertyIntrospectionError.getMessage(), |
85 | 87 | fieldIntrospectionError.getMessage()); |
86 | 88 | throw new IntrospectionError(message, fieldIntrospectionError); |
87 | 89 | } |
|
0 commit comments