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
* a thread-safe {@link ConversionExecutor} command for later use.
*
* TODO - is TypeDescriptor/TypedValue needed on source?
*
* @author Keith Donald
*/
public interface ConversionService {
@@ -33,10 +31,10 @@ public interface ConversionService {
* @param targetType the target type to convert to
* @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 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
@@ -45,11 +43,11 @@ public interface ConversionService {
* source to an instance of targetType
* @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;
/**
* 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
* capable of converting to the targetType
* @param source the source to convert from (may be null)
@@ -60,22 +58,22 @@ public interface ConversionService {
* source to an instance of targetType
* @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;
/**
* 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.
* @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(TypeDescriptor sourceType, TypeDescriptor targetType)
public ConversionExecutor getConversionExecutor(Class<?> sourceType, TypeDescriptor targetType)
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.
* @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)
@@ -84,7 +82,7 @@ public interface ConversionService {
* @return the executor that can execute instance type conversion, never null
* @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;
/**

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