反射获取声明泛型工具类

简介: 反射获取声明泛型工具类

和上帝一样聪明,和天才一样幼稚。——巴尔扎克《奥诺丽纳》

工具类:

public static Type[] getGenericTypes(Type paramType) {
    Type type;
    for (type = paramType; type instanceof Class;
         type = ((Class<?>) type).getGenericSuperclass()) {
        if (Object.class.equals(type)) {
            Type[] genericInterfaces = ((Class<?>) type).getGenericInterfaces();
            if (genericInterfaces.length > 0 && Objects.nonNull(genericInterfaces[0])) {
                type = genericInterfaces[0];
            }
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType ty = (ParameterizedType) type;
        return ty.getActualTypeArguments();
    }
    return new Type[0];
}


public static Type[] getGenericTypes(Type paramType) {
    Type type;
    for (type = paramType; type instanceof Class;
         type = ((Class<?>) type).getGenericSuperclass()) {
        if (Object.class.equals(type)) {
            Type[] genericInterfaces = ((Class<?>) type).getGenericInterfaces();
            if (genericInterfaces.length > 0 && Objects.nonNull(genericInterfaces[0])) {
                type = genericInterfaces[0];
            }
        }
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType ty = (ParameterizedType) type;
        return ty.getActualTypeArguments();
    }
    return new Type[0];
}

使用方式:

@Test
void testGetGenericTypes() {
    class StringArrayList extends ArrayList<String> {
        private static final long serialVersionUID = 5735314375293577082L;
    }
    Type[] stringType = ReflectHelper.getGenericTypes(new TypeReference<String>() {}.getClass());
    Assertions.assertEquals(String.class, stringType[0]);
    Type[] stringArrayListType = ReflectHelper.getGenericTypes(StringArrayList.class);
    Assertions.assertEquals(String.class, stringArrayListType[0]);
    Type[] hashMapType = ReflectHelper.getGenericTypes(new HashMap<String, Object>() {}.getClass());
    Assertions.assertEquals(String.class, hashMapType[0]);
    Assertions.assertEquals(Object.class, hashMapType[1]);
}

TypeReference

package io.github.vampireachao.stream.core.reflect;
import java.lang.reflect.Type;
/**
 * 单个泛型类型
 *
 * @author VampireAchao
 * @since 2022/6/2 18:53
 */
public abstract class TypeReference<T> implements Type {
    /**
     * Returns a string describing this type, including information
     * about any type parameters.
     *
     * @return a string describing this type
     * @implSpec The default implementation calls {@code toString}.
     * @since 1.8
     */
    @Override
    public String getTypeName() {
        return ReflectHelper.getGenericTypes(this.getClass())[0].getTypeName();
    }
    public Type getType() {
        return ReflectHelper.getGenericTypes(this.getClass())[0];
    }
}

完整源码:https://gitee.com/VampireAchao/stream-query/blob/master/stream-core/src/main/java/io/github/vampireachao/stream/core/reflect/ReflectHelper.java

相关文章
|
7月前
|
Java 编译器
Java反射操作泛型
Java反射操作泛型
38 0
|
8月前
|
设计模式 Java
Java反射(Class、反射实例化、反射与单例、获取类结构)附带相关面试题
1.了解反射,2.Class类的三种实例化方法,3.反射机制与对象实例化,4.反射与单例设计模式,5.通过反射获取类结构的信息
203 0
|
4月前
|
Java
【反射】Java反射机制 -- 常用构造器与方法
【反射】Java反射机制 -- 常用构造器与方法
28 0
|
8月前
|
Java API
Java反射(通过反射获取构造函数、方法、属性)
1.通过反射获取构造函数,2.通过反射获取方法,3.通过反射调用成员属性
81 0
|
9月前
|
编译器
反射、注解和泛型相关的坑
泛型 类型擦除 桥接方法
|
9月前
|
编译器
反射、注解和泛型相关的坑
根据反射来获取方法,很多人觉得获取方法传入参数就可以了,但是遇到方法的重载的时候,怎么能够知道此次执行走的是哪个方法呢?
|
12月前
|
XML 并行计算 安全
【Java基础】泛型+反射+枚举+Lambda表达式 知识点总结
本文重点介绍Java基础:泛型、反射、枚举、Lambda表达式知识点总结。
250 0
|
Java
获取java泛型类中的泛型类型
实现java中获取泛型类中的泛型类型的方法
230 0
|
XML 并行计算 安全
【Java】反射, 枚举,Lambda表达式
【Java】反射, 枚举,Lambda表达式
111 0
【Java】反射, 枚举,Lambda表达式
|
Java
Java枚举类(1)--枚举类的定义、方法使用和接口实现
Java枚举类(1)--枚举类的定义、方法使用和接口实现
314 0
Java枚举类(1)--枚举类的定义、方法使用和接口实现