use of org.springframework.data.mapping.Parameter in project spring-data-commons by spring-projects.
the class DtoInstantiatingConverter method convert.
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@NonNull
@Override
public Object convert(Object source) {
if (targetType.isInterface()) {
return source;
}
PersistentEntity<?, ? extends PersistentProperty<?>> sourceEntity = context.getRequiredPersistentEntity(source.getClass());
PersistentPropertyAccessor<Object> sourceAccessor = sourceEntity.getPropertyAccessor(source);
PersistentEntity<?, ? extends PersistentProperty<?>> targetEntity = context.getRequiredPersistentEntity(targetType);
@SuppressWarnings({ "rawtypes", "unchecked" }) Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {
@Override
@Nullable
public Object getParameterValue(Parameter parameter) {
String name = parameter.getName();
if (name == null) {
throw new IllegalArgumentException(String.format("Parameter %s does not have a name", parameter));
}
return sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(name));
}
});
PersistentPropertyAccessor<Object> targetAccessor = targetEntity.getPropertyAccessor(dto);
InstanceCreatorMetadata<? extends PersistentProperty<?>> creator = targetEntity.getInstanceCreatorMetadata();
targetEntity.doWithProperties((SimplePropertyHandler) property -> {
if ((creator != null) && creator.isCreatorParameter(property)) {
return;
}
targetAccessor.setProperty(property, sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(property.getName())));
});
return dto;
}
use of org.springframework.data.mapping.Parameter in project spring-data-commons by spring-projects.
the class InstanceCreatorMetadataDiscoverer method getFactoryMethod.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T, P extends PersistentProperty<P>> FactoryMethod<Object, P> getFactoryMethod(PersistentEntity<T, P> entity, Method method) {
Parameter<Object, P>[] parameters = new Parameter[method.getParameterCount()];
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
List<TypeInformation<?>> types = entity.getTypeInformation().getParameterTypes(method);
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
for (int i = 0; i < parameters.length; i++) {
String name = parameterNames == null || parameterNames.length <= i ? null : parameterNames[i];
TypeInformation<?> type = types.get(i);
Annotation[] annotations = parameterAnnotations[i];
parameters[i] = new Parameter(name, type, annotations, entity);
}
return new FactoryMethod<>(method, parameters);
}
Aggregations