removed entity to string id conversion routine as it is not required
This commit is contained in:
parent
064b6db666
commit
12e8f31b38
|
|
@ -61,7 +61,7 @@ public final class ConversionServiceFactory {
|
|||
conversionService.addConverterFactory(new CharacterToNumberFactory());
|
||||
conversionService.addConverter(new ObjectToStringConverter());
|
||||
conversionService.addGenericConverter(new ObjectToObjectGenericConverter());
|
||||
conversionService.addGenericConverter(new EntityConverter(conversionService));
|
||||
conversionService.addGenericConverter(new IdToEntityConverter(conversionService));
|
||||
return conversionService;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,59 +25,41 @@ import org.springframework.util.ReflectionUtils;
|
|||
|
||||
/**
|
||||
* Converts an entity identifier to a entity reference by calling a static finder method on the target entity type.
|
||||
* Also converts a entity reference to a String by printing its id property to String.
|
||||
* For id-to-entity conversion to match, the finder method must be public, static, have the signature 'find[EntityName]([IdType])', and return an instance of the desired entity type.
|
||||
* For entity-to-string conversion to match, a getter method for the 'id' property must be defined.
|
||||
* For this converter to match, the finder method must be public, static, have the signature 'find[EntityName]([IdType])', and return an instance of the desired entity type.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class EntityConverter implements ConditionalGenericConverter {
|
||||
final class IdToEntityConverter implements ConditionalGenericConverter {
|
||||
|
||||
private GenericConversionService conversionService;
|
||||
|
||||
public EntityConverter(GenericConversionService conversionService) {
|
||||
public IdToEntityConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
||||
public Class<?>[][] getConvertibleTypes() {
|
||||
return new Class[][] {
|
||||
{ Object.class, Object.class },
|
||||
{ Object.class, String.class }
|
||||
};
|
||||
return new Class[][] { { Object.class, Object.class } };
|
||||
}
|
||||
|
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (String.class.equals(targetType.getType())) {
|
||||
return getIdAccessor(sourceType.getType()) != null;
|
||||
} else {
|
||||
return getFinder(targetType.getType()) != null;
|
||||
}
|
||||
Method finder = getFinder(targetType.getType());
|
||||
return finder != null && this.conversionService.canConvert(sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0]));
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (source == null) {
|
||||
return this.conversionService.convertNullSource(sourceType, targetType);
|
||||
}
|
||||
if (String.class.equals(targetType.getType())) {
|
||||
Method idAccessor = getIdAccessor(sourceType.getType());
|
||||
Object id = ReflectionUtils.invokeMethod(idAccessor, source);
|
||||
return this.conversionService.convert(id, String.class);
|
||||
} else {
|
||||
Method finder = getFinder(targetType.getType());
|
||||
Object id = conversionService.convert(source, sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0]));
|
||||
return ReflectionUtils.invokeMethod(finder, source, id);
|
||||
}
|
||||
Method finder = getFinder(targetType.getType());
|
||||
Object id = this.conversionService.convert(source, sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0]));
|
||||
return ReflectionUtils.invokeMethod(finder, source, id);
|
||||
}
|
||||
|
||||
private Method getIdAccessor(Class<?> entityClass) {
|
||||
return ClassUtils.getMethodIfAvailable(entityClass, "getId");
|
||||
}
|
||||
|
||||
private Method getFinder(Class<?> entityClass) {
|
||||
String finderMethod = "find" + getEntityName(entityClass);
|
||||
Method[] methods = entityClass.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
if (Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 1) {
|
||||
if (Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 1 && method.getReturnType().equals(entityClass)) {
|
||||
if (method.getName().equals(finderMethod)) {
|
||||
return method;
|
||||
}
|
||||
|
|
@ -263,18 +263,6 @@ public class DefaultConversionTests {
|
|||
assertEquals(new Long(1), e.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToObjectToStringIdProperty() {
|
||||
String id = conversionService.convert(new TestEntity(1L), String.class);
|
||||
assertEquals("1", id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToObjectToStringIdPropertyWithNull() {
|
||||
String id = (String) conversionService.convert(null, TypeDescriptor.valueOf(TestEntity.class), TypeDescriptor.valueOf(String.class));
|
||||
assertNull(id);
|
||||
}
|
||||
|
||||
public static class TestEntity {
|
||||
|
||||
private Long id;
|
||||
|
|
|
|||
Loading…
Reference in New Issue