This commit is contained in:
Looly
2024-09-22 18:15:39 +08:00
parent 43a0201a20
commit ff187f6fde
44 changed files with 831 additions and 664 deletions

View File

@@ -96,6 +96,7 @@ public class Pair<L, R> implements Serializable, Cloneable {
if (o == null || getClass() != o.getClass()) {
return false;
}
final Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(left, pair.left) && Objects.equals(right, pair.right);
}

View File

@@ -92,7 +92,7 @@ public class ActualTypeMapperPool {
}
/**
* 创建类中所有的泛型变量和泛型实际类型的对应关系Map
* 创建类中所有的泛型变量和泛型实际类型的对应关系Map<br>
*
* @param type 被解析的包含泛型参数的类
* @return 泛型对应关系Map

View File

@@ -22,10 +22,10 @@ import java.lang.reflect.Type;
* Type类型参考<br>
* 通过构建一个类型参考子类可以获取其泛型参数中的Type类型。例如
*
* <pre>
* TypeReference&lt;List&lt;String&gt;&gt; list = new TypeReference&lt;List&lt;String&gt;&gt;() {};
* Type t = tr.getType();
* </pre>
* <pre>{@code
* TypeReference<List<String>> list = new TypeReference<List<String>>() {};
* Type t = tr.getType();
* }</pre>
*
* 此类无法应用于通配符泛型参数wildcard parameters比如{@code Class<?>} 或者 {@code List? extends CharSequence>}
*

View File

@@ -45,29 +45,32 @@ public class TypeUtil {
* @return 原始类,如果无法获取原始类,返回{@code null}
*/
public static Class<?> getClass(final Type type) {
if (null != type) {
if (type instanceof Class) {
return (Class<?>) type;
} else if (type instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) type).getRawType();
} else if (type instanceof TypeVariable) {
//return (Class<?>) ((TypeVariable<?>) type).getBounds()[0];
final Type[] bounds = ((TypeVariable<?>) type).getBounds();
if (bounds.length == 1) {
return getClass(bounds[0]);
}
} else if (type instanceof WildcardType) {
final Type[] upperBounds = ((WildcardType) type).getUpperBounds();
if (upperBounds.length == 1) {
return getClass(upperBounds[0]);
}
} else if(type instanceof GenericArrayType){
return Array.newInstance(getClass(((GenericArrayType)type).getGenericComponentType()), 0).getClass();
} else if(type instanceof TypeReference){
return getClass(((TypeReference<?>)type).getType());
}
if (null == type) {
return null;
}
return null;
if (type instanceof Class) {
return (Class<?>) type;
} else if (type instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) type).getRawType();
} else if (type instanceof TypeVariable) {
//return (Class<?>) ((TypeVariable<?>) type).getBounds()[0];
final Type[] bounds = ((TypeVariable<?>) type).getBounds();
if (bounds.length == 1) {
return getClass(bounds[0]);
}
} else if (type instanceof WildcardType) {
final Type[] upperBounds = ((WildcardType) type).getUpperBounds();
if (upperBounds.length == 1) {
return getClass(upperBounds[0]);
}
} else if (type instanceof GenericArrayType) {
return Array.newInstance(getClass(((GenericArrayType) type).getGenericComponentType()), 0).getClass();
} else if (type instanceof TypeReference) {
return getClass(((TypeReference<?>) type).getType());
}
throw new IllegalArgumentException("Unsupported Type: " + type.getClass().getName());
}
/**
@@ -107,7 +110,7 @@ public class TypeUtil {
return null == field ? null : field.getType();
}
// ----------------------------------------------------------------------------------- Param Type
// region ----- Param Type
/**
* 获取方法的第一个参数类型<br>
@@ -190,8 +193,9 @@ public class TypeUtil {
public static Class<?>[] getParamClasses(final Method method) {
return null == method ? null : method.getParameterTypes();
}
// endregion
// ----------------------------------------------------------------------------------- Return Type
// region ----- Return Type
/**
* 获取方法的返回值类型<br>
@@ -218,8 +222,9 @@ public class TypeUtil {
public static Class<?> getReturnClass(final Method method) {
return null == method ? null : method.getReturnType();
}
// endregion
// ----------------------------------------------------------------------------------- Type Argument
// region ----- Type Argument
/**
* 获得给定类的第一个泛型参数
@@ -267,6 +272,7 @@ public class TypeUtil {
final ParameterizedType parameterizedType = toParameterizedType(type);
return (null == parameterizedType) ? null : parameterizedType.getActualTypeArguments();
}
// endregion
/**
* 将{@link Type} 转换为{@link ParameterizedType}<br>
@@ -347,7 +353,7 @@ public class TypeUtil {
if (ArrayUtil.isNotEmpty(genericInterfaces)) {
for (final Type genericInterface : genericInterfaces) {
final ParameterizedType parameterizedType = toParameterizedType(genericInterface);
if(null != parameterizedType){
if (null != parameterizedType) {
result.add(parameterizedType);
}
}
@@ -383,20 +389,7 @@ public class TypeUtil {
return false;
}
/**
* 获取泛型变量和泛型实际类型的对应关系Map例如
*
* <pre>
* T org.dromara.hutool.test.User
* E java.lang.Integer
* </pre>
*
* @param clazz 被解析的包含泛型参数的类
* @return 泛型对应关系Map
*/
public static Map<Type, Type> getTypeMap(final Class<?> clazz) {
return ActualTypeMapperPool.get(clazz);
}
// region ----- Actual type
/**
* 获得泛型字段对应的泛型实际类型如果此变量没有对应的实际类型返回null
@@ -417,7 +410,7 @@ public class TypeUtil {
* 此方法可以处理:
*
* <pre>
* 1. 泛型化对象,类似于Map&lt;User, Key&lt;Long&gt;&gt;
* 1. 泛型化对象,类似于{@code Map<User, Key<Long>>}
* 2. 泛型变量类似于T
* </pre>
*
@@ -431,17 +424,30 @@ public class TypeUtil {
}
if (typeVariable instanceof TypeVariable) {
// TODO TypeReference无效
return ActualTypeMapperPool.getActualType(type, (TypeVariable<?>) typeVariable);
return getActualType(type, (TypeVariable<?>) typeVariable);
}
// 没有需要替换的泛型变量,原样输出
return typeVariable;
}
/**
* 获得泛型变量对应的泛型实际类型如果此变量没有对应的实际类型返回typeVariable
*
* @param type 类
* @param typeVariable 泛型变量
* @return 实际类型可能为Class等
*/
public static Type getActualType(final Type type, final TypeVariable<?> typeVariable) {
return ObjUtil.defaultIfNull(ActualTypeMapperPool.getActualType(type, typeVariable), typeVariable);
}
/**
* 获得泛型变量对应的泛型实际类型如果此变量没有对应的实际类型返回null
* 此方法可以处理复杂的泛型化对象,类似于Map&lt;User, Key&lt;Long&gt;&gt;
* 此方法可以处理复杂的泛型化对象,类似于
* <pre>{@code
* Map<User, Key<Long>>
* }</pre>
*
* @param type 类
* @param parameterizedType 泛型变量例如List&lt;T&gt;等
@@ -473,4 +479,20 @@ public class TypeUtil {
public static Type[] getActualTypes(final Type type, final Type... typeVariables) {
return ActualTypeMapperPool.getActualTypes(type, typeVariables);
}
/**
* 获取泛型变量和泛型实际类型的对应关系Map例如
*
* <pre>
* T org.dromara.hutool.test.User
* E java.lang.Integer
* </pre>
*
* @param clazz 被解析的包含泛型参数的类
* @return 泛型对应关系Map
*/
public static Map<Type, Type> getTypeMap(final Class<?> clazz) {
return ActualTypeMapperPool.get(clazz);
}
// endregion
}

View File

@@ -28,6 +28,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/
public class DefaultRegexDateParserTest {
@Test
void parseYearMonthDaySplitByDashedWithTTest() {
assertParse("2021-03-17 06:31:33", "2021-03-17T06:31:33");
assertParse("2021-03-17 06:31:33", "2021-03-17T06:31:33.999");
assertParse("2021-03-17 06:31:33", "2021-03-17T06:31:33.9999");
}
@Test
void parseYearMonthDaySplitByDashedTest() {
assertParse("2013-02-03 00:00:00", "2013-Feb-03");