removed typed value

This commit is contained in:
Keith Donald 2009-03-31 16:51:09 +00:00
parent d38c0d301c
commit 199c9bb9c5
4 changed files with 67 additions and 160 deletions

View File

@ -21,8 +21,6 @@ package org.springframework.core.convert;
* this system. Call one of the <i>getConversionExecutor</i> operations to obtain * this system. Call one of the <i>getConversionExecutor</i> operations to obtain
* a thread-safe {@link ConversionExecutor} command for later use. * a thread-safe {@link ConversionExecutor} command for later use.
* *
* TODO - is TypeDescriptor/TypedValue needed on source?
*
* @author Keith Donald * @author Keith Donald
*/ */
public interface ConversionService { public interface ConversionService {
@ -33,10 +31,10 @@ public interface ConversionService {
* @param targetType the target type to convert to * @param targetType the target type to convert to
* @return true if a conversion can be performed, false if not * @return true if a conversion can be performed, false if not
*/ */
public boolean canConvert(TypedValue source, TypeDescriptor targetType); public boolean canConvert(Object source, TypeDescriptor targetType);
/** /**
* Convert the source to target type T. * Convert the source to targetType.
* @param source the source to convert from (may be null) * @param source the source to convert from (may be null)
* @param targetType the target type to convert to * @param targetType the target type to convert to
* @return the converted object, an instance of the <code>targetType</code>, or <code>null</code> if a null source * @return the converted object, an instance of the <code>targetType</code>, or <code>null</code> if a null source
@ -45,11 +43,11 @@ public interface ConversionService {
* source to an instance of targetType * source to an instance of targetType
* @throws ConversionException if an exception occurred during the conversion process * @throws ConversionException if an exception occurred during the conversion process
*/ */
public Object executeConversion(TypedValue source, TypeDescriptor targetType) throws ConversionExecutorNotFoundException, public Object executeConversion(Object source, TypeDescriptor targetType) throws ConversionExecutorNotFoundException,
ConversionException; ConversionException;
/** /**
* Convert the source to target type T with a custom converter. * Convert the source to targetType using a custom converter.
* @param converterId the id of the custom converter, which must be registered with this conversion service and * @param converterId the id of the custom converter, which must be registered with this conversion service and
* capable of converting to the targetType * capable of converting to the targetType
* @param source the source to convert from (may be null) * @param source the source to convert from (may be null)
@ -60,22 +58,22 @@ public interface ConversionService {
* source to an instance of targetType * source to an instance of targetType
* @throws ConversionException if an exception occurred during the conversion process * @throws ConversionException if an exception occurred during the conversion process
*/ */
public Object executeConversion(String converterId, TypedValue source, TypeDescriptor targetType) public Object executeConversion(String converterId, Object source, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException, ConversionException; throws ConversionExecutorNotFoundException, ConversionException;
/** /**
* Get a ConversionExecutor that converts objects from S to T. * Get a ConversionExecutor that converts objects from sourceType to targetType.
* The returned ConversionExecutor is thread-safe and may safely be cached for later use by client code. * The returned ConversionExecutor is thread-safe and may safely be cached for later use by client code.
* @param sourceType the source type to convert from (required) * @param sourceType the source type to convert from (required)
* @param targetType the target type to convert to (required) * @param targetType the target type to convert to (required)
* @return the executor that can execute instance type conversion, never null * @return the executor that can execute instance type conversion, never null
* @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found * @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found
*/ */
public ConversionExecutor getConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType) public ConversionExecutor getConversionExecutor(Class<?> sourceType, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException; throws ConversionExecutorNotFoundException;
/** /**
* Get a ConversionExecutor that that converts objects from S to T with a custom converter. * Get a ConversionExecutor that converts objects from from sourceType to targetType using a custom converter.
* The returned ConversionExecutor is thread-safe and may safely be cached for use in client code. * The returned ConversionExecutor is thread-safe and may safely be cached for use in client code.
* @param converterId the id of the custom converter, which must be registered with this conversion service and * @param converterId the id of the custom converter, which must be registered with this conversion service and
* capable of converting from sourceType to targetType (required) * capable of converting from sourceType to targetType (required)
@ -84,7 +82,7 @@ public interface ConversionService {
* @return the executor that can execute instance type conversion, never null * @return the executor that can execute instance type conversion, never null
* @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found * @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found
*/ */
public ConversionExecutor getConversionExecutor(String converterId, TypeDescriptor sourceType, public ConversionExecutor getConversionExecutor(String converterId, Class<?> sourceType,
TypeDescriptor targetType) throws ConversionExecutorNotFoundException; TypeDescriptor targetType) throws ConversionExecutorNotFoundException;
/** /**

View File

@ -1,84 +0,0 @@
/*
* Copyright 2004-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.
* 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.core.convert;
/**
* A value with additional context about its type.
* The additional context provides information about how the value was obtained; for example from a field read or getter return value.
* This additional context allows access to generic information associated with a field, return value, or method argument.
* It also allows access to field-level or method-level annotations.
* All of this context can be utilized when performing a type conversion as part of a data binding routine.
*
* TODO - is this needed?
*
* @author Keith Donald
*/
public class TypedValue {
/**
* The NULL TypedValue object.
*/
public static final TypedValue NULL = new TypedValue(null);
private final Object value;
private final TypeDescriptor typeDescriptor;
/**
* Creates a new typed value.
* @param value the actual value (may be null)
*/
public TypedValue(Object value) {
this.value = value;
if (this.value != null) {
typeDescriptor = TypeDescriptor.valueOf(value.getClass());
} else {
typeDescriptor = null;
}
}
/**
* Creates a new typed value.
* @param value the actual value (may be null)
* @param typeDescriptor the value type descriptor (may be null)
*/
public TypedValue(Object value, TypeDescriptor typeDescriptor) {
this.value = value;
this.typeDescriptor = typeDescriptor;
}
/**
* The actual value. May be null.
*/
public Object getValue() {
return value;
}
/**
* Provides additional type context about the value. May be null.
*/
public TypeDescriptor getTypeDescriptor() {
return typeDescriptor;
}
/**
* True if both the actual value and type descriptor are null.
*/
public boolean isNull() {
return value == null && typeDescriptor == null;
}
}

View File

@ -31,7 +31,6 @@ import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.ConversionExecutorNotFoundException; import org.springframework.core.convert.ConversionExecutorNotFoundException;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.TypedValue;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterInfo; import org.springframework.core.convert.converter.ConverterInfo;
import org.springframework.core.convert.converter.SuperConverter; import org.springframework.core.convert.converter.SuperConverter;
@ -155,33 +154,32 @@ public class GenericConversionService implements ConversionService {
// implementing ConversionService // implementing ConversionService
public boolean canConvert(TypedValue source, TypeDescriptor targetType) { public boolean canConvert(Object source, TypeDescriptor targetType) {
return false; return false;
} }
public Object executeConversion(TypedValue source, TypeDescriptor targetType) public Object executeConversion(Object source, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException, ConversionException { throws ConversionExecutorNotFoundException, ConversionException {
Assert.notNull(source, "The source to convert from is required"); if (source == null) {
if (source.isNull()) {
return null; return null;
} }
return getConversionExecutor(source.getTypeDescriptor(), targetType).execute(source.getValue()); return getConversionExecutor(source.getClass(), targetType).execute(source);
} }
public Object executeConversion(String converterId, TypedValue source, TypeDescriptor targetType) public Object executeConversion(String converterId, Object source, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException, ConversionException { throws ConversionExecutorNotFoundException, ConversionException {
Assert.notNull(source, "The source to convert from is required"); if (source == null) {
if (source.isNull()) {
return null; return null;
} }
return getConversionExecutor(converterId, source.getTypeDescriptor(), targetType).execute(source.getValue()); return getConversionExecutor(converterId, source.getClass(), targetType).execute(source);
} }
public ConversionExecutor getConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType) public ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException { throws ConversionExecutorNotFoundException {
Assert.notNull(sourceType, "The sourceType to convert from is required"); Assert.notNull(sourceClass, "The sourceType to convert from is required");
Assert.notNull(targetType, "The targetType to convert to is required"); Assert.notNull(targetType, "The targetType to convert to is required");
// special handling for arrays since they are not indexable classes // special handling for arrays since they are not indexable classes
TypeDescriptor sourceType = TypeDescriptor.valueOf(sourceClass);
if (sourceType.isArray()) { if (sourceType.isArray()) {
if (targetType.isArray()) { if (targetType.isArray()) {
return new ArrayToArray(sourceType, targetType, this); return new ArrayToArray(sourceType, targetType, this);
@ -217,7 +215,7 @@ public class GenericConversionService implements ConversionService {
return new StaticSuperConversionExecutor(sourceType, targetType, superConverter); return new StaticSuperConversionExecutor(sourceType, targetType, superConverter);
} }
if (parent != null) { if (parent != null) {
return parent.getConversionExecutor(sourceType, targetType); return parent.getConversionExecutor(sourceClass, targetType);
} else { } else {
if (sourceType.isAssignableTo(targetType)) { if (sourceType.isAssignableTo(targetType)) {
return new StaticConversionExecutor(sourceType, targetType, NoOpConverter.INSTANCE); return new StaticConversionExecutor(sourceType, targetType, NoOpConverter.INSTANCE);
@ -229,7 +227,7 @@ public class GenericConversionService implements ConversionService {
} }
} }
public ConversionExecutor getConversionExecutor(String converterId, TypeDescriptor sourceType, public ConversionExecutor getConversionExecutor(String converterId, Class sourceType,
TypeDescriptor targetType) throws ConversionExecutorNotFoundException { TypeDescriptor targetType) throws ConversionExecutorNotFoundException {
throw new UnsupportedOperationException("Not yet implemented"); throw new UnsupportedOperationException("Not yet implemented");
} }
@ -450,7 +448,7 @@ public class GenericConversionService implements ConversionService {
} }
public ConversionExecutor getElementConverter(Class<?> sourceElementType, Class<?> targetElementType) { public ConversionExecutor getElementConverter(Class<?> sourceElementType, Class<?> targetElementType) {
return getConversionExecutor(TypeDescriptor.valueOf(sourceElementType), TypeDescriptor return getConversionExecutor(sourceElementType, TypeDescriptor
.valueOf(targetElementType)); .valueOf(targetElementType));
} }

View File

@ -31,7 +31,6 @@ import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor; import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.ConversionExecutorNotFoundException; import org.springframework.core.convert.ConversionExecutorNotFoundException;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.TypedValue;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.NumberToNumber; import org.springframework.core.convert.converter.NumberToNumber;
import org.springframework.core.convert.converter.StringToEnum; import org.springframework.core.convert.converter.StringToEnum;
@ -43,30 +42,30 @@ public class GenericConversionServiceTests extends TestCase {
public void testExecuteConversion() { public void testExecuteConversion() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
assertEquals(new Integer(3), service.executeConversion(value("3"), type(Integer.class))); assertEquals(new Integer(3), service.executeConversion("3", type(Integer.class)));
} }
public void testExecuteConversionNullSource() { public void testExecuteConversionNullSource() {
assertEquals(null, service.executeConversion(TypedValue.NULL, type(Integer.class))); assertEquals(null, service.executeConversion(null, type(Integer.class)));
} }
public void testConverterConversionForwardIndex() { public void testConverterConversionForwardIndex() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(Integer.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
Integer three = (Integer) executor.execute("3"); Integer three = (Integer) executor.execute("3");
assertEquals(3, three.intValue()); assertEquals(3, three.intValue());
} }
public void testConverterConversionReverseIndex() { public void testConverterConversionReverseIndex() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(Integer.class), type(String.class)); ConversionExecutor executor = service.getConversionExecutor(Integer.class, type(String.class));
String threeString = (String) executor.execute(new Integer(3)); String threeString = (String) executor.execute(new Integer(3));
assertEquals("3", threeString); assertEquals("3", threeString);
} }
public void testConversionExecutorNotFound() { public void testConversionExecutorNotFound() {
try { try {
service.getConversionExecutor(type(String.class), type(Integer.class)); service.getConversionExecutor(String.class, type(Integer.class));
fail("Should have thrown an exception"); fail("Should have thrown an exception");
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
} }
@ -91,18 +90,18 @@ public class GenericConversionServiceTests extends TestCase {
public void testConversionCompatibleTypes() { public void testConversionCompatibleTypes() {
String source = "foo"; String source = "foo";
assertSame(source, service.getConversionExecutor(type(String.class), type(String.class)).execute(source)); assertSame(source, service.getConversionExecutor(String.class, type(String.class)).execute(source));
} }
public void testConversionExecutorNullArgument() { public void testConversionExecutorNullArgument() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(Integer.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
assertNull(executor.execute(null)); assertNull(executor.execute(null));
} }
public void testConversionExecutorWrongTypeArgument() { public void testConversionExecutorWrongTypeArgument() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(Integer.class), type(String.class)); ConversionExecutor executor = service.getConversionExecutor(Integer.class, type(String.class));
try { try {
executor.execute("BOGUS"); executor.execute("BOGUS");
fail("Should have failed"); fail("Should have failed");
@ -121,7 +120,7 @@ public class GenericConversionServiceTests extends TestCase {
return target.toString(); return target.toString();
} }
}); });
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(Integer.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
Integer result = (Integer) executor.execute("3"); Integer result = (Integer) executor.execute("3");
assertEquals(new Integer(3), result); assertEquals(new Integer(3), result);
} }
@ -137,7 +136,7 @@ public class GenericConversionServiceTests extends TestCase {
} }
}); });
try { try {
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(Integer.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
fail("Should have failed"); fail("Should have failed");
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
@ -146,14 +145,14 @@ public class GenericConversionServiceTests extends TestCase {
public void testConversionObjectToPrimitive() { public void testConversionObjectToPrimitive() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(int.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(int.class));
Integer three = (Integer) executor.execute("3"); Integer three = (Integer) executor.execute("3");
assertEquals(3, three.intValue()); assertEquals(3, three.intValue());
} }
public void testConversionArrayToArray() { public void testConversionArrayToArray() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String[].class), type(Integer[].class)); ConversionExecutor executor = service.getConversionExecutor(String[].class, type(Integer[].class));
Integer[] result = (Integer[]) executor.execute(new String[] { "1", "2", "3" }); Integer[] result = (Integer[]) executor.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer(1), result[0]); assertEquals(new Integer(1), result[0]);
assertEquals(new Integer(2), result[1]); assertEquals(new Integer(2), result[1]);
@ -162,7 +161,7 @@ public class GenericConversionServiceTests extends TestCase {
public void testConversionArrayToPrimitiveArray() { public void testConversionArrayToPrimitiveArray() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String[].class), type(int[].class)); ConversionExecutor executor = service.getConversionExecutor(String[].class, type(int[].class));
int[] result = (int[]) executor.execute(new String[] { "1", "2", "3" }); int[] result = (int[]) executor.execute(new String[] { "1", "2", "3" });
assertEquals(1, result[0]); assertEquals(1, result[0]);
assertEquals(2, result[1]); assertEquals(2, result[1]);
@ -170,7 +169,7 @@ public class GenericConversionServiceTests extends TestCase {
} }
public void testConversionArrayToListInterface() { public void testConversionArrayToListInterface() {
ConversionExecutor executor = service.getConversionExecutor(type(String[].class), type(List.class)); ConversionExecutor executor = service.getConversionExecutor(String[].class, type(List.class));
List result = (List) executor.execute(new String[] { "1", "2", "3" }); List result = (List) executor.execute(new String[] { "1", "2", "3" });
assertEquals("1", result.get(0)); assertEquals("1", result.get(0));
assertEquals("2", result.get(1)); assertEquals("2", result.get(1));
@ -181,7 +180,7 @@ public class GenericConversionServiceTests extends TestCase {
public void testConversionArrayToListGenericTypeConversion() throws Exception { public void testConversionArrayToListGenericTypeConversion() throws Exception {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); ConversionExecutor executor = service.getConversionExecutor(String[].class, new TypeDescriptor(getClass().getDeclaredField("genericList")));
List result = (List) executor.execute(new String[] { "1", "2", "3" }); List result = (List) executor.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer("1"), result.get(0)); assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1)); assertEquals(new Integer("2"), result.get(1));
@ -189,7 +188,7 @@ public class GenericConversionServiceTests extends TestCase {
} }
public void testConversionArrayToListImpl() { public void testConversionArrayToListImpl() {
ConversionExecutor executor = service.getConversionExecutor(type(String[].class), type(LinkedList.class)); ConversionExecutor executor = service.getConversionExecutor(String[].class, type(LinkedList.class));
LinkedList result = (LinkedList) executor.execute(new String[] { "1", "2", "3" }); LinkedList result = (LinkedList) executor.execute(new String[] { "1", "2", "3" });
assertEquals("1", result.get(0)); assertEquals("1", result.get(0));
assertEquals("2", result.get(1)); assertEquals("2", result.get(1));
@ -198,14 +197,14 @@ public class GenericConversionServiceTests extends TestCase {
public void testConversionArrayToAbstractList() { public void testConversionArrayToAbstractList() {
try { try {
service.getConversionExecutor(type(String[].class), type(AbstractList.class)); service.getConversionExecutor(String[].class, type(AbstractList.class));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
} }
} }
public void testConversionListToArray() { public void testConversionListToArray() {
ConversionExecutor executor = service.getConversionExecutor(type(Collection.class), type(String[].class)); ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(String[].class));
List list = new ArrayList(); List list = new ArrayList();
list.add("1"); list.add("1");
list.add("2"); list.add("2");
@ -218,7 +217,7 @@ public class GenericConversionServiceTests extends TestCase {
public void testConversionListToArrayWithComponentConversion() { public void testConversionListToArrayWithComponentConversion() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(Collection.class), type(Integer[].class)); ConversionExecutor executor = service.getConversionExecutor(Collection.class, type(Integer[].class));
List list = new ArrayList(); List list = new ArrayList();
list.add("1"); list.add("1");
list.add("2"); list.add("2");
@ -232,7 +231,7 @@ public class GenericConversionServiceTests extends TestCase {
@Ignore @Ignore
@Test @Test
public void conversionObjectToArray() { public void conversionObjectToArray() {
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(String[].class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(String[].class));
String[] result = (String[]) executor.execute("1,2,3"); String[] result = (String[]) executor.execute("1,2,3");
assertEquals(1, result.length); assertEquals(1, result.length);
assertEquals("1,2,3", result[0]); assertEquals("1,2,3", result[0]);
@ -242,7 +241,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void conversionObjectToArrayWithElementConversion() { public void conversionObjectToArrayWithElementConversion() {
service.addConverter(new StringToInteger()); service.addConverter(new StringToInteger());
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(Integer[].class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer[].class));
Integer[] result = (Integer[]) executor.execute("123"); Integer[] result = (Integer[]) executor.execute("123");
assertEquals(1, result.length); assertEquals(1, result.length);
assertEquals(new Integer(123), result[0]); assertEquals(new Integer(123), result[0]);
@ -254,19 +253,19 @@ public class GenericConversionServiceTests extends TestCase {
public void testSuperConverterConversionForwardIndex() { public void testSuperConverterConversionForwardIndex() {
service.addConverter(new StringToEnum()); service.addConverter(new StringToEnum());
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(FooEnum.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(FooEnum.class));
assertEquals(FooEnum.BAR, executor.execute("BAR")); assertEquals(FooEnum.BAR, executor.execute("BAR"));
} }
public void testSuperTwoWayConverterConversionReverseIndex() { public void testSuperTwoWayConverterConversionReverseIndex() {
service.addConverter(new StringToEnum()); service.addConverter(new StringToEnum());
ConversionExecutor executor = service.getConversionExecutor(type(FooEnum.class), type(String.class)); ConversionExecutor executor = service.getConversionExecutor(FooEnum.class, type(String.class));
assertEquals("BAR", executor.execute(FooEnum.BAR)); assertEquals("BAR", executor.execute(FooEnum.BAR));
} }
public void testSuperConverterConversionNotConvertibleAbstractType() { public void testSuperConverterConversionNotConvertibleAbstractType() {
service.addConverter(new StringToEnum()); service.addConverter(new StringToEnum());
ConversionExecutor executor = service.getConversionExecutor(type(String.class), type(Enum.class)); ConversionExecutor executor = service.getConversionExecutor(String.class, type(Enum.class));
try { try {
executor.execute("WHATEV"); executor.execute("WHATEV");
fail("Should have failed"); fail("Should have failed");
@ -298,7 +297,7 @@ public class GenericConversionServiceTests extends TestCase {
return 0; return 0;
} }
}; };
ConversionExecutor executor = service.getConversionExecutor(type(Integer.class), type(customNumber.getClass())); ConversionExecutor executor = service.getConversionExecutor(Integer.class, type(customNumber.getClass()));
try { try {
executor.execute(3); executor.execute(3);
fail("Should have failed"); fail("Should have failed");
@ -311,7 +310,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionForwardIndex() { public void customConverterConversionForwardIndex() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(String.class), type(Principal.class)); ConversionExecutor executor = service.getConversionExecutor("princy", String.class, type(Principal.class));
assertEquals("keith", ((Principal) executor.execute("keith")).getName()); assertEquals("keith", ((Principal) executor.execute("keith")).getName());
} }
@ -319,7 +318,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionReverseIndex() { public void customConverterConversionReverseIndex() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(Principal.class), type(String.class)); ConversionExecutor executor = service.getConversionExecutor("princy", Principal.class, type(String.class));
assertEquals("keith", executor.execute(new Principal() { assertEquals("keith", executor.execute(new Principal() {
public String getName() { public String getName() {
return "keith"; return "keith";
@ -331,7 +330,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionForSameType() { public void customConverterConversionForSameType() {
service.addConverter("trimmer", new Trimmer()); service.addConverter("trimmer", new Trimmer());
ConversionExecutor executor = service.getConversionExecutor("trimmer", type(String.class), type(String.class)); ConversionExecutor executor = service.getConversionExecutor("trimmer", String.class, type(String.class));
assertEquals("a string", executor.execute("a string ")); assertEquals("a string", executor.execute("a string "));
} }
@ -340,7 +339,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupNotCompatibleSource() { public void customConverterLookupNotCompatibleSource() {
service.addConverter("trimmer", new Trimmer()); service.addConverter("trimmer", new Trimmer());
try { try {
service.getConversionExecutor("trimmer", type(Object.class), type(String.class)); service.getConversionExecutor("trimmer", Object.class, type(String.class));
fail("Should have failed"); fail("Should have failed");
} catch (ConversionException e) { } catch (ConversionException e) {
@ -352,7 +351,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupNotCompatibleTarget() { public void customConverterLookupNotCompatibleTarget() {
service.addConverter("trimmer", new Trimmer()); service.addConverter("trimmer", new Trimmer());
try { try {
service.getConversionExecutor("trimmer", type(String.class), type(Object.class)); service.getConversionExecutor("trimmer", String.class, type(Object.class));
} catch (ConversionException e) { } catch (ConversionException e) {
} }
@ -363,7 +362,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupNotCompatibleTargetReverse() { public void customConverterLookupNotCompatibleTargetReverse() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
try { try {
service.getConversionExecutor("princy", type(Principal.class), type(Integer.class)); service.getConversionExecutor("princy", Principal.class, type(Integer.class));
} catch (ConversionException e) { } catch (ConversionException e) {
} }
@ -373,7 +372,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionArrayToArray() { public void customConverterConversionArrayToArray() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(String[].class), type(Principal[].class)); ConversionExecutor executor = service.getConversionExecutor("princy", String[].class, type(Principal[].class));
Principal[] p = (Principal[]) executor.execute(new String[] { "princy1", "princy2" }); Principal[] p = (Principal[]) executor.execute(new String[] { "princy1", "princy2" });
assertEquals("princy1", p[0].getName()); assertEquals("princy1", p[0].getName());
assertEquals("princy2", p[1].getName()); assertEquals("princy2", p[1].getName());
@ -383,7 +382,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionArrayToArrayReverse() { public void customConverterConversionArrayToArrayReverse() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(Principal[].class), type(String[].class)); ConversionExecutor executor = service.getConversionExecutor("princy", Principal[].class, type(String[].class));
final Principal princy1 = new Principal() { final Principal princy1 = new Principal() {
public String getName() { public String getName() {
return "princy1"; return "princy1";
@ -404,7 +403,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupArrayToArrayBogusSource() { public void customConverterLookupArrayToArrayBogusSource() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
try { try {
service.getConversionExecutor("princy", type(Integer[].class), type(Principal[].class)); service.getConversionExecutor("princy", Integer[].class, type(Principal[].class));
fail("Should have failed"); fail("Should have failed");
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
} }
@ -415,7 +414,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupArrayToArrayBogusTarget() { public void customConverterLookupArrayToArrayBogusTarget() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
try { try {
service.getConversionExecutor("princy", type(Principal[].class), type(Integer[].class)); service.getConversionExecutor("princy", Principal[].class, type(Integer[].class));
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
} }
@ -425,7 +424,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionArrayToCollection() { public void customConverterConversionArrayToCollection() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(String[].class), type(List.class)); ConversionExecutor executor = service.getConversionExecutor("princy", String[].class, type(List.class));
List list = (List) executor.execute(new String[] { "princy1", "princy2" }); List list = (List) executor.execute(new String[] { "princy1", "princy2" });
assertEquals("princy1", ((Principal) list.get(0)).getName()); assertEquals("princy1", ((Principal) list.get(0)).getName());
assertEquals("princy2", ((Principal) list.get(1)).getName()); assertEquals("princy2", ((Principal) list.get(1)).getName());
@ -435,7 +434,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionArrayToCollectionReverse() { public void customConverterConversionArrayToCollectionReverse() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(Principal[].class), type(List.class)); ConversionExecutor executor = service.getConversionExecutor("princy", Principal[].class, type(List.class));
final Principal princy1 = new Principal() { final Principal princy1 = new Principal() {
public String getName() { public String getName() {
return "princy1"; return "princy1";
@ -456,7 +455,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupArrayToCollectionBogusSource() { public void customConverterLookupArrayToCollectionBogusSource() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
try { try {
service.getConversionExecutor("princy", type(Integer[].class), type(List.class)); service.getConversionExecutor("princy", Integer[].class, type(List.class));
fail("Should have failed"); fail("Should have failed");
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
@ -467,7 +466,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterLookupCollectionToArray() { public void customConverterLookupCollectionToArray() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(List.class), type(Principal[].class)); ConversionExecutor executor = service.getConversionExecutor("princy", List.class, type(Principal[].class));
List princyList = new ArrayList(); List princyList = new ArrayList();
princyList.add("princy1"); princyList.add("princy1");
princyList.add("princy2"); princyList.add("princy2");
@ -480,7 +479,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterLookupCollectionToArrayReverse() { public void customConverterLookupCollectionToArrayReverse() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(List.class), type(String[].class)); ConversionExecutor executor = service.getConversionExecutor("princy", List.class, type(String[].class));
final Principal princy1 = new Principal() { final Principal princy1 = new Principal() {
public String getName() { public String getName() {
return "princy1"; return "princy1";
@ -504,7 +503,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupCollectionToArrayBogusTarget() { public void customConverterLookupCollectionToArrayBogusTarget() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
try { try {
service.getConversionExecutor("princy", type(List.class), type(Integer[].class)); service.getConversionExecutor("princy", List.class, type(Integer[].class));
fail("Should have failed"); fail("Should have failed");
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
@ -515,7 +514,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionObjectToArray() { public void customConverterConversionObjectToArray() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(String.class), type(Principal[].class)); ConversionExecutor executor = service.getConversionExecutor("princy", String.class, type(Principal[].class));
Principal[] p = (Principal[]) executor.execute("princy1"); Principal[] p = (Principal[]) executor.execute("princy1");
assertEquals("princy1", p[0].getName()); assertEquals("princy1", p[0].getName());
} }
@ -524,7 +523,7 @@ public class GenericConversionServiceTests extends TestCase {
@Test @Test
public void customConverterConversionObjectToArrayReverse() { public void customConverterConversionObjectToArrayReverse() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", type(Principal.class), type(String[].class)); ConversionExecutor executor = service.getConversionExecutor("princy", Principal.class, type(String[].class));
final Principal princy1 = new Principal() { final Principal princy1 = new Principal() {
public String getName() { public String getName() {
return "princy1"; return "princy1";
@ -539,7 +538,7 @@ public class GenericConversionServiceTests extends TestCase {
public void customConverterLookupObjectToArrayBogusSource() { public void customConverterLookupObjectToArrayBogusSource() {
service.addConverter("princy", new CustomTwoWayConverter()); service.addConverter("princy", new CustomTwoWayConverter());
try { try {
service.getConversionExecutor("princy", type(Integer.class), type(Principal[].class)); service.getConversionExecutor("princy", Integer.class, type(Principal[].class));
fail("Should have failed"); fail("Should have failed");
} catch (ConversionExecutorNotFoundException e) { } catch (ConversionExecutorNotFoundException e) {
@ -576,11 +575,7 @@ public class GenericConversionServiceTests extends TestCase {
public void testSuperTwoWayConverterConverterAdaption() { public void testSuperTwoWayConverterConverterAdaption() {
service.addConverter(GenericConversionService.converterFor(String.class, FooEnum.class, new StringToEnum())); service.addConverter(GenericConversionService.converterFor(String.class, FooEnum.class, new StringToEnum()));
assertEquals(FooEnum.BAR, service.executeConversion(value("BAR"), type(FooEnum.class))); assertEquals(FooEnum.BAR, service.executeConversion("BAR", type(FooEnum.class)));
}
private TypedValue value(Object obj) {
return new TypedValue(obj);
} }
private TypeDescriptor type(Class<?> clazz) { private TypeDescriptor type(Class<?> clazz) {