From 33b53b7cca7ec2667d57d320d888c51e48b09ed6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 1 Dec 2011 18:51:36 +0000 Subject: [PATCH] alignment with 3.0.7 backports (SPR-8674) --- .../core/convert/TypeDescriptor.java | 5 ++-- .../support/ArrayToCollectionConverter.java | 6 +++-- .../support/ArrayToObjectConverter.java | 2 +- .../support/GenericConversionService.java | 21 ++++++++++------ .../CollectionToCollectionConverterTests.java | 2 +- .../support/MapToMapConverterTests.java | 25 ++++++++++++++----- 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index e2c32922b9..b8dd4b9695 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -241,7 +241,8 @@ public class TypeDescriptor { if (value == null) { 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); } 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); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java index f35faacb48..301c563655 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java @@ -49,7 +49,8 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter { } 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") @@ -68,7 +69,8 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter { else { for (int i = 0; i < length; 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); } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java index cfef36fe75..6d66a5ddf7 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java @@ -45,7 +45,7 @@ final class ArrayToObjectConverter implements ConditionalGenericConverter { public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType, this.conversionService); } - + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 6c4ed99e78..95a2ef772c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -163,12 +163,15 @@ public class GenericConversionService implements ConfigurableConversionService { return handleResult(sourceType, targetType, convertNullSource(sourceType, targetType)); } 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); if (converter != null) { - return handleResult(sourceType, targetType, ConversionUtils.invokeConverter(converter, source, sourceType, targetType)); - } else { + Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType); + return handleResult(sourceType, targetType, result); + } + else { return handleConverterNotFound(source, sourceType, targetType); } } @@ -235,7 +238,7 @@ public class GenericConversionService implements ConfigurableConversionService { ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType); GenericConverter converter = this.converterCache.get(key); if (converter != null) { - return converter != NO_MATCH ? converter : null; + return (converter != NO_MATCH ? converter : null); } else { converter = findConverterForClassPair(sourceType, targetType); @@ -335,7 +338,8 @@ public class GenericConversionService implements ConfigurableConversionService { } } return null; - } else { + } + else { HashSet> interfaces = new LinkedHashSet>(); LinkedList> classQueue = new LinkedList>(); classQueue.addFirst(sourceObjectType); @@ -393,7 +397,8 @@ public class GenericConversionService implements ConfigurableConversionService { } } return matchConverter(converters.get(Object.class), sourceType, targetType); - } else if (targetObjectType.isArray()) { + } + else if (targetObjectType.isArray()) { LinkedList> classQueue = new LinkedList>(); classQueue.addFirst(targetObjectType); while (!classQueue.isEmpty()) { @@ -463,7 +468,7 @@ public class GenericConversionService implements ConfigurableConversionService { assertNotPrimitiveTargetType(sourceType, targetType); return source; } - else if (sourceType.isAssignableTo(targetType)) { + else if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) { return source; } else { @@ -484,6 +489,7 @@ public class GenericConversionService implements ConfigurableConversionService { } } + @SuppressWarnings("unchecked") private final class ConverterAdapter implements GenericConverter { @@ -515,7 +521,6 @@ public class GenericConversionService implements ConfigurableConversionService { return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() + " : " + this.converter.toString(); } - } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java index e45c18356d..a69f19f0bc 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java @@ -200,7 +200,7 @@ public class CollectionToCollectionConverterTests { @Test(expected=ConverterNotFoundException.class) public void elementTypesNotConvertible() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList(); resources.add(null); resources.add(null); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings")); diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java index 420da3531a..afb525cb5b 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java @@ -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; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +package org.springframework.core.convert.support; import java.util.Arrays; import java.util.HashMap; @@ -14,10 +24,13 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; + import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; +import static org.junit.Assert.*; + public class MapToMapConverterTests { private GenericConversionService conversionService = new GenericConversionService();