From 3a496b1f449aa1e2e3ff202eda2b37b4fab8f41f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 10 Jun 2013 15:01:39 +0200 Subject: [PATCH] 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. --- .../data/mapping/PersistentProperty.java | 10 ++++- .../model/AbstractPersistentProperty.java | 11 ++++- .../AbstractPersistentPropertyUnitTests.java | 42 +++++++++++++++++-- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index 378937fb4..5ffd7c4d0 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -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

> { * @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(); } diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index d05ab08db..87bb00e35 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -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

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) diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index e8d6f1628..543bba92b 100644 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -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; 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 { } } + /** + * @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 SamplePersistentProperty getProperty(Class type, String name) { + + BasicPersistentEntity entity = new BasicPersistentEntity( + ClassTypeInformation.from(type)); + + Field field = ReflectionUtils.findField(type, name); + return new SamplePersistentProperty(field, null, entity, typeHolder); + } + class Generic { T genericField; @@ -228,8 +257,7 @@ public class AbstractPersistentPropertyUnitTests { } @SuppressWarnings("serial") - class TestClassSet extends TreeSet { - } + class TestClassSet extends TreeSet {} @SuppressWarnings("rawtypes") class TestClassComplex { @@ -299,4 +327,12 @@ public class AbstractPersistentPropertyUnitTests { return null; } } + + static class Sample { + + Person person; + Collection persons; + Person[] personArray; + Map personMap; + } }