From 5a879ffd9283b093bf11c51d61a808addebdea6d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 12 Mar 2011 11:53:05 +0100 Subject: [PATCH] =?UTF-8?q?Use=20Spring's=20ReflectionUtils.doWithFields(?= =?UTF-8?q?=E2=80=A6)=20to=20simplify=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spring already contains functionality to to something with all fields of a class including all fields from superclasses. Refactored the field collecting code to use this. --- .../data/mapping/BasicMappingContext.java | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java index 039b93dea..0cc3c9569 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java @@ -26,6 +26,8 @@ import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.data.mapping.model.*; import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; +import org.springframework.util.ReflectionUtils.FieldCallback; import org.springframework.validation.Validator; import java.beans.BeanInfo; @@ -83,32 +85,36 @@ public class BasicMappingContext implements MappingContext, InitializingBean { public PersistentEntity addPersistentEntity(Class type) { if (null == persistentEntities.get(type.getName())) { try { - PersistentEntity entity = builder.createPersistentEntity(type, this); + final PersistentEntity entity = builder.createPersistentEntity(type, this); BeanInfo info = Introspector.getBeanInfo(type); - Map descriptors = new HashMap(); + final Map descriptors = new HashMap(); for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { descriptors.put(descriptor.getName(), descriptor); } - - List fields = new LinkedList(Arrays.asList(type.getDeclaredFields())); - Class superClazz = type.getSuperclass(); - while (Object.class != superClazz) { - fields.addAll(new LinkedList(Arrays.asList(superClazz.getDeclaredFields()))); - superClazz = superClazz.getSuperclass(); - } - for (Field field : fields) { - PropertyDescriptor descriptor = descriptors.get(field.getName()); - if (builder.isPersistentProperty(field, descriptor)) { - PersistentProperty property = builder.createPersistentProperty(field, descriptor); - property.setOwner(entity); - entity.addPersistentProperty(property); - if (builder.isAssociation(field, descriptor)) { - Association association = builder.createAssociation(property); - entity.addAssociation(association); + + ReflectionUtils.doWithFields(type, new FieldCallback() { + + @Override + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { + + try { + PropertyDescriptor descriptor = descriptors.get(field.getName()); + if (builder.isPersistentProperty(field, descriptor)) { + PersistentProperty property = builder.createPersistentProperty(field, descriptor); + property.setOwner(entity); + entity.addPersistentProperty(property); + if (builder.isAssociation(field, descriptor)) { + Association association = builder.createAssociation(property); + entity.addAssociation(association); + } + } + } catch (MappingConfigurationException e) { + log.error(e.getMessage(), e); } } - } + }); + entity.setIdProperty(builder.getIdProperty(type)); entity.setPreferredConstructor(builder.getPreferredConstructor(type));