generified TypeConverter interface

This commit is contained in:
Juergen Hoeller 2009-05-11 22:44:33 +00:00
parent b72af54f53
commit 0297116542
5 changed files with 28 additions and 27 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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); return convertIfNecessary(value, requiredType, null);
} }

View File

@ -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); return convertIfNecessary(value, requiredType, null);
} }
public Object convertIfNecessary( public <T> T convertIfNecessary(
Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { Object value, Class<T> requiredType, MethodParameter methodParam) throws TypeMismatchException {
try { try {
return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam); return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam);
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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#setAsText(String)
* @see java.beans.PropertyEditor#getValue() * @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). * 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#setAsText(String)
* @see java.beans.PropertyEditor#getValue() * @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; throws TypeMismatchException;
} }

View File

@ -81,7 +81,7 @@ class TypeConverterDelegate {
* @return the new value, possibly the result of type conversion * @return the new value, possibly the result of type conversion
* @throws IllegalArgumentException if type conversion failed * @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); return convertIfNecessary(null, null, newValue, requiredType, null, null);
} }
@ -95,7 +95,7 @@ class TypeConverterDelegate {
* @return the new value, possibly the result of type conversion * @return the new value, possibly the result of type conversion
* @throws IllegalArgumentException if type conversion failed * @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 { throws IllegalArgumentException {
return convertIfNecessary(null, null, newValue, requiredType, null, methodParam); return convertIfNecessary(null, null, newValue, requiredType, null, methodParam);
@ -111,8 +111,8 @@ class TypeConverterDelegate {
* @return the new value, possibly the result of type conversion * @return the new value, possibly the result of type conversion
* @throws IllegalArgumentException if type conversion failed * @throws IllegalArgumentException if type conversion failed
*/ */
public Object convertIfNecessary( public <T> T convertIfNecessary(
String propertyName, Object oldValue, Object newValue, Class requiredType) String propertyName, Object oldValue, Object newValue, Class<T> requiredType)
throws IllegalArgumentException { throws IllegalArgumentException {
return convertIfNecessary(propertyName, oldValue, newValue, requiredType, null, null); return convertIfNecessary(propertyName, oldValue, newValue, requiredType, null, null);
@ -149,8 +149,9 @@ class TypeConverterDelegate {
* @return the new value, possibly the result of type conversion * @return the new value, possibly the result of type conversion
* @throws IllegalArgumentException if type conversion failed * @throws IllegalArgumentException if type conversion failed
*/ */
protected Object convertIfNecessary( @SuppressWarnings("unchecked")
String propertyName, Object oldValue, Object newValue, Class requiredType, protected <T> T convertIfNecessary(
String propertyName, Object oldValue, Object newValue, Class<T> requiredType,
PropertyDescriptor descriptor, MethodParameter methodParam) PropertyDescriptor descriptor, MethodParameter methodParam)
throws IllegalArgumentException { throws IllegalArgumentException {
@ -173,11 +174,11 @@ class TypeConverterDelegate {
if (convertedValue != null) { if (convertedValue != null) {
if (String.class.equals(requiredType) && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) { if (String.class.equals(requiredType) && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) {
// We can stringify any primitive value... // We can stringify any primitive value...
return convertedValue.toString(); return (T) convertedValue.toString();
} }
else if (requiredType.isArray()) { else if (requiredType.isArray()) {
// Array required -> apply appropriate conversion of elements. // 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)) { else if (convertedValue instanceof Collection && CollectionFactory.isApproximableCollectionType(requiredType)) {
// Convert elements to target type, if determined. // 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 // we just want to allow special PropertyEditors to override setValue
// for type conversion from non-String values to the required type. // for type conversion from non-String values to the required type.
try { try {
Object newConvertedValue = null; Object newConvertedValue;
if (sharedEditor) { if (sharedEditor) {
// Synchronized access to shared editor instance. // Synchronized access to shared editor instance.
synchronized (editor) { synchronized (editor) {
@ -358,7 +359,7 @@ class TypeConverterDelegate {
return editor.getValue(); 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) { if (input instanceof Collection) {
// Convert Collection elements to array elements. // Convert Collection elements to array elements.
Collection coll = (Collection) input; Collection coll = (Collection) input;
@ -409,8 +410,8 @@ class TypeConverterDelegate {
return original; return original;
} }
Collection convertedCopy = null; Collection convertedCopy;
Iterator it = null; Iterator it;
try { try {
it = original.iterator(); it = original.iterator();
if (it == null) { if (it == null) {
@ -461,8 +462,8 @@ class TypeConverterDelegate {
return original; return original;
} }
Map convertedCopy = null; Map convertedCopy;
Iterator it = null; Iterator it;
try { try {
it = original.entrySet().iterator(); it = original.entrySet().iterator();
if (it == null) { if (it == null) {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -445,12 +445,12 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
return getPropertyEditorRegistry().findCustomEditor(requiredType, propertyPath); return getPropertyEditorRegistry().findCustomEditor(requiredType, propertyPath);
} }
public Object convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { public <T> T convertIfNecessary(Object value, Class<T> requiredType) throws TypeMismatchException {
return getTypeConverter().convertIfNecessary(value, requiredType); return getTypeConverter().convertIfNecessary(value, requiredType);
} }
public Object convertIfNecessary( public <T> T convertIfNecessary(
Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { Object value, Class<T> requiredType, MethodParameter methodParam) throws TypeMismatchException {
return getTypeConverter().convertIfNecessary(value, requiredType, methodParam); return getTypeConverter().convertIfNecessary(value, requiredType, methodParam);
} }