Skip to content

Commit 6616f6e

Browse files
committed
DATACMNS-1827 - Polishing.
Extract qualifier lookup into SpringDataAnnotationUtils. Original pull request: #474.
1 parent 438b01e commit 6616f6e

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/main/java/org/springframework/data/web/PageableHandlerMethodArgumentResolverSupport.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ protected String getParameterNameToUse(String source, @Nullable MethodParameter
231231

232232
StringBuilder builder = new StringBuilder(prefix);
233233

234-
String value = getQualifier(parameter);
234+
String value = SpringDataAnnotationUtils.getQualifier(parameter);
235235

236236
if (StringUtils.hasLength(value)) {
237237
builder.append(value);
@@ -292,16 +292,5 @@ private Optional<Integer> parseAndApplyBoundaries(@Nullable String parameter, in
292292
}
293293
}
294294

295-
@Nullable
296-
private static String getQualifier(@Nullable MethodParameter parameter) {
297295

298-
if (parameter == null) {
299-
return null;
300-
}
301-
302-
MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
303-
MergedAnnotation<Qualifier> qualifier = annotations.get(Qualifier.class);
304-
305-
return qualifier.isPresent() ? qualifier.getString("value") : null;
306-
}
307296
}

src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolverSupport.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected String getSortParameter(@Nullable MethodParameter parameter) {
186186

187187
StringBuilder builder = new StringBuilder();
188188

189-
String value = getQualifier(parameter);
189+
String value = SpringDataAnnotationUtils.getQualifier(parameter);
190190

191191
if (StringUtils.hasLength(value)) {
192192
builder.append(value);
@@ -295,19 +295,6 @@ static boolean notOnlyDots(String source) {
295295
return StringUtils.hasText(source.replace(".", ""));
296296
}
297297

298-
@Nullable
299-
private static String getQualifier(@Nullable MethodParameter parameter) {
300-
301-
if (parameter == null) {
302-
return null;
303-
}
304-
305-
MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
306-
MergedAnnotation<Qualifier> qualifier = annotations.get(Qualifier.class);
307-
308-
return qualifier.isPresent() ? qualifier.getString("value") : null;
309-
}
310-
311298
/**
312299
* Helper to easily build request parameter expressions for {@link Sort} instances.
313300
*

src/main/java/org/springframework/data/web/SpringDataAnnotationUtils.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@
2323
import org.springframework.beans.factory.annotation.Qualifier;
2424
import org.springframework.core.MethodParameter;
2525
import org.springframework.core.annotation.AnnotationUtils;
26+
import org.springframework.core.annotation.MergedAnnotation;
27+
import org.springframework.core.annotation.MergedAnnotations;
2628
import org.springframework.data.domain.Pageable;
2729
import org.springframework.lang.Nullable;
2830
import org.springframework.util.ObjectUtils;
2931

3032
/**
31-
* Helper class to ease sharing code between legacy {@link PageableArgumentResolver} and
32-
* {@link PageableHandlerMethodArgumentResolver}.
33+
* Helper class to ease sharing code between legacy {@link PageableHandlerMethodArgumentResolverSupport} and
34+
* {@link SortHandlerMethodArgumentResolverSupport}.
3335
*
3436
* @author Oliver Gierke
37+
* @author Mark Paluch
3538
*/
3639
abstract class SpringDataAnnotationUtils {
3740

@@ -105,6 +108,26 @@ public static <T> T getSpecificPropertyOrDefaultFromValue(Annotation annotation,
105108
return (T) result;
106109
}
107110

111+
/**
112+
* Determine a qualifier value for a {@link MethodParameter}.
113+
*
114+
* @param parameter must not be {@literal null}.
115+
* @return the qualifier value if {@code @Qualifier} is present.
116+
* @since 2.5
117+
*/
118+
@Nullable
119+
public static String getQualifier(@Nullable MethodParameter parameter) {
120+
121+
if (parameter == null) {
122+
return null;
123+
}
124+
125+
MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
126+
MergedAnnotation<Qualifier> qualifier = annotations.get(Qualifier.class);
127+
128+
return qualifier.isPresent() ? qualifier.getString("value") : null;
129+
}
130+
108131
/**
109132
* Asserts that every {@link Pageable} parameter of the given parameters carries an {@link Qualifier} annotation to
110133
* distinguish them from each other.
@@ -154,4 +177,5 @@ private static Qualifier findAnnotation(Annotation[] annotations) {
154177

155178
return null;
156179
}
180+
157181
}

0 commit comments

Comments
 (0)