generified TypeConverter interface
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -103,7 +103,7 @@ public abstract class AbstractPropertyAccessor extends PropertyEditorRegistrySup
|
||||
}
|
||||
}
|
||||
|
||||
public Object convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException {
|
||||
public <T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException {
|
||||
return convertIfNecessary(value, requiredType, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,12 +37,12 @@ public class SimpleTypeConverter extends PropertyEditorRegistrySupport implement
|
||||
}
|
||||
|
||||
|
||||
public Object convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException {
|
||||
public <T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException {
|
||||
return convertIfNecessary(value, requiredType, null);
|
||||
}
|
||||
|
||||
public Object convertIfNecessary(
|
||||
Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException {
|
||||
public <T> T convertIfNecessary(
|
||||
Object value, Class<T> requiredType, MethodParameter methodParam) throws TypeMismatchException {
|
||||
try {
|
||||
return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -44,7 +44,7 @@ public interface TypeConverter {
|
||||
* @see java.beans.PropertyEditor#setAsText(String)
|
||||
* @see java.beans.PropertyEditor#getValue()
|
||||
*/
|
||||
Object convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException;
|
||||
<T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException;
|
||||
|
||||
/**
|
||||
* Convert the value to the required type (if necessary from a String).
|
||||
@@ -62,7 +62,7 @@ public interface TypeConverter {
|
||||
* @see java.beans.PropertyEditor#setAsText(String)
|
||||
* @see java.beans.PropertyEditor#getValue()
|
||||
*/
|
||||
Object convertIfNecessary(Object value, Class requiredType, MethodParameter methodParam)
|
||||
<T> T convertIfNecessary(Object value, Class<T> requiredType, MethodParameter methodParam)
|
||||
throws TypeMismatchException;
|
||||
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ class TypeConverterDelegate {
|
||||
* @return the new value, possibly the result of type conversion
|
||||
* @throws IllegalArgumentException if type conversion failed
|
||||
*/
|
||||
public Object convertIfNecessary(Object newValue, Class requiredType) throws IllegalArgumentException {
|
||||
public <T> T convertIfNecessary(Object newValue, Class<T> requiredType) throws IllegalArgumentException {
|
||||
return convertIfNecessary(null, null, newValue, requiredType, null, null);
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ class TypeConverterDelegate {
|
||||
* @return the new value, possibly the result of type conversion
|
||||
* @throws IllegalArgumentException if type conversion failed
|
||||
*/
|
||||
public Object convertIfNecessary(Object newValue, Class requiredType, MethodParameter methodParam)
|
||||
public <T> T convertIfNecessary(Object newValue, Class<T> requiredType, MethodParameter methodParam)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
return convertIfNecessary(null, null, newValue, requiredType, null, methodParam);
|
||||
@@ -111,8 +111,8 @@ class TypeConverterDelegate {
|
||||
* @return the new value, possibly the result of type conversion
|
||||
* @throws IllegalArgumentException if type conversion failed
|
||||
*/
|
||||
public Object convertIfNecessary(
|
||||
String propertyName, Object oldValue, Object newValue, Class requiredType)
|
||||
public <T> T convertIfNecessary(
|
||||
String propertyName, Object oldValue, Object newValue, Class<T> requiredType)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
return convertIfNecessary(propertyName, oldValue, newValue, requiredType, null, null);
|
||||
@@ -149,8 +149,9 @@ class TypeConverterDelegate {
|
||||
* @return the new value, possibly the result of type conversion
|
||||
* @throws IllegalArgumentException if type conversion failed
|
||||
*/
|
||||
protected Object convertIfNecessary(
|
||||
String propertyName, Object oldValue, Object newValue, Class requiredType,
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T> T convertIfNecessary(
|
||||
String propertyName, Object oldValue, Object newValue, Class<T> requiredType,
|
||||
PropertyDescriptor descriptor, MethodParameter methodParam)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
@@ -173,11 +174,11 @@ class TypeConverterDelegate {
|
||||
if (convertedValue != null) {
|
||||
if (String.class.equals(requiredType) && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) {
|
||||
// We can stringify any primitive value...
|
||||
return convertedValue.toString();
|
||||
return (T) convertedValue.toString();
|
||||
}
|
||||
else if (requiredType.isArray()) {
|
||||
// Array required -> apply appropriate conversion of elements.
|
||||
return convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType());
|
||||
return (T) convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType());
|
||||
}
|
||||
else if (convertedValue instanceof Collection && CollectionFactory.isApproximableCollectionType(requiredType)) {
|
||||
// Convert elements to target type, if determined.
|
||||
@@ -228,7 +229,7 @@ class TypeConverterDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
return convertedValue;
|
||||
return (T) convertedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -278,7 +279,7 @@ class TypeConverterDelegate {
|
||||
// we just want to allow special PropertyEditors to override setValue
|
||||
// for type conversion from non-String values to the required type.
|
||||
try {
|
||||
Object newConvertedValue = null;
|
||||
Object newConvertedValue;
|
||||
if (sharedEditor) {
|
||||
// Synchronized access to shared editor instance.
|
||||
synchronized (editor) {
|
||||
@@ -358,7 +359,7 @@ class TypeConverterDelegate {
|
||||
return editor.getValue();
|
||||
}
|
||||
|
||||
protected Object convertToTypedArray(Object input, String propertyName, Class componentType) {
|
||||
protected Object convertToTypedArray(Object input, String propertyName, Class<?> componentType) {
|
||||
if (input instanceof Collection) {
|
||||
// Convert Collection elements to array elements.
|
||||
Collection coll = (Collection) input;
|
||||
@@ -409,8 +410,8 @@ class TypeConverterDelegate {
|
||||
return original;
|
||||
}
|
||||
|
||||
Collection convertedCopy = null;
|
||||
Iterator it = null;
|
||||
Collection convertedCopy;
|
||||
Iterator it;
|
||||
try {
|
||||
it = original.iterator();
|
||||
if (it == null) {
|
||||
@@ -461,8 +462,8 @@ class TypeConverterDelegate {
|
||||
return original;
|
||||
}
|
||||
|
||||
Map convertedCopy = null;
|
||||
Iterator it = null;
|
||||
Map convertedCopy;
|
||||
Iterator it;
|
||||
try {
|
||||
it = original.entrySet().iterator();
|
||||
if (it == null) {
|
||||
|
||||
Reference in New Issue
Block a user