alignment with 3.0.7 backports (SPR-8674)

master
Juergen Hoeller 13 years ago
parent 71e0a506b9
commit 33b53b7cca
  1. 5
      org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  2. 6
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java
  3. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java
  4. 21
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  5. 2
      org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java
  6. 25
      org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.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);
}
}

@ -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);
}
}

@ -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;

@ -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<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
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<Class<?>> classQueue = new LinkedList<Class<?>>();
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();
}
}

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

@ -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();

Loading…
Cancel
Save