From 12e8f31b3866bf0595a6bfef0d88a223fc1146c0 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Mon, 23 Nov 2009 00:07:39 +0000 Subject: [PATCH] removed entity to string id conversion routine as it is not required --- .../support/ConversionServiceFactory.java | 2 +- ...onverter.java => IdToEntityConverter.java} | 40 +++++-------------- .../support/DefaultConversionTests.java | 12 ------ 3 files changed, 12 insertions(+), 42 deletions(-) rename org.springframework.core/src/main/java/org/springframework/core/convert/support/{EntityConverter.java => IdToEntityConverter.java} (60%) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java index f7242f65203..aea4ad964c7 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java @@ -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; } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/IdToEntityConverter.java similarity index 60% rename from org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java rename to org.springframework.core/src/main/java/org/springframework/core/convert/support/IdToEntityConverter.java index c085eb3844b..c4ac376e9fc 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/EntityConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/IdToEntityConverter.java @@ -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; } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java index dca76faddd5..97a70fd2f4d 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java @@ -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;