From ddd414b201b2403e7d9703e6f41efe6aca5611be Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 23 Sep 2016 09:09:35 +0200 Subject: [PATCH] DATAJPA-970 - Remove explicit group name from alias detection pattern. We now use the group index instead of an explicit name. This fixes problems when using the pattern on Java 6. Original pull request: #181. --- .../data/jpa/repository/query/QueryUtils.java | 5 ++- .../repository/query/QueryUtilsUnitTests.java | 33 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index 8f177ec90..dcb8b631d 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -102,7 +102,6 @@ public abstract class QueryUtils { private static final int VARIABLE_NAME_GROUP_INDEX = 4; private static final Pattern PUNCTATION_PATTERN = Pattern.compile(".*((?![\\._])[\\p{Punct}|\\s])"); - private static final String FUNCTION_ALIAS_GROUP_NAME = "alias"; private static final Pattern FUNCTION_PATTERN; private static final String UNSAFE_PROPERTY_REFERENCE = "Sort expression '%s' must only contain property references or " @@ -142,7 +141,7 @@ public abstract class QueryUtils { builder = new StringBuilder(); builder.append("\\s+"); // at least one space builder.append("\\w+\\([0-9a-zA-z\\._,\\s']+\\)"); // any function call including parameters within the brackets - builder.append("\\s+[as|AS]+\\s+(?<" + FUNCTION_ALIAS_GROUP_NAME + ">[\\w\\.]+)"); // the potential alias + builder.append("\\s+[as|AS]+\\s+(([\\w\\.]+))"); // the potential alias FUNCTION_PATTERN = compile(builder.toString()); } @@ -308,7 +307,7 @@ public abstract class QueryUtils { while (matcher.find()) { - String alias = matcher.group(FUNCTION_ALIAS_GROUP_NAME); + String alias = matcher.group(1); if (StringUtils.hasText(alias)) { result.add(alias); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java index 47facd57c..15ffb0cb9 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java @@ -335,7 +335,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test(expected = InvalidDataAccessApiUsageException.class) public void doesNotAllowWhitespaceInSort() { @@ -345,7 +346,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixUnsageJpaSortFunctionCalls() { @@ -355,7 +357,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixMultipleAliasedFunctionCalls() { @@ -367,7 +370,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixSingleAliasedFunctionCalls() { @@ -379,7 +383,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void prefixesSingleNonAliasedFunctionCallRelatedSortProperty() { @@ -391,7 +396,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseContainesAliasedFunctionForDifferentProperty() { @@ -403,7 +409,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters() { @@ -415,7 +422,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters() { @@ -427,7 +435,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixAliasedFunctionCallNameWithUnderscores() { @@ -439,7 +448,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixAliasedFunctionCallNameWithDots() { @@ -451,7 +461,8 @@ public class QueryUtilsUnitTests { } /** - * @see DATAJPA-??? + * @see DATAJPA-965 + * @see DATAJPA-970 */ @Test public void doesNotPrefixAliasedFunctionCallNameWhenQueryStringContainsMultipleWhiteSpaces() {