diff --git a/src/main/java/org/springframework/data/repository/query/Parameter.java b/src/main/java/org/springframework/data/repository/query/Parameter.java index 9c9f3df6e..d96182749 100644 --- a/src/main/java/org/springframework/data/repository/query/Parameter.java +++ b/src/main/java/org/springframework/data/repository/query/Parameter.java @@ -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; + } } diff --git a/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java b/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java index be7e21df4..1eb03b30f 100644 --- a/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java @@ -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 optional); } }