|
50 | 50 | import org.springframework.data.mapping.MappingException;
|
51 | 51 | import org.springframework.data.mapping.PersistentPropertyAccessor;
|
52 | 52 | import org.springframework.data.mapping.PreferredConstructor;
|
| 53 | +import org.springframework.data.mapping.SimplePropertyHandler; |
53 | 54 | import org.springframework.data.mapping.context.MappingContext;
|
54 | 55 | import org.springframework.data.mapping.model.*;
|
55 | 56 | import org.springframework.data.util.ClassTypeInformation;
|
56 | 57 | import org.springframework.data.util.TypeInformation;
|
57 | 58 | import org.springframework.format.datetime.DateFormatterRegistrar;
|
58 | 59 | import org.springframework.lang.Nullable;
|
59 |
| -import org.springframework.util.Assert; |
60 |
| -import org.springframework.util.ClassUtils; |
61 |
| -import org.springframework.util.CollectionUtils; |
62 |
| -import org.springframework.util.ObjectUtils; |
| 60 | +import org.springframework.util.*; |
63 | 61 |
|
64 | 62 | /**
|
65 | 63 | * Elasticsearch specific {@link org.springframework.data.convert.EntityConverter} implementation based on domain type
|
@@ -354,7 +352,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
|
354 | 352 |
|
355 | 353 | if (source instanceof SearchDocument) {
|
356 | 354 | SearchDocument searchDocument = (SearchDocument) source;
|
357 |
| - populateScriptFields(result, searchDocument); |
| 355 | + populateScriptFields(targetEntity, result, searchDocument); |
358 | 356 | }
|
359 | 357 |
|
360 | 358 | return result;
|
@@ -555,27 +553,18 @@ private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullab
|
555 | 553 | return conversionService.convert(value, target);
|
556 | 554 | }
|
557 | 555 |
|
558 |
| - private <T> void populateScriptFields(T result, SearchDocument searchDocument) { |
| 556 | + private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T result, SearchDocument searchDocument) { |
559 | 557 | Map<String, List<Object>> fields = searchDocument.getFields();
|
560 |
| - if (!fields.isEmpty()) { |
561 |
| - for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { |
562 |
| - ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); |
563 |
| - if (scriptedField != null) { |
564 |
| - String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); |
565 |
| - Object value = searchDocument.getFieldValue(name); |
566 |
| - if (value != null) { |
567 |
| - field.setAccessible(true); |
568 |
| - try { |
569 |
| - field.set(result, value); |
570 |
| - } catch (IllegalArgumentException e) { |
571 |
| - throw new MappingException("failed to set scripted field: " + name + " with value: " + value, e); |
572 |
| - } catch (IllegalAccessException e) { |
573 |
| - throw new MappingException("failed to access scripted field: " + name, e); |
574 |
| - } |
575 |
| - } |
576 |
| - } |
| 558 | + entity.doWithProperties((SimplePropertyHandler) property -> { |
| 559 | + if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) { |
| 560 | + ScriptedField scriptedField = property.findAnnotation(ScriptedField.class); |
| 561 | + @SuppressWarnings({"constantCondition"}) |
| 562 | + String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name(); |
| 563 | + Object value = searchDocument.getFieldValue(name); |
| 564 | + |
| 565 | + entity.getPropertyAccessor(result).setProperty(property, value); |
577 | 566 | }
|
578 |
| - } |
| 567 | + }); |
579 | 568 | }
|
580 | 569 |
|
581 | 570 | /**
|
|
0 commit comments