Skip to content

Commit 9d57410

Browse files
authored
Remove usage of java.lang.reflect in favour of use of the PersistenceEntity.
Original Pull Request spring-projects#2033 Closes spring-projects#2027
1 parent 3203004 commit 9d57410

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

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

+11-20
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
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;
@@ -354,7 +355,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
354355

355356
if (source instanceof SearchDocument) {
356357
SearchDocument searchDocument = (SearchDocument) source;
357-
populateScriptFields(result, searchDocument);
358+
populateScriptFields(targetEntity, result, searchDocument);
358359
}
359360

360361
return result;
@@ -555,27 +556,17 @@ private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullab
555556
return conversionService.convert(value, target);
556557
}
557558

558-
private <T> void populateScriptFields(T result, SearchDocument searchDocument) {
559+
private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T result, SearchDocument searchDocument) {
559560
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-
}
561+
entity.doWithProperties((SimplePropertyHandler) property -> {
562+
if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {
563+
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
564+
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
565+
Object value = searchDocument.getFieldValue(name);
566+
567+
entity.getPropertyAccessor(result).setProperty(property, value);
577568
}
578-
}
569+
});
579570
}
580571

581572
/**

0 commit comments

Comments
 (0)