diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentProperty.java index 1db3f0e3f..7f973ef83 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentProperty.java @@ -28,14 +28,14 @@ import org.springframework.data.util.TypeInformation; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; import java.util.Collection; -import java.util.List; import java.util.Map; /** + * Simple impementation of {@link PersistentProperty}. + * * @author Jon Brisbin + * @author Oliver Gierke */ public class BasicPersistentProperty implements PersistentProperty { @@ -147,7 +147,7 @@ public class BasicPersistentProperty implements PersistentProperty { @Override public boolean isComplexType() { - if (isCollection() || isArray()) { + if (isCollection() || isArray()) { return !MappingBeanHelper.isSimpleType(getComponentType()); } else { return !MappingBeanHelper.isSimpleType(getType()); @@ -160,19 +160,16 @@ public class BasicPersistentProperty implements PersistentProperty { } @Override - public Class getComponentType() { - if (isCollection()) { - Type genericType = field.getGenericType(); - if (genericType instanceof ParameterizedType) { - Type[] genericTypes = ((ParameterizedType) genericType).getActualTypeArguments(); - for (Type t : genericTypes) { - if (t instanceof Class) { - return (Class) t; - } - } - } - } - return getType().getComponentType(); + public Class getComponentType() { + return isMap() || isCollection() ? information.getComponentType().getType() : null; + } + + /* (non-Javadoc) + * @see org.springframework.data.mapping.model.PersistentProperty#getMapValueType() + */ + @Override + public Class getMapValueType() { + return isMap() ? information.getMapValueType().getType() : null; } @Override diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentProperty.java index 55f876acf..c0c34b116 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentProperty.java @@ -5,10 +5,12 @@ import org.springframework.data.util.TypeInformation; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Map; /** * @author Graeme Rocher - * @since 1.0 + * @author Oliver Gierke */ public interface PersistentProperty { @@ -62,7 +64,20 @@ public interface PersistentProperty { */ boolean isEntity(); + /** + * Returns the component type of the type if it is a {@link Collection}. Will return the type of the key if the + * property is a {@link Map}. + * + * @return the component type, the map's key type or {@literal null} if neither {@link Collection} nor {@link Map}. + */ Class getComponentType(); + + /** + * Returns the type of the values if the property is a {@link Map}. + * + * @return the map's value type or {@literal null} if no {@link Map} + */ + Class getMapValueType(); boolean isIdProperty(); } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index d1dee4c09..de3817fb5 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -22,7 +22,10 @@ public class ClassTypeInformationUnitTests { TypeInformation discoverer = new ClassTypeInformation( ConcreteType.class); assertEquals(ConcreteType.class, discoverer.getType()); - assertEquals(String.class, discoverer.getProperty("content").getType()); + TypeInformation content = discoverer.getProperty("content"); + assertEquals(String.class, content.getType()); + assertNull(content.getComponentType()); + assertNull(content.getMapValueType()); } @Test