From 6ddcad6998b2ef8ac51319fba2c02cb7a772a83d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 17 Jun 2011 19:39:24 +0200 Subject: [PATCH] Property instances can now be created from a dot notation as well. Some JavaDoc polishing. Removed private method to lookup actual property type (read transparently resolving collection component and map value types) over TypeInformation.getActualType(). --- .../repository/query/parser/Property.java | 29 +++++++------------ .../query/parser/PropertyUnitTests.java | 5 ++++ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java index 8af6a6866..8d1c885b1 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java @@ -34,7 +34,8 @@ import org.springframework.util.StringUtils; */ public class Property { - private static final Pattern SPLITTER = Pattern.compile("(?:_?(_*?[^_]+))"); + private static final String DELIMITERS = "_\\."; + private static final Pattern SPLITTER = Pattern.compile("(?:[%s]?([%s]*?[^%s]+))".replaceAll("%s", DELIMITERS)); private static final String ERROR_TEMPLATE = "No property %s found for type %s"; private final String name; @@ -45,7 +46,7 @@ public class Property { /** - * Creates a leaf property (no nested ones) with the given name inside the + * Creates a leaf {@link Property} (no nested ones) with the given name inside the * given owning type. * * @param name @@ -56,6 +57,12 @@ public class Property { this(name, ClassTypeInformation.from(owningType)); } + /** + * Creates a leaf {@link Property} (no nested ones with the given name and owning type. + * + * @param name + * @param owningType + */ Property(String name, TypeInformation owningType) { Assert.hasText(name); @@ -70,24 +77,10 @@ public class Property { } this.isCollection = type.isCollectionLike(); - this.type = getPropertyType(type); + this.type = type.getActualType(); this.name = propertyName; } - private TypeInformation getPropertyType(TypeInformation type) { - - if (type.isCollectionLike()) { - return type.getComponentType(); - } - - if (type.isMap()) { - return type.getMapValueType(); - } - - return type; - } - - /** * Creates a {@link Property} with the given name inside the given owning * type and tries to resolve the other {@link String} to create nested @@ -110,7 +103,7 @@ public class Property { /** * Returns the name of the {@link Property}. * - * @return the name + * @return the name will never be {@literal null}. */ public String getName() { diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java index 69781b082..f57ba2ebc 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java @@ -144,6 +144,11 @@ public class PropertyUnitTests { assertThat(property.toDotPath(), is("_foo._email")); } + @Test + public void supportsDotNotationAsWell() { + Property.from("bar.userMap.name", Sample.class); + } + private class Foo { String userName;