|
28 | 28 | import org.springframework.beans.PropertyAccessorUtils; |
29 | 29 | import org.springframework.data.mapping.PersistentEntity; |
30 | 30 | import org.springframework.data.mapping.PersistentProperty; |
31 | | -import org.springframework.data.mapping.PersistentPropertyAccessor; |
32 | 31 | import org.springframework.data.mapping.context.PersistentEntities; |
| 32 | +import org.springframework.lang.Nullable; |
33 | 33 | import org.springframework.util.Assert; |
34 | 34 | import org.springframework.validation.AbstractPropertyBindingResult; |
35 | | -import org.springframework.validation.Errors; |
36 | 35 |
|
37 | 36 | /** |
38 | 37 | * An {@link Errors} implementation for use in the events mechanism of Spring Data REST. Customizes actual field lookup |
@@ -92,22 +91,29 @@ public Object getPropertyValue(String propertyName) throws BeansException { |
92 | 91 | * @param segment the property segment to look up, must not be {@literal null} or empty. |
93 | 92 | * @return |
94 | 93 | */ |
| 94 | + @Nullable |
95 | 95 | private Object lookupValueOn(Object value, String segment) { |
96 | 96 |
|
97 | | - Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(value.getClass()); |
| 97 | + Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities |
| 98 | + .getPersistentEntity(value.getClass()); |
| 99 | + |
| 100 | + return getAccessor(entity, value, segment).getPropertyValue(segment); |
| 101 | + } |
| 102 | + |
| 103 | + private static ConfigurablePropertyAccessor getAccessor( |
| 104 | + Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity, Object value, String segment) { |
| 105 | + |
98 | 106 | if (!entity.isPresent()) { |
99 | | - return new DirectFieldAccessor(value).getPropertyValue(segment); |
| 107 | + return PropertyAccessorFactory.forDirectFieldAccess(value); |
100 | 108 | } |
101 | 109 |
|
102 | 110 | PersistentProperty<?> property = entity // |
103 | 111 | .map(it -> it.getPersistentProperty(PropertyAccessorUtils.getPropertyName(segment))) // |
104 | 112 | .orElseThrow(() -> new NotReadablePropertyException(value.getClass(), segment)); |
105 | 113 |
|
106 | | - ConfigurablePropertyAccessor accessor = property.usePropertyAccess() // |
| 114 | + return property.usePropertyAccess() // |
107 | 115 | ? PropertyAccessorFactory.forBeanPropertyAccess(value) // |
108 | 116 | : PropertyAccessorFactory.forDirectFieldAccess(value); |
109 | | - |
110 | | - return accessor.getPropertyValue(segment); |
111 | 117 | } |
112 | 118 | }; |
113 | 119 | } |
|
0 commit comments