提交 d7c90cff 编写于 作者: K Keith Donald

made ConversionExecutor internal; removed other unused operations from public SPI

上级 bf7a9475
......@@ -16,10 +16,10 @@
package org.springframework.core.convert;
/**
* A service interface for type conversion. This is the entry point into the convert system. Call one of the
* <i>executeConversion</i> operations to perform a thread-safe type conversion using
* this system. Call one of the <i>getConversionExecutor</i> operations to obtain
* a thread-safe {@link ConversionExecutor} command for later use.
* A service interface for type conversion. This is the entry point into the convert system.
* <p>
* Call {@link #executeConversion(Object, TypeDescriptor)} to perform a thread-safe type conversion using
* this system.
*
* @author Keith Donald
*/
......@@ -28,47 +28,21 @@ public interface ConversionService {
/**
* Returns true if objects of sourceType can be converted to targetType.
* @param source the source to convert from (may be null)
* @param targetType the target type to convert to
* @param targetType context about the target type to convert to
* @return true if a conversion can be performed, false if not
*/
public boolean canConvert(Class<?> sourceType, TypeDescriptor targetType);
/**
* Returns true if the source can be converted to targetType.
* @param source the source to convert from (may be null)
* @param targetType the target type to convert to
* @return true if a conversion can be performed, false if not
*/
public boolean canConvert(Object source, TypeDescriptor targetType);
/**
* Convert the source to targetType.
* @param source the source to convert from (may be null)
* @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
* @param targetType context about the target type to convert to
* @return the converted object, an instance of {@link TypeDescriptor#getType()}</code>, or <code>null</code> if a null source
* was provided
* @throws ConversionExecutorNotFoundException if no suitable conversion executor could be found to convert the
* @throws ConverterNotFoundException if no suitable conversion executor could be found to convert the
* source to an instance of targetType
* @throws ConversionException if an exception occurred during the conversion process
*/
public Object executeConversion(Object source, TypeDescriptor targetType) throws ConversionExecutorNotFoundException,
ConversionException;
/**
* 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.
* @param sourceType the source type to convert from (required)
* @param targetType the target type to convert to (required)
* @return the executor that can execute instance type conversion, never null
* @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found
*/
public ConversionExecutor getConversionExecutor(Class<?> sourceType, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException;
/**
* Get a type by its name; may be the fully-qualified class name or a registered type alias such as 'int'.
* @return the class, or <code>null</code> if no such name exists
*/
public Class<?> getType(String name);
public Object executeConversion(Object source, TypeDescriptor targetType);
}
\ No newline at end of file
/*
* 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;
/**
* Thrown when a conversion executor could not be found in a conversion service.
*
* @author Keith Donald
*/
public class ConverterNotFoundException extends ConversionException {
private Class<?> sourceType;
private TypeDescriptor targetType;
/**
* Creates a new conversion executor not found exception.
* @param sourceType the source type requested to convert from
* @param targetType the target type requested to convert to
* @param message a descriptive message
*/
public ConverterNotFoundException(Class<?> sourceType, TypeDescriptor targetType, String message) {
super(message);
this.sourceType = sourceType;
this.targetType = targetType;
}
/**
* Returns the source type that was requested to convert from.
*/
public Class<?> getSourceType() {
return sourceType;
}
/**
* Returns the target type that was requested to convert to.
*/
public TypeDescriptor getTargetType() {
return targetType;
}
}
......@@ -16,7 +16,6 @@
package org.springframework.core.convert.service;
import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
......@@ -26,7 +25,7 @@ import org.springframework.core.convert.TypeDescriptor;
*/
abstract class AbstractCollectionConverter implements ConversionExecutor {
private ConversionService conversionService;
private GenericConversionService conversionService;
private ConversionExecutor elementConverter;
......@@ -34,7 +33,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
private TypeDescriptor targetCollectionType;
public AbstractCollectionConverter(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, ConversionService conversionService) {
public AbstractCollectionConverter(TypeDescriptor sourceCollectionType, TypeDescriptor targetCollectionType, GenericConversionService conversionService) {
this.conversionService = conversionService;
this.sourceCollectionType = sourceCollectionType;
this.targetCollectionType = targetCollectionType;
......@@ -61,7 +60,7 @@ abstract class AbstractCollectionConverter implements ConversionExecutor {
return targetCollectionType.getElementType();
}
protected ConversionService getConversionService() {
protected GenericConversionService getConversionService() {
return conversionService;
}
......
......@@ -18,7 +18,6 @@ package org.springframework.core.convert.service;
import java.lang.reflect.Array;
import java.util.Collection;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.TypeDescriptor;
/**
......
......@@ -19,7 +19,6 @@ import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.TypeDescriptor;
/**
......
......@@ -18,7 +18,6 @@ package org.springframework.core.convert.service;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.TypeDescriptor;
/**
......
......@@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert;
package org.springframework.core.convert.service;
import org.springframework.core.convert.ConversionExecutionException;
/**
* A command parameterized with the information necessary to perform a conversion of a source input to a
......@@ -29,6 +31,6 @@ public interface ConversionExecutor {
* @param source the source to convert
* @throws ConversionExecutionException if an exception occurs during type conversion
*/
public Object execute(Object source) throws ConversionExecutionException;
public Object execute(Object source);
}
\ No newline at end of file
......@@ -49,7 +49,6 @@ public class DefaultConversionService extends GenericConversionService {
*/
public DefaultConversionService() {
addDefaultConverters();
addDefaultAliases();
}
/**
......@@ -73,21 +72,4 @@ public class DefaultConversionService extends GenericConversionService {
addConverter(new ObjectToString());
}
protected void addDefaultAliases() {
addAlias("string", String.class);
addAlias("byte", Byte.class);
addAlias("boolean", Boolean.class);
addAlias("char", Character.class);
addAlias("short", Short.class);
addAlias("int", Integer.class);
addAlias("long", Long.class);
addAlias("float", Float.class);
addAlias("double", Double.class);
addAlias("bigInt", BigInteger.class);
addAlias("bigDecimal", BigDecimal.class);
addAlias("locale", Locale.class);
addAlias("enum", Enum.class);
addAlias("date", Date.class);
}
}
\ No newline at end of file
......@@ -26,10 +26,8 @@ import java.util.List;
import java.util.Map;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.ConversionExecutorNotFoundException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterInfo;
......@@ -61,11 +59,6 @@ public class GenericConversionService implements ConversionService {
*/
private final Map sourceTypeSuperConverters = new HashMap();
/**
* Indexes classes by well-known aliases.
*/
private final Map aliasMap = new HashMap<String, Class<?>>();
/**
* An optional parent conversion service.
*/
......@@ -132,48 +125,41 @@ public class GenericConversionService implements ConversionService {
return new SuperTwoWayConverterConverter(converter, sourceType, targetType);
}
/**
* Add a convenient alias for the target type. {@link #getType(String)} can then be used to lookup the type given
* the alias.
* @see #getType(String)
*/
public void addAlias(String alias, Class targetType) {
aliasMap.put(alias, targetType);
}
// implementing ConversionService
public boolean canConvert(Class<?> sourceType, TypeDescriptor targetType) {
try {
getConversionExecutor(sourceType, targetType);
ConversionExecutor executor = getConversionExecutor(sourceType, targetType);
if (executor != null) {
return true;
} catch (ConversionExecutorNotFoundException e) {
return false;
}
}
public boolean canConvert(Object source, TypeDescriptor targetType) {
if (source == null) {
return true;
}
try {
getConversionExecutor(source.getClass(), targetType);
return true;
} catch (ConversionExecutorNotFoundException e) {
return false;
} else {
if (parent != null) {
return parent.canConvert(sourceType, targetType);
} else {
return false;
}
}
}
public Object executeConversion(Object source, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException, ConversionException {
public Object executeConversion(Object source, TypeDescriptor targetType) {
if (source == null) {
return null;
}
return getConversionExecutor(source.getClass(), targetType).execute(source);
ConversionExecutor executor = getConversionExecutor(source.getClass(), targetType);
if (executor != null) {
return executor.execute(source);
} else {
if (parent != null) {
return parent.executeConversion(source, targetType);
} else {
throw new ConverterNotFoundException(source.getClass(), targetType,
"No converter found that can convert from sourceType [" + source.getClass().getName()
+ "] to targetType [" + targetType.getName() + "]");
}
}
}
public ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType)
throws ConversionExecutorNotFoundException {
ConversionExecutor getConversionExecutor(Class sourceClass, TypeDescriptor targetType)
throws ConverterNotFoundException {
Assert.notNull(sourceClass, "The sourceType to convert from is required");
Assert.notNull(targetType, "The targetType to convert to is required");
TypeDescriptor sourceType = TypeDescriptor.valueOf(sourceClass);
......@@ -193,21 +179,21 @@ public class GenericConversionService implements ConversionService {
if (sourceType.isCollection()) {
return new CollectionToArray(sourceType, targetType, this);
} else {
throw new ConversionExecutorNotFoundException(sourceType, targetType, "Object to Array conversion not yet supported");
return null;
}
}
if (sourceType.isCollection()) {
if (targetType.isCollection()) {
return new CollectionToCollection(sourceType, targetType, this);
} else {
throw new ConversionExecutorNotFoundException(sourceType, targetType, "Object to Collection conversion not yet supported");
return null;
}
}
if (sourceType.isMap()) {
if (targetType.isMap()) {
return new MapToMap(sourceType, targetType, this);
} else {
throw new ConversionExecutorNotFoundException(sourceType, targetType, "Object to Map conversion not yet supported");
return null;
}
}
Converter converter = findRegisteredConverter(sourceClass, targetType.getType());
......@@ -217,28 +203,10 @@ public class GenericConversionService implements ConversionService {
SuperConverter superConverter = findRegisteredSuperConverter(sourceClass, targetType.getType());
if (superConverter != null) {
return new StaticSuperConversionExecutor(sourceType, targetType, superConverter);
}
if (parent != null) {
return parent.getConversionExecutor(sourceClass, targetType);
} else {
if (sourceType.isAssignableTo(targetType)) {
return new StaticConversionExecutor(sourceType, targetType, NoOpConverter.INSTANCE);
}
throw new ConversionExecutorNotFoundException(sourceType, targetType,
"No ConversionExecutor found for converting from sourceType [" + sourceType.getName()
+ "] to targetType [" + targetType.getName() + "]");
}
}
}
public Class getType(String name) throws IllegalArgumentException {
Class clazz = (Class) aliasMap.get(name);
if (clazz != null) {
return clazz;
} else {
if (parent != null) {
return parent.getType(name);
} else {
return null;
}
}
......
......@@ -22,7 +22,6 @@ import java.util.SortedMap;
import java.util.TreeMap;
import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
......@@ -36,7 +35,7 @@ class MapToMap implements ConversionExecutor {
private TypeDescriptor targetType;
private ConversionService conversionService;
private GenericConversionService conversionService;
private EntryConverter entryConverter;
......@@ -46,7 +45,7 @@ class MapToMap implements ConversionExecutor {
* @param targetType the target map type
* @param conversionService the conversion service
*/
public MapToMap(TypeDescriptor sourceType, TypeDescriptor targetType, ConversionService conversionService) {
public MapToMap(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) {
this.sourceType = sourceType;
this.targetType = targetType;
this.conversionService = conversionService;
......
......@@ -16,7 +16,6 @@
package org.springframework.core.convert.service;
import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor;
/**
* Conversion executor that does nothing. Access singleton at {@link #INSTANCE}.s
......
......@@ -16,7 +16,6 @@
package org.springframework.core.convert.service;
import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.style.ToStringCreator;
......
......@@ -16,7 +16,6 @@
package org.springframework.core.convert.service;
import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.SuperConverter;
import org.springframework.core.style.ToStringCreator;
......
......@@ -31,8 +31,7 @@ import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.convert.ConversionExecutionException;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.ConversionExecutorNotFoundException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.NumberToNumber;
......@@ -84,9 +83,9 @@ public class GenericConversionServiceTests {
@Test
public void convertExecutorNotFound() {
try {
service.getConversionExecutor(String.class, type(Integer.class));
service.executeConversion("3", type(Integer.class));
fail("Should have thrown an exception");
} catch (ConversionExecutorNotFoundException e) {
} catch (ConverterNotFoundException e) {
}
}
......@@ -161,9 +160,9 @@ public class GenericConversionServiceTests {
}
});
try {
ConversionExecutor executor = service.getConversionExecutor(String.class, type(Integer.class));
service.executeConversion("3", type(Integer.class));
fail("Should have failed");
} catch (ConversionExecutorNotFoundException e) {
} catch (ConverterNotFoundException e) {
}
}
......
......@@ -17,7 +17,7 @@
package org.springframework.expression.spel.support;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionExecutorNotFoundException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.service.DefaultConversionService;
......@@ -53,7 +53,7 @@ public class StandardTypeConverter implements TypeConverter {
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
try {
return conversionService.executeConversion(value, typeDescriptor);
} catch (ConversionExecutorNotFoundException cenfe) {
} catch (ConverterNotFoundException cenfe) {
throw new SpelException(cenfe, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
} catch (ConversionException ce) {
throw new SpelException(ce, SpelMessages.TYPE_CONVERSION_ERROR, value.getClass(), typeDescriptor.asString());
......
......@@ -19,7 +19,6 @@ package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.convert.ConversionExecutor;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.service.DefaultConversionService;
import org.springframework.core.convert.service.GenericConversionService;
......@@ -65,17 +64,14 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
// ArrayList containing List<Integer> to List<String>
Class<?> clazz = typeDescriptorForListOfString.getElementType();
assertEquals(String.class,clazz);
ConversionExecutor executor = tcs.getConversionExecutor(ArrayList.class, typeDescriptorForListOfString);
assertNotNull(executor);
List l = (List)executor.execute(listOfInteger);
List l = (List) tcs.executeConversion(listOfInteger, typeDescriptorForListOfString);
assertNotNull(l);
// ArrayList containing List<String> to List<Integer>
clazz = typeDescriptorForListOfInteger.getElementType();
assertEquals(Integer.class,clazz);
executor = tcs.getConversionExecutor(ArrayList.class, typeDescriptorForListOfInteger);
assertNotNull(executor);
l = (List)executor.execute(listOfString);
l = (List) tcs.executeConversion(listOfString, typeDescriptorForListOfString);
assertNotNull(l);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册