mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 20:42:11 +00:00
Remove usage of java.lang.reflect in favour of use of the PersistenceEntity.
Original Pull Request #2033 Closes #2027
This commit is contained in:
parent
32030043a3
commit
9d574103e2
@ -50,6 +50,7 @@ import org.springframework.data.elasticsearch.core.query.SourceFilter;
|
|||||||
import org.springframework.data.mapping.MappingException;
|
import org.springframework.data.mapping.MappingException;
|
||||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||||
import org.springframework.data.mapping.PreferredConstructor;
|
import org.springframework.data.mapping.PreferredConstructor;
|
||||||
|
import org.springframework.data.mapping.SimplePropertyHandler;
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
import org.springframework.data.mapping.context.MappingContext;
|
||||||
import org.springframework.data.mapping.model.*;
|
import org.springframework.data.mapping.model.*;
|
||||||
import org.springframework.data.util.ClassTypeInformation;
|
import org.springframework.data.util.ClassTypeInformation;
|
||||||
@ -354,7 +355,7 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
if (source instanceof SearchDocument) {
|
if (source instanceof SearchDocument) {
|
||||||
SearchDocument searchDocument = (SearchDocument) source;
|
SearchDocument searchDocument = (SearchDocument) source;
|
||||||
populateScriptFields(result, searchDocument);
|
populateScriptFields(targetEntity, result, searchDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -555,27 +556,17 @@ public class MappingElasticsearchConverter
|
|||||||
return conversionService.convert(value, target);
|
return conversionService.convert(value, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void populateScriptFields(T result, SearchDocument searchDocument) {
|
private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T result, SearchDocument searchDocument) {
|
||||||
Map<String, List<Object>> fields = searchDocument.getFields();
|
Map<String, List<Object>> fields = searchDocument.getFields();
|
||||||
if (!fields.isEmpty()) {
|
entity.doWithProperties((SimplePropertyHandler) property -> {
|
||||||
for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
|
if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {
|
||||||
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
|
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
|
||||||
if (scriptedField != null) {
|
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
|
||||||
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
|
|
||||||
Object value = searchDocument.getFieldValue(name);
|
Object value = searchDocument.getFieldValue(name);
|
||||||
if (value != null) {
|
|
||||||
field.setAccessible(true);
|
entity.getPropertyAccessor(result).setProperty(property, value);
|
||||||
try {
|
|
||||||
field.set(result, value);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new MappingException("failed to set scripted field: " + name + " with value: " + value, e);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new MappingException("failed to access scripted field: " + name, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user