Skip to content

Commit 64d10c7

Browse files
committed
Replaced the java.lang.reflect to the PersistenceEntity of Spring DATA
1 parent 3203004 commit 64d10c7

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+13-24
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@
5050
import org.springframework.data.mapping.MappingException;
5151
import org.springframework.data.mapping.PersistentPropertyAccessor;
5252
import org.springframework.data.mapping.PreferredConstructor;
53+
import org.springframework.data.mapping.SimplePropertyHandler;
5354
import org.springframework.data.mapping.context.MappingContext;
5455
import org.springframework.data.mapping.model.*;
5556
import org.springframework.data.util.ClassTypeInformation;
5657
import org.springframework.data.util.TypeInformation;
5758
import org.springframework.format.datetime.DateFormatterRegistrar;
5859
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.*;
6361

6462
/**
6563
* 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
354352

355353
if (source instanceof SearchDocument) {
356354
SearchDocument searchDocument = (SearchDocument) source;
357-
populateScriptFields(result, searchDocument);
355+
populateScriptFields(targetEntity, result, searchDocument);
358356
}
359357

360358
return result;
@@ -555,27 +553,18 @@ private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullab
555553
return conversionService.convert(value, target);
556554
}
557555

558-
private <T> void populateScriptFields(T result, SearchDocument searchDocument) {
556+
private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T result, SearchDocument searchDocument) {
559557
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);
577566
}
578-
}
567+
});
579568
}
580569

581570
/**

0 commit comments

Comments
 (0)