由于 Java 的泛型在运行时会被擦除,不能够直接获取泛型的类型,但是其实在 class 字节码中还是保存着泛型的信息,可以通过特殊的方式获取到泛型的类型
- 获取父类中的泛型类型
/**
* 定义一个抽象的父类
* 获取父类中的泛型类型 T
*/
public abstract class SuperClass<T> {
// 泛型类型
private Class<T> clazz;
public SuperClass() {
super();
// 根据实现类反射获取包含泛型的父类,然后获取泛型的类型
this.clazz = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getClazz() {
return this.clazz;
}
public static void main(String[] args) {
// 构造匿名子类
SuperClass<String> superClassString = new SuperClass<String>(){};
System.out.println(superClassString.getClazz()); // class java.lang.String
// 构造匿名子类
SuperClass<Entity> superClassEntity = new SuperClass<Entity>(){};
System.out.println(superClassEntity.getClazz()); // class Entity
}
}
- 获取父接口中的泛型类型
public interface SuperInterface<T> {
@SuppressWarnings("unchecked")
default Class<T> getEntityClass() {
Type[] types = getClass().getGenericInterfaces();
for (Type type : types) {
if (type.getTypeName().startsWith(SuperInterface.class.getName())) {
ParameterizedType pt = (ParameterizedType) type;
return (Class<T>)pt.getActualTypeArguments()[0];
}
}
return null;
}
}
public class Children implements SuperInterface<Entity> {
public static void main() {
Children children = new Children();
System.out.println(children.getEntityClass()); // class Entity
}
}
- 获取父类或接口中的指定位置的泛型类型
public class SystemUtils {
/**
* 根据子类获取父接口指定的泛型类型,未找到指定泛型时将返回空
*
* @param subClass 子类类型
* @param targetInterfaceClass 父接口类型
* @param genericIndex 泛型下标
* @return 泛型类型
*/
public static Class<?> getInterfaceGenericClass(Class<?> subClass, Class<?> targetInterfaceClass, int genericIndex) {
if (subClass == null || targetInterfaceClass == null || genericIndex < 0) {
return null;
}
Class<?> clazz = subClass;
while (clazz != null && clazz != Object.class) {
Type[] types = clazz.getGenericInterfaces();
for (Type type : types) {
if (type.getTypeName().contains(targetInterfaceClass.getName())) {
Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
if (genericIndex >= typeArguments.length) {
return null;
}
Type typeArgument = typeArguments[genericIndex];
if (typeArgument instanceof Class) {
return (Class<?>) typeArgument;
}
return null;
}
}
Class<?>[] interfaces = clazz.getInterfaces();
if (interfaces != null) {
for (Class<?> inter : interfaces) {
Class<?> result = getInterfaceGenericClass(inter, targetInterfaceClass, genericIndex);
if (result != null) {
return result;
}
}
}
clazz = clazz.getSuperclass();
}
return null;
}
public static void main(String[] args) {
System.out.println(SystemUtils.getInterfaceGenericClass(Children.class, SuperInterface.class, 0)); // class Entity
}
}
- Entity 对象
/**
* 简单 Java 对象 POJO
*/
@Getter
@Setter
class Entity {
private int id;
private String name;
}