- Revision
- 724
- Author
- mauro
- Date
- 2008-06-17 10:21:59 -0500 (Tue, 17 Jun 2008)
Log Message
WAFFLE-84: DelegatingTypeConverter returns value itself is no ValueConverter found and type is not a Class.
Modified Paths
Diff
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverter.java (723 => 724)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverter.java 2008-06-17 15:06:44 UTC (rev 723) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverter.java 2008-06-17 15:21:59 UTC (rev 724) @@ -15,9 +15,9 @@ import org.codehaus.waffle.bind.ValueConverterFinder; /** - * An implementation of Ognl's <code>TypeConverter</code> which handles Java 5 enums and will delegate - * custom <code>ValueConverter</code>'s registered per application. - * + * An implementation of Ognl's <code>TypeConverter</code> which handles Java 5 enums and will delegate custom + * <code>ValueConverter</code>'s registered per application. + * * @author Michael Ward * @author Mauro Talevi */ @@ -34,34 +34,28 @@ } /** - * <b>Comments copied from Ognl</b> - * <p/> - * Converts the given value to a given type. The OGNL context, target, member and - * name of property being set are given. This method should be able to handle - * conversion in general without any context, target, member or property name specified. - * - * @param context OGNL context under which the conversion is being done - * @param target target object in which the property is being set - * @param member member (Constructor, Method or Field) being set + * <b>Comments copied from Ognl</b> <p/> Converts the given value to a given type. The OGNL context, target, member + * and name of property being set are given. This method should be able to handle conversion in general without any + * context, target, member or property name specified. + * + * @param context OGNL context under which the conversion is being done + * @param target target object in which the property is being set + * @param member member (Constructor, Method or Field) being set * @param propertyName property name being set - * @param value value to be converted - * @param toType type to which value is converted + * @param value value to be converted + * @param toType type to which value is converted * @return Converted value Object of type toType or TypeConverter.NoConversionPossible to indicate that the * conversion was not possible. */ @SuppressWarnings("unchecked") - public Object convertValue(Map context, - Object target, - Member member, - String propertyName, - Object value, - Class toType) { - return convertValue(propertyName, (String) value, genericParameterTypeFor((Method)member)); + public Object convertValue(Map context, Object target, Member member, String propertyName, Object value, + Class toType) { + return convertValue(propertyName, (String) value, genericParameterTypeFor((Method) member)); } private Type genericParameterTypeFor(Method method) { Type[] parameterTypes = method.getGenericParameterTypes(); - if ( parameterTypes.length > 0 ){ + if (parameterTypes.length > 0) { return parameterTypes[0]; } return null; @@ -69,29 +63,31 @@ /** * Simplified entry point for Ognl use in Waffle - * + * * @param propertyName property name being set - * @param value value to be converted - * @param toType type to which value is converted + * @param value value to be converted + * @param toType type to which value is converted * @return Converted value Object of type toType or TypeConverter.NoConversionPossible to indicate that the * conversion was not possible. */ - @SuppressWarnings({"unchecked"}) + @SuppressWarnings( { "unchecked" }) public Object convertValue(String propertyName, String value, Type toType) { - if (toType instanceof Class && ((Class)toType).isEnum()) { - if (EMPTY.equals(value)) { + if (toType instanceof Class && ((Class) toType).isEnum()) { + if (EMPTY.equals(value)) { return null; } - return Enum.valueOf((Class)toType, value); + return Enum.valueOf((Class) toType, value); } ValueConverter converter = valueConverterFinder.findConverter(toType); if (converter != null) { return converter.convertValue(propertyName, value, toType); + } else if (toType instanceof Class) { + return OgnlOps.convertValue(value, (Class) toType); + } else { + return value; } - - return OgnlOps.convertValue(value, (Class)toType); } }
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java (723 => 724)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java 2008-06-17 15:06:44 UTC (rev 723) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/ognl/DelegatingTypeConverterTest.java 2008-06-17 15:21:59 UTC (rev 724) @@ -4,6 +4,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.MethodDescriptor; +import java.lang.reflect.Type; import java.util.List; import org.codehaus.waffle.bind.ValueConverter; @@ -50,7 +55,9 @@ public void canDelegateToCustomValueConverter() { // Mock ValueConverter final ValueConverter valueConverter = mockery.mock(ValueConverter.class); - final CustomType type = new CustomType(){}; + final CustomType type = new CustomType(){ + public void list(List<String> list) { + }}; mockery.checking(new Expectations() { { one(valueConverter).accept(CustomType.class); @@ -62,6 +69,24 @@ DelegatingTypeConverter converter = new DelegatingTypeConverter(new OgnlValueConverterFinder(valueConverter)); assertSame(type, converter.convertValue("propertyName", "foobar", CustomType.class)); } + + @Test + public void canReturnValueIfNotConverterFoundForTypeThatIsNotAClass() throws IntrospectionException { + DelegatingTypeConverter converter = new DelegatingTypeConverter(new OgnlValueConverterFinder()); + assertEquals("one,two", converter.convertValue("propertyName", "one,two", parameterType())); + } - private static interface CustomType {}; + private Type parameterType() throws IntrospectionException { + BeanInfo beanInfo = Introspector.getBeanInfo(CustomType.class); + for (MethodDescriptor md : beanInfo.getMethodDescriptors()) { + if (md.getMethod().getName().equals("list")) { + return md.getMethod().getGenericParameterTypes()[0]; + } + } + return null; + } + + private static interface CustomType { + void list(List<String> list); + }; }
To unsubscribe from this list please visit:
