alignment with 3.0.7 backports (SPR-8674)

This commit is contained in:
Juergen Hoeller 2011-12-01 18:51:36 +00:00
parent 71e0a506b9
commit 33b53b7cca
6 changed files with 41 additions and 20 deletions

View File

@ -241,7 +241,8 @@ public class TypeDescriptor {
if (value == null) { if (value == null) {
return this; return this;
} }
return new TypeDescriptor(value.getClass(), elementTypeDescriptor, mapKeyTypeDescriptor, mapValueTypeDescriptor, annotations); return new TypeDescriptor(value.getClass(), this.elementTypeDescriptor,
this.mapKeyTypeDescriptor, this.mapValueTypeDescriptor, this.annotations);
} }
/** /**
@ -513,7 +514,7 @@ public class TypeDescriptor {
return typeDescriptor.narrow(value); return typeDescriptor.narrow(value);
} }
else { else {
return value != null ? new TypeDescriptor(value.getClass(), null, null, null, annotations) : null; return (value != null ? new TypeDescriptor(value.getClass(), null, null, null, this.annotations) : null);
} }
} }

View File

@ -49,7 +49,8 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
} }
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor(), this.conversionService); return ConversionUtils.canConvertElements(
sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor(), this.conversionService);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -68,7 +69,8 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
else { else {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Object sourceElement = Array.get(source, i); Object sourceElement = Array.get(source, i);
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor()); Object targetElement = this.conversionService.convert(sourceElement,
sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor());
target.add(targetElement); target.add(targetElement);
} }
} }

View File

@ -45,7 +45,7 @@ final class ArrayToObjectConverter implements ConditionalGenericConverter {
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType, this.conversionService); return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType, this.conversionService);
} }
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) { if (source == null) {
return null; return null;

View File

@ -163,12 +163,15 @@ public class GenericConversionService implements ConfigurableConversionService {
return handleResult(sourceType, targetType, convertNullSource(sourceType, targetType)); return handleResult(sourceType, targetType, convertNullSource(sourceType, targetType));
} }
if (source != null && !sourceType.getObjectType().isInstance(source)) { if (source != null && !sourceType.getObjectType().isInstance(source)) {
throw new IllegalArgumentException("The source to convert from must be an instance of " + sourceType + "; instead it was a " + source.getClass().getName()); throw new IllegalArgumentException("The source to convert from must be an instance of " +
sourceType + "; instead it was a " + source.getClass().getName());
} }
GenericConverter converter = getConverter(sourceType, targetType); GenericConverter converter = getConverter(sourceType, targetType);
if (converter != null) { if (converter != null) {
return handleResult(sourceType, targetType, ConversionUtils.invokeConverter(converter, source, sourceType, targetType)); Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType);
} else { return handleResult(sourceType, targetType, result);
}
else {
return handleConverterNotFound(source, sourceType, targetType); return handleConverterNotFound(source, sourceType, targetType);
} }
} }
@ -235,7 +238,7 @@ public class GenericConversionService implements ConfigurableConversionService {
ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType); ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType);
GenericConverter converter = this.converterCache.get(key); GenericConverter converter = this.converterCache.get(key);
if (converter != null) { if (converter != null) {
return converter != NO_MATCH ? converter : null; return (converter != NO_MATCH ? converter : null);
} }
else { else {
converter = findConverterForClassPair(sourceType, targetType); converter = findConverterForClassPair(sourceType, targetType);
@ -335,7 +338,8 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
} }
return null; return null;
} else { }
else {
HashSet<Class<?>> interfaces = new LinkedHashSet<Class<?>>(); HashSet<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>(); LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
classQueue.addFirst(sourceObjectType); classQueue.addFirst(sourceObjectType);
@ -393,7 +397,8 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
} }
return matchConverter(converters.get(Object.class), sourceType, targetType); return matchConverter(converters.get(Object.class), sourceType, targetType);
} else if (targetObjectType.isArray()) { }
else if (targetObjectType.isArray()) {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>(); LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
classQueue.addFirst(targetObjectType); classQueue.addFirst(targetObjectType);
while (!classQueue.isEmpty()) { while (!classQueue.isEmpty()) {
@ -463,7 +468,7 @@ public class GenericConversionService implements ConfigurableConversionService {
assertNotPrimitiveTargetType(sourceType, targetType); assertNotPrimitiveTargetType(sourceType, targetType);
return source; return source;
} }
else if (sourceType.isAssignableTo(targetType)) { else if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) {
return source; return source;
} }
else { else {
@ -484,6 +489,7 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private final class ConverterAdapter implements GenericConverter { private final class ConverterAdapter implements GenericConverter {
@ -515,7 +521,6 @@ public class GenericConversionService implements ConfigurableConversionService {
return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() + return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() +
" : " + this.converter.toString(); " : " + this.converter.toString();
} }
} }

View File

@ -200,7 +200,7 @@ public class CollectionToCollectionConverterTests {
@Test(expected=ConverterNotFoundException.class) @Test(expected=ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception { public void elementTypesNotConvertible() throws Exception {
List<Resource> resources = new ArrayList<Resource>(); List<String> resources = new ArrayList<String>();
resources.add(null); resources.add(null);
resources.add(null); resources.add(null);
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings")); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));

View File

@ -1,10 +1,20 @@
package org.springframework.core.convert.support; /*
* Copyright 2002-2011 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.
*/
import static org.junit.Assert.assertEquals; package org.springframework.core.convert.support;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -14,10 +24,13 @@ import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import static org.junit.Assert.*;
public class MapToMapConverterTests { public class MapToMapConverterTests {
private GenericConversionService conversionService = new GenericConversionService(); private GenericConversionService conversionService = new GenericConversionService();