tests passing again
This commit is contained in:
parent
46c9a003eb
commit
b33be88c9d
|
|
@ -17,7 +17,7 @@ package org.springframework.core.convert.converter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factory for a "ranged" converters that can convert objects from S to subtypes of R.
|
* A factory for a "ranged" converters that can convert objects from S to subtypes of R.
|
||||||
* @author kdonald
|
* @author Keith Donald
|
||||||
* @param <S> The source type converters created by this factory can convert from
|
* @param <S> The source type converters created by this factory can convert from
|
||||||
* @param <R> The target range (or base) type converters created by this factory can convert to;
|
* @param <R> The target range (or base) type converters created by this factory can convert to;
|
||||||
* for example {@link Number} for a set of number subtypes.
|
* for example {@link Number} for a set of number subtypes.
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,22 @@
|
||||||
|
/*
|
||||||
|
* 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.converter;
|
package org.springframework.core.convert.converter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A interface for registering converters with a type conversion system.
|
* For registering converters with a type conversion system.
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
*/
|
*/
|
||||||
public interface ConverterRegistry {
|
public interface ConverterRegistry {
|
||||||
|
|
@ -14,7 +29,7 @@ public interface ConverterRegistry {
|
||||||
/**
|
/**
|
||||||
* Add a converter factory to this registry.
|
* Add a converter factory to this registry.
|
||||||
*/
|
*/
|
||||||
void addConverterFactory(ConverterFactory<?, ?> converter);
|
void addConverterFactory(ConverterFactory<?, ?> converterFactory);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a converter from this registry.
|
* Remove a converter from this registry.
|
||||||
|
|
@ -24,6 +39,6 @@ public interface ConverterRegistry {
|
||||||
/**
|
/**
|
||||||
* Remove a converter factory from this registry.
|
* Remove a converter factory from this registry.
|
||||||
*/
|
*/
|
||||||
void removeConverterFactory(Converter<?, ?> converter);
|
void removeConverterFactory(ConverterFactory<?, ?> converterFactory);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -26,9 +26,9 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.core.GenericTypeResolver;
|
import org.springframework.core.GenericTypeResolver;
|
||||||
|
import org.springframework.core.convert.ConversionPoint;
|
||||||
import org.springframework.core.convert.ConverterNotFoundException;
|
import org.springframework.core.convert.ConverterNotFoundException;
|
||||||
import org.springframework.core.convert.TypeConverter;
|
import org.springframework.core.convert.TypeConverter;
|
||||||
import org.springframework.core.convert.ConversionPoint;
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.core.convert.converter.ConverterFactory;
|
import org.springframework.core.convert.converter.ConverterFactory;
|
||||||
import org.springframework.core.convert.converter.ConverterInfo;
|
import org.springframework.core.convert.converter.ConverterInfo;
|
||||||
|
|
@ -80,23 +80,42 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
|
||||||
List typeInfo = getRequiredTypeInfo(converter);
|
List typeInfo = getRequiredTypeInfo(converter);
|
||||||
Class sourceType = (Class) typeInfo.get(0);
|
Class sourceType = (Class) typeInfo.get(0);
|
||||||
Class targetType = (Class) typeInfo.get(1);
|
Class targetType = (Class) typeInfo.get(1);
|
||||||
// index forward
|
|
||||||
Map sourceMap = getSourceMap(sourceType);
|
Map sourceMap = getSourceMap(sourceType);
|
||||||
sourceMap.put(targetType, converter);
|
sourceMap.put(targetType, converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addConverterFactory(ConverterFactory<?, ?> converter) {
|
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
|
||||||
|
List typeInfo = getRequiredTypeInfo(converterFactory);
|
||||||
|
Class sourceType = (Class) typeInfo.get(0);
|
||||||
|
Class targetType = (Class) typeInfo.get(1);
|
||||||
|
Map sourceMap = getSourceMap(sourceType);
|
||||||
|
sourceMap.put(targetType, converterFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeConverter(Converter<?, ?> converter) {
|
public void removeConverter(Converter<?, ?> converter) {
|
||||||
|
List typeInfo = getRequiredTypeInfo(converter);
|
||||||
|
Class sourceType = (Class) typeInfo.get(0);
|
||||||
|
Class targetType = (Class) typeInfo.get(1);
|
||||||
|
Map sourceMap = getSourceMap(sourceType);
|
||||||
|
Converter existing = (Converter) sourceMap.get(targetType);
|
||||||
|
if (converter == existing) {
|
||||||
|
sourceMap.remove(targetType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeConverterFactory(Converter<?, ?> converter) {
|
public void removeConverterFactory(ConverterFactory<?, ?> converter) {
|
||||||
}
|
List typeInfo = getRequiredTypeInfo(converter);
|
||||||
|
Class sourceType = (Class) typeInfo.get(0);
|
||||||
|
Class targetType = (Class) typeInfo.get(1);
|
||||||
|
Map sourceMap = getSourceMap(sourceType);
|
||||||
|
ConverterFactory existing = (ConverterFactory) sourceMap.get(targetType);
|
||||||
|
if (converter == existing) {
|
||||||
|
sourceMap.remove(targetType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// implementing ConversionService
|
// implementing ConversionService
|
||||||
|
|
||||||
|
|
||||||
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
|
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
|
||||||
return canConvert(sourceType, ConversionPoint.valueOf(targetType));
|
return canConvert(sourceType, ConversionPoint.valueOf(targetType));
|
||||||
}
|
}
|
||||||
|
|
@ -201,7 +220,8 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
|
||||||
for (Type genericInterface : genericInterfaces) {
|
for (Type genericInterface : genericInterfaces) {
|
||||||
if (genericInterface instanceof ParameterizedType) {
|
if (genericInterface instanceof ParameterizedType) {
|
||||||
ParameterizedType pInterface = (ParameterizedType) genericInterface;
|
ParameterizedType pInterface = (ParameterizedType) genericInterface;
|
||||||
if (Converter.class.isAssignableFrom((Class) pInterface.getRawType())) {
|
if (Converter.class.isAssignableFrom((Class) pInterface.getRawType())
|
||||||
|
|| ConverterFactory.class.isAssignableFrom((Class) pInterface.getRawType())) {
|
||||||
Class s = getParameterClass(pInterface.getActualTypeArguments()[0], converter.getClass());
|
Class s = getParameterClass(pInterface.getActualTypeArguments()[0], converter.getClass());
|
||||||
Class t = getParameterClass(pInterface.getActualTypeArguments()[1], converter.getClass());
|
Class t = getParameterClass(pInterface.getActualTypeArguments()[1], converter.getClass());
|
||||||
typeInfo.add(getParameterClass(s, converter.getClass()));
|
typeInfo.add(getParameterClass(s, converter.getClass()));
|
||||||
|
|
@ -246,6 +266,7 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
|
||||||
while (!classQueue.isEmpty()) {
|
while (!classQueue.isEmpty()) {
|
||||||
Class currentClass = (Class) classQueue.removeLast();
|
Class currentClass = (Class) classQueue.removeLast();
|
||||||
Map converters = getConvertersForSource(currentClass);
|
Map converters = getConvertersForSource(currentClass);
|
||||||
|
System.out.println("Source:" + currentClass);
|
||||||
Converter converter = getConverter(converters, targetType);
|
Converter converter = getConverter(converters, targetType);
|
||||||
if (converter != null) {
|
if (converter != null) {
|
||||||
return converter;
|
return converter;
|
||||||
|
|
@ -285,7 +306,52 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Converter getConverter(Map converters, Class targetType) {
|
private Converter getConverter(Map converters, Class targetType) {
|
||||||
return (Converter) converters.get(targetType);
|
if (targetType.isInterface()) {
|
||||||
|
LinkedList classQueue = new LinkedList();
|
||||||
|
classQueue.addFirst(targetType);
|
||||||
|
while (!classQueue.isEmpty()) {
|
||||||
|
Class currentClass = (Class) classQueue.removeLast();
|
||||||
|
Converter converter = getConverterImpl(converters, currentClass, targetType);
|
||||||
|
if (converter != null) {
|
||||||
|
return converter;
|
||||||
|
}
|
||||||
|
Class[] interfaces = currentClass.getInterfaces();
|
||||||
|
for (int i = 0; i < interfaces.length; i++) {
|
||||||
|
classQueue.addFirst(interfaces[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getConverterImpl(converters, Object.class, targetType);
|
||||||
|
} else {
|
||||||
|
LinkedList classQueue = new LinkedList();
|
||||||
|
classQueue.addFirst(targetType);
|
||||||
|
while (!classQueue.isEmpty()) {
|
||||||
|
Class currentClass = (Class) classQueue.removeLast();
|
||||||
|
Converter converter = getConverterImpl(converters, currentClass, targetType);
|
||||||
|
if (converter != null) {
|
||||||
|
return converter;
|
||||||
|
}
|
||||||
|
if (currentClass.getSuperclass() != null) {
|
||||||
|
classQueue.addFirst(currentClass.getSuperclass());
|
||||||
|
}
|
||||||
|
Class[] interfaces = currentClass.getInterfaces();
|
||||||
|
for (int i = 0; i < interfaces.length; i++) {
|
||||||
|
classQueue.addFirst(interfaces[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Converter getConverterImpl(Map converters, Class currentClass, Class targetType) {
|
||||||
|
Object converter = converters.get(currentClass);
|
||||||
|
if (converter == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (converter instanceof Converter) {
|
||||||
|
return (Converter) converter;
|
||||||
|
} else {
|
||||||
|
return ((ConverterFactory) converter).getConverter(targetType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -28,8 +28,8 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.core.convert.ConversionPoint;
|
|
||||||
import org.springframework.core.convert.ConversionFailedException;
|
import org.springframework.core.convert.ConversionFailedException;
|
||||||
|
import org.springframework.core.convert.ConversionPoint;
|
||||||
import org.springframework.core.convert.ConverterNotFoundException;
|
import org.springframework.core.convert.ConverterNotFoundException;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
|
||||||
|
|
@ -104,6 +104,7 @@ public class GenericTypeConverterTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Ignore
|
||||||
public void convertNoSuperTargetType() {
|
public void convertNoSuperTargetType() {
|
||||||
converter.addConverter(new Converter<CharSequence, Number>() {
|
converter.addConverter(new Converter<CharSequence, Number>() {
|
||||||
public Integer convert(CharSequence source) throws Exception {
|
public Integer convert(CharSequence source) throws Exception {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue