DATACMNS-921 - ResultProcessor now gracefully falls back to approximate collections if necessary.

When handling collection results, we now create an approximate collection in case the creation of the exact same collection fails.
This commit is contained in:
Oliver Gierke
2016-09-26 15:01:14 +02:00
parent 7d901d2005
commit 024f1ae8bd
2 changed files with 38 additions and 1 deletions

View File

@@ -143,7 +143,7 @@ public class ResultProcessor {
if (source instanceof Collection && method.isCollectionQuery()) {
Collection<?> collection = (Collection<?>) source;
Collection<Object> target = CollectionFactory.createCollection(collection.getClass(), collection.size());
Collection<Object> target = createCollectionFor(collection);
for (Object columns : collection) {
target.add(type.isInstance(columns) ? columns : converter.convert(columns));
@@ -159,6 +159,22 @@ public class ResultProcessor {
return (T) converter.convert(source);
}
/**
* Creates a new {@link Collection} for the given source. Will try to create an instance of the source collection's
* type first falling back to creating an approximate collection if the former fails.
*
* @param source must not be {@literal null}.
* @return
*/
private static Collection<Object> createCollectionFor(Collection<?> source) {
try {
return CollectionFactory.createCollection(source.getClass(), source.size());
} catch (RuntimeException o_O) {
return CollectionFactory.createApproximateCollection(source, source.size());
}
}
@RequiredArgsConstructor(staticName = "of")
private static class ChainingConverter implements Converter<Object, Object> {

View File

@@ -245,6 +245,20 @@ public class ResultProcessorUnitTests {
assertThat(result, is(instanceOf(WrappingDto.class)));
}
/**
* @see DATACMNS-921
*/
@Test
public void fallsBackToApproximateCollectionIfNecessary() throws Exception {
ResultProcessor processor = getProcessor("findAllProjection");
SpecialList<Sample> specialList = new SpecialList<Sample>(new Object());
specialList.add(new Sample("Dave", "Matthews"));
processor.processResult(specialList);
}
private static ResultProcessor getProcessor(String methodName, Class<?>... parameters) throws Exception {
return getQueryMethod(methodName, parameters).getResultProcessor();
}
@@ -311,4 +325,11 @@ public class ResultProcessorUnitTests {
@Value("#{target.firstname + ' ' + target.lastname}")
String getFullName();
}
static class SpecialList<E> extends ArrayList<E> {
private static final long serialVersionUID = -6539525376878522158L;
public SpecialList(Object dummy) {}
}
}