fix TypeReference not fit Type

This commit is contained in:
Looly
2019-08-16 22:19:03 +08:00
parent a91686a7e1
commit e42c0216db
6 changed files with 99 additions and 1 deletions

View File

@@ -51,6 +51,7 @@ import cn.hutool.core.convert.impl.URIConverter;
import cn.hutool.core.convert.impl.URLConverter;
import cn.hutool.core.convert.impl.UUIDConverter;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
@@ -199,6 +200,10 @@ public class ConverterRegistry implements Serializable{
type = defaultValue.getClass();
}
if(type instanceof TypeReference) {
type = ((TypeReference<?>)type).getType();
}
// 标准转换器
final Converter<T> converter = getConverter(type, isCustomFirst);
if (null != converter) {

View File

@@ -7,6 +7,7 @@ import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
/**
* 类型转换工具单元测试
@@ -26,13 +27,15 @@ public class ConvertTest {
public void toStrTest() {
int a = 1;
long[] b = { 1, 2, 3, 4, 5 };
Console.log(Convert.convert(String.class, b));
String aStr = Convert.toStr(a);
Assert.assertEquals("1", aStr);
String bStr = Convert.toStr(b);
Assert.assertEquals("[1, 2, 3, 4, 5]", Convert.toStr(bStr));
}
@Test
public void toStrTest2() {
String result = Convert.convert(String.class, "aaaa");