Spirng-泛型处理

在这里插入图片描述

public class GenericDemo {
    public static void main(String[] args) {
        // 原生类型 int long float
        Class<Integer> integerClass = int.class;
        // 数组类型
        Class<Object[]> aClass = Object[].class;
        // 原始类型
        Class<String> stringClass = String.class;
        // 泛型参数类型 AbstractList<E>
        ParameterizedType paramType = (ParameterizedType) ArrayList.class.getGenericSuperclass();
        System.out.println(paramType);
        // 类型变量 <E>
        Type[] types = paramType.getActualTypeArguments();
        Arrays.stream(paramType.getActualTypeArguments()).forEach(System.out::println);
    }
}

Spring中的泛型辅助类

在这里插入图片描述
案例演示:

public class GenericTypeResolverDemo {
    public static void main(String[] args) throws NoSuchMethodException {
        // 这里表示接口是Comparable 但是我们所以其实具体化的类就是String
        displayReturnTypeGenericInfo(GenericTypeResolverDemo.class, Comparable.class, "getString");
        displayReturnTypeGenericInfo(GenericTypeResolverDemo.class, List.class,"getList");
        displayReturnTypeGenericInfo(GenericTypeResolverDemo.class, List.class, "getStringList");
        displayReturnTypeGenericInfo(GenericTypeResolverDemo.class, List.class, "getObjectList");
        // 没有具体化的时候获取的就是 泛型变量 这里打印的就是 String , E
		Map<TypeVariable, Type> typeVariableMap = GenericTypeResolver.getTypeVariableMap(StringList.class);
        System.out.println(typeVariableMap);
    }
    public static <E> List<E> getList() {
        return null;
    }
    public static StringList getStringList() {
        return null;
    }
    public static String getString() {
        return null;
    }
    // 这里也是泛型参数具体化 会打印返回类型
    public static ArrayList<Object> getObjectList() {
        return null;
    }
    // 泛型参数具体化以后才会返回 才在字节码中有记录
    static class StringList extends ArrayList<String> {

    }
    private static void displayReturnTypeGenericInfo(Class<?> containingClass,Class<?> genericItf, String methodName, Class... argumentsTypes) throws NoSuchMethodException {
        Method method = containingClass.getMethod(methodName, argumentsTypes);
        Class<?> returnClass = GenericTypeResolver.resolveReturnType(method, containingClass);
        // String
        System.out.println(String.format("GenericTypeResolver.resolvReturnType %s, %s = %s", methodName, containingClass.getSimpleName(), returnClass));
        // 常规类型不具备泛型参数类型 泛型参数具体化以后才有返回
        System.out.println(String.format("GenericTypeResolver.resolveReturnTypeArgument %s, %s = %s",methodName, containingClass.getSimpleName(), GenericTypeResolver.resolveReturnTypeArgument(method, genericItf)));
        System.out.println("====>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}

在这里插入图片描述

 public static void main(String[] args) throws NoSuchMethodException {
        ResolvableType type = ResolvableType.forMethodReturnType(GenericCollectionResolverDemo.class.getMethod("getString"));
        System.out.println(type);
    }

在这里插入图片描述
在这里插入图片描述

// 获取泛型参数
ResolvableType generic = type.getSuperType().getGeneric(0);
System.out.println(generic);
// 原生类型
Class<?> resolve = type.resolve();

System.out.println(resolve);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值