mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add test
This commit is contained in:
parent
179cd2c8ba
commit
646815c3de
@ -8,6 +8,7 @@ import cn.hutool.core.util.XmlUtil;
|
|||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.sql.Blob;
|
import java.sql.Blob;
|
||||||
import java.sql.Clob;
|
import java.sql.Clob;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@ -31,6 +32,8 @@ public class StringConverter extends AbstractConverter<String> {
|
|||||||
return clobToStr((Clob) value);
|
return clobToStr((Clob) value);
|
||||||
} else if (value instanceof Blob) {
|
} else if (value instanceof Blob) {
|
||||||
return blobToStr((Blob) value);
|
return blobToStr((Blob) value);
|
||||||
|
} else if (value instanceof Type) {
|
||||||
|
return ((Type) value).getTypeName();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 其它情况
|
// 其它情况
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.lang.reflect;
|
package cn.hutool.core.lang.reflect;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.lang.SimpleCache;
|
import cn.hutool.core.lang.SimpleCache;
|
||||||
import cn.hutool.core.util.TypeUtil;
|
import cn.hutool.core.util.TypeUtil;
|
||||||
|
|
||||||
@ -29,6 +30,17 @@ public class ActualTypeMapperPool {
|
|||||||
return CACHE.get(type, () -> createTypeMap(type));
|
return CACHE.get(type, () -> createTypeMap(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取泛型变量名(字符串)和泛型实际类型的对应关系Map
|
||||||
|
*
|
||||||
|
* @param type 被解析的包含泛型参数的类
|
||||||
|
* @return 泛型对应关系Map
|
||||||
|
* @since 5.7.16
|
||||||
|
*/
|
||||||
|
public static Map<String, Type> getStrKeyMap(Type type){
|
||||||
|
return Convert.toMap(String.class, Type.class, get(type));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
|
* 获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
|
||||||
*
|
*
|
||||||
@ -89,8 +101,13 @@ public class ActualTypeMapperPool {
|
|||||||
final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
|
final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
|
||||||
final Type[] typeParameters = rawType.getTypeParameters();
|
final Type[] typeParameters = rawType.getTypeParameters();
|
||||||
|
|
||||||
|
Type value;
|
||||||
for (int i = 0; i < typeParameters.length; i++) {
|
for (int i = 0; i < typeParameters.length; i++) {
|
||||||
typeMap.put(typeParameters[i], typeArguments[i]);
|
value = typeArguments[i];
|
||||||
|
// 跳过泛型变量对应泛型变量的情况
|
||||||
|
if(false == value instanceof TypeVariable){
|
||||||
|
typeMap.put(typeParameters[i], value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type = rawType;
|
type = rawType;
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package cn.hutool.core.lang.reflect;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 见:https://gitee.com/dromara/hutool/pulls/447/files
|
||||||
|
*
|
||||||
|
* TODO 同时继承泛型和实现泛型接口需要解析,此处为F
|
||||||
|
*/
|
||||||
|
public class ActualTypeMapperPoolTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTypeArgumentTest(){
|
||||||
|
final Map<Type, Type> typeTypeMap = ActualTypeMapperPool.get(FinalClass.class);
|
||||||
|
typeTypeMap.forEach((key, value)->{
|
||||||
|
if("A".equals(key.getTypeName())){
|
||||||
|
Assert.assertEquals(Character.class, value);
|
||||||
|
} else if("B".equals(key.getTypeName())){
|
||||||
|
Assert.assertEquals(Boolean.class, value);
|
||||||
|
} else if("C".equals(key.getTypeName())){
|
||||||
|
Assert.assertEquals(String.class, value);
|
||||||
|
} else if("D".equals(key.getTypeName())){
|
||||||
|
Assert.assertEquals(Double.class, value);
|
||||||
|
} else if("E".equals(key.getTypeName())){
|
||||||
|
Assert.assertEquals(Integer.class, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTypeArgumentStrKeyTest(){
|
||||||
|
final Map<String, Type> typeTypeMap = ActualTypeMapperPool.getStrKeyMap(FinalClass.class);
|
||||||
|
typeTypeMap.forEach((key, value)->{
|
||||||
|
if("A".equals(key)){
|
||||||
|
Assert.assertEquals(Character.class, value);
|
||||||
|
} else if("B".equals(key)){
|
||||||
|
Assert.assertEquals(Boolean.class, value);
|
||||||
|
} else if("C".equals(key)){
|
||||||
|
Assert.assertEquals(String.class, value);
|
||||||
|
} else if("D".equals(key)){
|
||||||
|
Assert.assertEquals(Double.class, value);
|
||||||
|
} else if("E".equals(key)){
|
||||||
|
Assert.assertEquals(Integer.class, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface BaseInterface<A, B, C> {}
|
||||||
|
public interface FirstInterface<A, B, D, E> extends BaseInterface<A, B, String> {}
|
||||||
|
public interface SecondInterface<A, B, F> extends BaseInterface<A, B, String> {}
|
||||||
|
|
||||||
|
public static class BaseClass<A, D> implements FirstInterface<A, Boolean, D, Integer> {}
|
||||||
|
public static class FirstClass extends BaseClass<Character, Double> implements SecondInterface<Character, Boolean, FirstClass> {}
|
||||||
|
public static class SecondClass extends FirstClass {}
|
||||||
|
public static class FinalClass extends SecondClass {}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user