Browse Source

DATACMNS-856, DATACMNS-685 - Made ToEntityConverter.matches(…) less offensive.

ToEntityConverter.matches(…) now doesn't throw an exception anymore as it might be picked up by classpath scanning an the exception previously thrown causing the entire conversion attempt to abort.

We're now rather inspecting the ToEntityConverter in case the target type is a repository managed one and the ToIdConverter otherwise.
pull/172/head
Oliver Gierke 10 years ago
parent
commit
c9fc0c9f36
  1. 30
      src/main/java/org/springframework/data/repository/support/DomainClassConverter.java

30
src/main/java/org/springframework/data/repository/support/DomainClassConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,8 +39,8 @@ import org.springframework.util.StringUtils; @@ -39,8 +39,8 @@ import org.springframework.util.StringUtils;
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class DomainClassConverter<T extends ConversionService & ConverterRegistry> implements
ConditionalGenericConverter, ApplicationContextAware {
public class DomainClassConverter<T extends ConversionService & ConverterRegistry>
implements ConditionalGenericConverter, ApplicationContextAware {
private final T conversionService;
private Repositories repositories = Repositories.NONE;
@ -73,8 +73,9 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -73,8 +73,9 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
*/
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return repositories.hasRepositoryFor(targetType.getType()) ? toEntityConverter.convert(source, sourceType,
targetType) : toIdConverter.convert(source, sourceType, targetType);
return repositories.hasRepositoryFor(targetType.getType())
? toEntityConverter.convert(source, sourceType, targetType)
: toIdConverter.convert(source, sourceType, targetType);
}
/*
@ -84,11 +85,8 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -84,11 +85,8 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
return toEntityConverter.matches(sourceType, targetType) || toIdConverter.matches(sourceType, targetType);
} catch (ConversionMatchAbbreviationException o_O) {
return false;
}
return repositories.hasRepositoryFor(targetType.getType()) ? toEntityConverter.matches(sourceType, targetType)
: toIdConverter.matches(sourceType, targetType);
}
/*
@ -176,13 +174,8 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -176,13 +174,8 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
Class<?> rawIdType = repositories.getRepositoryInformationFor(targetType.getType()).getIdType();
if (sourceType.equals(TypeDescriptor.valueOf(rawIdType))
|| conversionService.canConvert(sourceType.getType(), rawIdType)) {
return true;
}
// Throw an exception to indicate it should have matched an no further resolution should be tried
throw new ConversionMatchAbbreviationException();
return sourceType.equals(TypeDescriptor.valueOf(rawIdType))
|| conversionService.canConvert(sourceType.getType(), rawIdType);
}
}
@ -246,7 +239,4 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -246,7 +239,4 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
|| conversionService.canConvert(rawIdType, targetType.getType());
}
}
@SuppressWarnings("serial")
private static final class ConversionMatchAbbreviationException extends RuntimeException {}
}

Loading…
Cancel
Save