Browse Source

DATACMNS-337 - PersistentProperty now exposes getActualType().

The call transparently resolves map value types and collection component types if the property is either one of them.
pull/30/merge
Oliver Gierke 13 years ago
parent
commit
3a496b1f44
  1. 10
      src/main/java/org/springframework/data/mapping/PersistentProperty.java
  2. 11
      src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java
  3. 42
      src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

10
src/main/java/org/springframework/data/mapping/PersistentProperty.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -171,4 +171,12 @@ public interface PersistentProperty<P extends PersistentProperty<P>> { @@ -171,4 +171,12 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
* @return the map's value type or {@literal null} if no {@link java.util.Map}
*/
Class<?> getMapValueType();
/**
* Returns the actual type of the property. This will be the original property type if no generics were used, the
* component type for collection-like types and arrays as well as the value type for map properties.
*
* @return
*/
Class<?> getActualType();
}

11
src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -282,6 +282,15 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -282,6 +282,15 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
return isMap() ? information.getMapValueType().getType() : null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentProperty#getActualType()
*/
@Override
public Class<?> getActualType() {
return information.getActualType().getType();
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)

42
src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import org.junit.Test; @@ -32,6 +32,7 @@ import org.junit.Test;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.Person;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.ReflectionUtils;
@ -214,6 +215,34 @@ public class AbstractPersistentPropertyUnitTests { @@ -214,6 +215,34 @@ public class AbstractPersistentPropertyUnitTests {
}
}
/**
* @see DATACMNS-337
*/
@Test
public void resolvesActualType() {
SamplePersistentProperty property = getProperty(Sample.class, "person");
assertThat(property.getActualType(), is((Object) Person.class));
property = getProperty(Sample.class, "persons");
assertThat(property.getActualType(), is((Object) Person.class));
property = getProperty(Sample.class, "personArray");
assertThat(property.getActualType(), is((Object) Person.class));
property = getProperty(Sample.class, "personMap");
assertThat(property.getActualType(), is((Object) Person.class));
}
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
ClassTypeInformation.from(type));
Field field = ReflectionUtils.findField(type, name);
return new SamplePersistentProperty(field, null, entity, typeHolder);
}
class Generic<T> {
T genericField;
@ -228,8 +257,7 @@ public class AbstractPersistentPropertyUnitTests { @@ -228,8 +257,7 @@ public class AbstractPersistentPropertyUnitTests {
}
@SuppressWarnings("serial")
class TestClassSet extends TreeSet<Object> {
}
class TestClassSet extends TreeSet<Object> {}
@SuppressWarnings("rawtypes")
class TestClassComplex {
@ -299,4 +327,12 @@ public class AbstractPersistentPropertyUnitTests { @@ -299,4 +327,12 @@ public class AbstractPersistentPropertyUnitTests {
return null;
}
}
static class Sample {
Person person;
Collection<Person> persons;
Person[] personArray;
Map<String, Person> personMap;
}
}

Loading…
Cancel
Save