DATACMNS-700 - ReflectionRepositoryInvoker now captures context for failed parameter conversions.

Introduced QueryMethodParameterConversionException to capture the context of a failed ConversionException to allow better error reporting.

Cleaned up test cases to not use deprecated API anymore.
This commit is contained in:
Oliver Gierke
2015-05-22 09:24:23 +02:00
parent 1fb7466065
commit 501f0c8493
3 changed files with 119 additions and 9 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2015 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 the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.support;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionException;
import org.springframework.util.Assert;
/**
* Exception to represent a failed attempt to convert a source value into a query method parameter.
*
* @author Oliver Gierke
* @soundtrack Dave Matthews Band - The Dreaming Tree (DMB 2009 Europe)
* @since 1.11
*/
public class QueryMethodParameterConversionException extends RuntimeException {
private static final long serialVersionUID = -5818002272039533066L;
private final Object source;
private final MethodParameter parameter;
/**
* @param source can be {@literal null}.
* @param parameter the {@link MethodParameter} the value should've been converted for, must not be {@literal null}..
* @param cause the original {@link ConversionException}, must not be {@literal null}.
*/
public QueryMethodParameterConversionException(Object source, MethodParameter parameter, ConversionException cause) {
super("message", cause);
Assert.notNull(parameter, "Method parameter must not be null!");
Assert.notNull(cause, "ConversionException must not be null!");
this.parameter = parameter;
this.source = source;
}
/**
* Returns the source value that we failed converting.
*
* @return the source can be {@literal null}.
*/
public Object getSource() {
return source;
}
/**
* Returns the {@link MethodParameter} we tried to convert the source value for.
*
* @return the parameter will never be {@literal null}.
* @see #getSource()
*/
public MethodParameter getParameter() {
return parameter;
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Map;
import java.util.Map.Entry;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.domain.Pageable;
@@ -234,14 +235,22 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
Object value = unwrapSingleElement(rawParameters.get(parameterName));
result[i] = targetType.isInstance(value) ? value : conversionService.convert(value,
TypeDescriptor.forObject(value), new TypeDescriptor(param));
result[i] = targetType.isInstance(value) ? value : convert(value, param);
}
}
return result;
}
private Object convert(Object value, MethodParameter parameter) {
try {
return conversionService.convert(value, TypeDescriptor.forObject(value), new TypeDescriptor(parameter));
} catch (ConversionException o_O) {
throw new QueryMethodParameterConversionException(value, parameter, o_O);
}
}
/**
* Invokes the given method with the given arguments on the backing repository.
*