Check the super class and interfaces of the source and target,
attempting to find a match. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@908698 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -92,6 +92,10 @@ public class Converters {
|
||||
return UtilGenerics.cast(value);
|
||||
}
|
||||
}
|
||||
result = checkExtendsImplements(sourceClass, targetClass);
|
||||
if (result != null) {
|
||||
return UtilGenerics.cast(result);
|
||||
}
|
||||
// Null converter must be checked last
|
||||
if (nullConverter.canConvert(sourceClass, targetClass)) {
|
||||
Converter passThruConverter = new PassThruConverter<S>(sourceClass);
|
||||
@@ -110,6 +114,37 @@ public class Converters {
|
||||
return UtilGenerics.cast(result);
|
||||
}
|
||||
|
||||
private static <S, T> Converter<S, T> checkExtendsImplements(Class<S> sourceClass, Class<T> targetClass) throws ClassNotFoundException {
|
||||
if (targetClass == null || sourceClass == null) {
|
||||
return null;
|
||||
}
|
||||
String key = sourceClass.getName().concat(DELIMITER).concat(targetClass.getName());
|
||||
Converter<?, ?> result = converterMap.get(key);
|
||||
if (result == null) {
|
||||
result = checkExtendsImplements(sourceClass, targetClass.getSuperclass());
|
||||
if (result == null) {
|
||||
for (Class<?> intf: targetClass.getInterfaces()) {
|
||||
result = checkExtendsImplements(sourceClass, intf);
|
||||
if (result != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (result == null) {
|
||||
result = checkExtendsImplements(sourceClass.getSuperclass(), targetClass);
|
||||
if (result == null) {
|
||||
for (Class<?> intf: sourceClass.getInterfaces()) {
|
||||
result = checkExtendsImplements(intf, targetClass);
|
||||
if (result != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return UtilGenerics.cast(result);
|
||||
}
|
||||
|
||||
/** Load all classes that implement <code>Converter</code> and are
|
||||
* contained in <code>containerClass</code>.
|
||||
*
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.ofbiz.base.conversion.test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -35,6 +36,7 @@ import org.ofbiz.base.conversion.Converter;
|
||||
import org.ofbiz.base.conversion.Converters;
|
||||
import org.ofbiz.base.test.GenericTestCaseBase;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
import org.ofbiz.base.util.collections.LRUMap;
|
||||
|
||||
public class MiscTests extends GenericTestCaseBase {
|
||||
|
||||
@@ -42,6 +44,24 @@ public class MiscTests extends GenericTestCaseBase {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void testExtendsImplements() throws Exception {
|
||||
List<String> arraysList = Arrays.asList("a", "b", "c");
|
||||
Converter converter = Converters.getConverter(arraysList.getClass(), String.class);
|
||||
assertEquals("", "[a, b, c]", converter.convert(arraysList));
|
||||
Exception caught = null;
|
||||
try {
|
||||
Converters.getConverter(MiscTests.class, String.class);
|
||||
} catch (ClassNotFoundException e) {
|
||||
caught = e;
|
||||
} finally {
|
||||
assertNotNull("ClassNotFoundException thrown for MiscTests.class", caught);
|
||||
}
|
||||
LRUMap<String, String> map = new LRUMap<String, String>();
|
||||
map.put("a", "1");
|
||||
converter = Converters.getConverter(LRUMap.class, String.class);
|
||||
assertEquals("", "{a=1}", converter.convert(map));
|
||||
}
|
||||
|
||||
public void testPassthru() throws Exception {
|
||||
String string = "ofbiz";
|
||||
BigDecimal bigDecimal = new BigDecimal("1.234");
|
||||
|
||||
Reference in New Issue
Block a user