ConversionService is able to apply Converters to interface-based array elements (SPR-7150); a context ConversionService is able to override an ApplicationContext's resource editors (SPR-7079)

This commit is contained in:
Juergen Hoeller
2010-05-26 13:58:37 +00:00
parent 6c6004a93b
commit 1532119787
8 changed files with 158 additions and 36 deletions

View File

@@ -147,7 +147,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
logger.debug("Yes, I can convert");
}
return true;
} else {
}
else {
if (logger.isDebugEnabled()) {
logger.debug("No, I cannot convert");
}
@@ -252,7 +253,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
logger.debug("Matched cached converter " + converter);
}
return converter != NO_MATCH ? converter : null;
} else {
}
else {
converter = findConverterForClassPair(sourceType, targetType);
if (converter != null) {
if (logger.isTraceEnabled()) {
@@ -374,6 +376,9 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (componentType.getSuperclass() != null) {
classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
}
else if (componentType.isInterface()) {
classQueue.addFirst(Object[].class);
}
}
else {
Class<?>[] interfaces = currentClass.getInterfaces();
@@ -441,6 +446,9 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (componentType.getSuperclass() != null) {
classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
}
else if (componentType.isInterface()) {
classQueue.addFirst(Object[].class);
}
}
else {
Class<?>[] interfaces = currentClass.getInterfaces();

View File

@@ -16,13 +16,6 @@
package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -30,7 +23,9 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
@@ -190,6 +185,24 @@ public class GenericConversionServiceTests {
assertEquals("RESULT", converted);
}
@Test
public void testInterfaceArrayToStringArray() {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new MyBaseInterfaceConverter());
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
String[] converted = conversionService.convert(new MyInterface[] {new MyInterfaceImplementer()}, String[].class);
assertEquals("RESULT", converted[0]);
}
@Test
public void testObjectArrayToStringArray() {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new MyBaseInterfaceConverter());
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
String[] converted = conversionService.convert(new MyInterfaceImplementer[] {new MyInterfaceImplementer()}, String[].class);
assertEquals("RESULT", converted[0]);
}
@Test
public void testWildcardMap() throws Exception {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();