Skip to content

Replacing java.lang.reflect to the PersistenceEntity accessor #2033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.*;
import org.springframework.data.util.ClassTypeInformation;
Expand Down Expand Up @@ -354,7 +355,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob

if (source instanceof SearchDocument) {
SearchDocument searchDocument = (SearchDocument) source;
populateScriptFields(result, searchDocument);
populateScriptFields(targetEntity, result, searchDocument);
}

return result;
Expand Down Expand Up @@ -555,27 +556,17 @@ private Object getPotentiallyConvertedSimpleRead(@Nullable Object value, @Nullab
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();
if (!fields.isEmpty()) {
for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
if (scriptedField != null) {
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
Object value = searchDocument.getFieldValue(name);
if (value != null) {
field.setAccessible(true);
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);
}
}
}
entity.doWithProperties((SimplePropertyHandler) property -> {
if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
Object value = searchDocument.getFieldValue(name);

entity.getPropertyAccessor(result).setProperty(property, value);
}
}
});
}

/**
Expand Down