From 0297116542a3ce33d19a6a81b8240097081376c5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 11 May 2009 22:44:33 +0000 Subject: [PATCH] generified TypeConverter interface --- .../beans/AbstractPropertyAccessor.java | 4 +-- .../beans/SimpleTypeConverter.java | 6 ++-- .../springframework/beans/TypeConverter.java | 6 ++-- .../beans/TypeConverterDelegate.java | 31 ++++++++++--------- .../validation/DataBinder.java | 8 ++--- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java b/org.springframework.beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java index 2df53e11b02..4a2f380d81d 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java @@ -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 convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { return convertIfNecessary(value, requiredType, null); } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/SimpleTypeConverter.java b/org.springframework.beans/src/main/java/org/springframework/beans/SimpleTypeConverter.java index 3747c081f25..a24d119a453 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/SimpleTypeConverter.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/SimpleTypeConverter.java @@ -37,12 +37,12 @@ public class SimpleTypeConverter extends PropertyEditorRegistrySupport implement } - public Object convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { + public T convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { return convertIfNecessary(value, requiredType, null); } - public Object convertIfNecessary( - Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { + public T convertIfNecessary( + Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { try { return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam); } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverter.java b/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverter.java index 1a38dde532f..8aa36cf7f81 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverter.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverter.java @@ -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 convertIfNecessary(Object value, Class 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 convertIfNecessary(Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException; } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java b/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java index 44e401026e4..948e2511546 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java @@ -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 convertIfNecessary(Object newValue, Class 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 convertIfNecessary(Object newValue, Class 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 convertIfNecessary( + String propertyName, Object oldValue, Object newValue, Class 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 convertIfNecessary( + String propertyName, Object oldValue, Object newValue, Class 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) { diff --git a/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java b/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java index 105a16425ed..149ef53d65d 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/DataBinder.java @@ -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. @@ -445,12 +445,12 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { return getPropertyEditorRegistry().findCustomEditor(requiredType, propertyPath); } - public Object convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { + public T convertIfNecessary(Object value, Class requiredType) throws TypeMismatchException { return getTypeConverter().convertIfNecessary(value, requiredType); } - public Object convertIfNecessary( - Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { + public T convertIfNecessary( + Object value, Class requiredType, MethodParameter methodParam) throws TypeMismatchException { return getTypeConverter().convertIfNecessary(value, requiredType, methodParam); }