Tweaked getComponentType() and added getMapValueType() to PersistentProperty.

Changed implementation of getComponentType() to use the TypeInformation instance backing the BasicPersistentProperty.
This commit is contained in:
Oliver Gierke
2011-03-17 09:24:51 +01:00
parent a07a2cc760
commit ab59c57577
3 changed files with 34 additions and 19 deletions

View File

@@ -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 <jbrisbin@vmware.com>
* @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

View File

@@ -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();
}

View File

@@ -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