DATACMNS-863 - Fixed parameter type lookup for wrapper types.
When repository parameters use wrapper types (e.g. Optional) the Parameter instance for that parameter now returns the component type.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
* Copyright 2008-2016 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.
|
||||
@@ -21,8 +21,10 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.util.QueryExecutionConverters;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -38,6 +40,7 @@ public class Parameter {
|
||||
private static final String POSITION_PARAMETER_TEMPLATE = "?%s";
|
||||
|
||||
private final MethodParameter parameter;
|
||||
private final Class<?> parameterType;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Parameter} for the given {@link MethodParameter}.
|
||||
@@ -48,6 +51,7 @@ public class Parameter {
|
||||
|
||||
Assert.notNull(parameter);
|
||||
this.parameter = parameter;
|
||||
this.parameterType = potentiallyUnwrapParameterType(parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,7 +122,7 @@ public class Parameter {
|
||||
* @return the type
|
||||
*/
|
||||
public Class<?> getType() {
|
||||
return parameter.getParameterType();
|
||||
return parameterType;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,4 +161,18 @@ public class Parameter {
|
||||
boolean isSort() {
|
||||
return Sort.class.isAssignableFrom(getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the component type if the given {@link MethodParameter} is a wrapper type.
|
||||
*
|
||||
* @param parameter must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private static Class<?> potentiallyUnwrapParameterType(MethodParameter parameter) {
|
||||
|
||||
Class<?> originalType = parameter.getParameterType();
|
||||
|
||||
return QueryExecutionConverters.supports(originalType)
|
||||
? ResolvableType.forMethodParameter(parameter).getGeneric(0).getRawClass() : originalType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2014 the original author or authors.
|
||||
* Copyright 2008-2016 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. You may obtain a copy of
|
||||
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.repository.query;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -151,6 +152,17 @@ public class ParametersUnitTests {
|
||||
assertThat(parameter.isExplicitlyNamed(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-863
|
||||
*/
|
||||
@Test
|
||||
public void unwrapsOptionals() throws Exception {
|
||||
|
||||
Parameters<?, ?> parameters = getParametersFor("methodWithOptional", Optional.class);
|
||||
|
||||
assertThat(parameters.getParameter(0).getType(), is(typeCompatibleWith(String.class)));
|
||||
}
|
||||
|
||||
private Parameters<?, ?> getParametersFor(String methodName, Class<?>... parameterTypes)
|
||||
throws SecurityException, NoSuchMethodException {
|
||||
|
||||
@@ -181,5 +193,6 @@ public class ParametersUnitTests {
|
||||
|
||||
User emptyParameters();
|
||||
|
||||
void methodWithOptional(Optional<String> optional);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user