diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index b2889c4dc..251a148fa 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -47,10 +47,13 @@ public abstract class AbstractPersistentProperty

protected final PersistentEntity owner; private final SimpleTypeHolder simpleTypeHolder; - public AbstractPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { - + public AbstractPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity owner, + SimpleTypeHolder simpleTypeHolder) { + + Assert.notNull(field); Assert.notNull(simpleTypeHolder); - + Assert.notNull(owner); + this.name = field.getName(); this.rawType = field.getType(); this.information = owner.getTypeInformation().getProperty(this.name); @@ -144,7 +147,13 @@ public abstract class AbstractPersistentProperty

} public Class getComponentType() { - return isMap() || isCollection() ? information.getComponentType().getType() : null; + + if (!isMap() && !isCollection()) { + return null; + } + + TypeInformation componentType = information.getComponentType(); + return componentType == null ? null : componentType.getType(); } /* (non-Javadoc) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java index c1047eb07..af1877194 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java @@ -18,8 +18,10 @@ package org.springframework.data.util; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.util.Collection; import java.util.Map; +import org.springframework.core.GenericCollectionTypeResolver; import org.springframework.util.Assert; /** @@ -79,19 +81,18 @@ public class ClassTypeInformation extends TypeDiscoverer { return type; } - /* (non-Javadoc) - * @see org.springframework.data.util.TypeDiscoverer#getComponentType() - */ + /* + * (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#getComponentType() + */ @Override - @SuppressWarnings({"rawtypes", "unchecked"}) public TypeInformation getComponentType() { if (type.isArray()) { return createInfo(resolveArrayType(type)); } - TypeVariable[] typeParameters = type.getTypeParameters(); - return typeParameters.length > 0 ? new TypeVariableTypeInformation(typeParameters[0], this.getType(), this) : null; + return super.getComponentType(); } private static Type resolveArrayType(Class type) { diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java new file mode 100644 index 000000000..3525d9f9e --- /dev/null +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -0,0 +1,62 @@ +package org.springframework.data.mapping.model; + +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.util.TreeSet; + +import org.junit.Test; +import org.springframework.data.mapping.Association; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.util.ClassTypeInformation; +import org.springframework.util.ReflectionUtils; + +/** + * Unit tests for {@link AbstractPersistentProperty}. + * + * @author Oliver Gierke + */ +public class AbstractPersistentPropertyUnitTests { + + /** + * @see DATACMNS-68 + * @throws Exception + */ + @Test + public void discoversComponentTypeCorrectly() throws Exception { + + BasicPersistentEntity entity = new BasicPersistentEntity( + ClassTypeInformation.from(TestClassComplex.class)); + + Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet"); + + SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, new SimpleTypeHolder()); + property.getComponentType(); + } + + @SuppressWarnings("serial") + class TestClassSet extends TreeSet { + } + + class TestClassComplex { + + String id; + TestClassSet testClassSet; + } + + class SamplePersistentProperty extends AbstractPersistentProperty { + + public SamplePersistentProperty(Field field, PropertyDescriptor propertyDescriptor, + PersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { + super(field, propertyDescriptor, owner, simpleTypeHolder); + } + + public boolean isIdProperty() { + return false; + } + + @Override + protected Association createAssociation() { + return null; + } + } +}