package org.springframework.data.projection;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nonnull;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
@RequiredArgsConstructor
class ProjectingMethodInterceptor implements MethodInterceptor {
private final @NonNull ProjectionFactory factory;
private final @NonNull MethodInterceptor delegate;
private final @NonNull ConversionService conversionService;
@Nullable
@Override
public Object invoke(@SuppressWarnings("null") @Nonnull MethodInvocation invocation) throws Throwable {
Object result = delegate.invoke(invocation);
if (result == null) {
return null;
}
TypeInformation<?> type = ClassTypeInformation.fromReturnTypeOf(invocation.getMethod());
Class<?> rawType = type.getType();
if (type.isCollectionLike() && !ClassUtils.isPrimitiveArray(rawType)) {
return projectCollectionElements(asCollection(result), type);
} else if (type.isMap()) {
return projectMapValues((Map<?, ?>) result, type);
} else if (conversionRequiredAndPossible(result, rawType)) {
return conversionService.convert(result, rawType);
} else {
return getProjection(result, rawType);
}
}
private Object projectCollectionElements(Collection<?> sources, TypeInformation<?> type) {
Class<?> rawType = type.getType();
TypeInformation<?> componentType = type.getComponentType();
Collection<Object> result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType,
componentType != null ? componentType.getType() : null, sources.size());
for (Object source : sources) {
result.add(getProjection(source, type.getRequiredComponentType().getType()));
}
if (rawType.isArray()) {
return result.toArray((Object[]) Array.newInstance(type.getRequiredComponentType().getType(), result.size()));
}
return result;
}
private Map<Object, Object> projectMapValues(Map<?, ?> sources, TypeInformation<?> type) {
Map<Object, Object> result = CollectionFactory.createMap(type.getType(), sources.size());
for (Entry<?, ?> source : sources.entrySet()) {
result.put(source.getKey(), getProjection(source.getValue(), type.getRequiredMapValueType().getType()));
}
return result;
}
@Nullable
private Object getProjection(Object result, Class<?> returnType) {
return result == null || ClassUtils.isAssignable(returnType, result.getClass()) ? result
: factory.createProjection(returnType, result);
}
private boolean conversionRequiredAndPossible(Object source, Class<?> targetType) {
if (source == null || targetType.isInstance(source)) {
return false;
}
return conversionService.canConvert(source.getClass(), targetType);
}
private static Collection<?> asCollection(Object source) {
Assert.notNull(source, "Source object must not be null!");
if (source instanceof Collection) {
return (Collection<?>) source;
} else if (source.getClass().isArray()) {
return Arrays.asList(ObjectUtils.toObjectArray(source));
} else {
return Collections.singleton(source);
}
}
}