java.lang.reflect之TypeVariable ParameterizedType

TypeVariable

TypeVariable是在反射中用来反映泛型类型参数的信息,在Spring的注解开发中会使用到。
下面是方法泛型参数的获取方式。

public class TypeVariableExample<T extends Object> {

    public static void main(String[] args) {
        // 返回类或接口声明的方法,包括: public protected 缺省的 private,但是不包括继承的
        Method[] declaredMethods = TypeVariableExample.class.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if (declaredMethod.getName().equals("genericType")){
                Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    System.out.println("class parameterType: " + parameterType.getName());
                }

                // 获取方法的所有参数
                Parameter[] parameters = declaredMethod.getParameters();
                for (Parameter parameter : parameters) {
                    if (parameter.getAnnotation(RequestBody.class) != null){
                        // 方法参数的类型
                        Type parameterizedType = parameter.getParameterizedType();
                        if (parameterizedType instanceof TypeVariable){
                            System.out.println("is TypeVariable");
                        }
                        if (parameter.getParameterizedType() instanceof ParameterizedType){
                            System.out.println("is ParameterizedType");
                        }

                    }
                }

            }
        }

        // 测试同一个类下的泛型是否相等
        TypeVariableExample<String> stringType = new TypeVariableExample<>();
        TypeVariableExample<User> userType = new TypeVariableExample<>();

        Type stringParameterType = getParameterType(stringType);
        Type userParameterType = getParameterType(userType);
        if (stringParameterType != null && userParameterType != null){
            System.out.println(stringParameterType.equals(userParameterType));
        }

    }

    public void genericType(@RequestBody T t){

    }

    private static Type getParameterType(TypeVariableExample<?> example){
        Method[] methods = example.getClass().getMethods();
        for (Method method : methods) {
            for (Parameter parameter : method.getParameters()) {
                if (parameter.getAnnotation(RequestBody.class) != null){
                    return parameter.getParameterizedType();
                }
            }
        }
        return null;
    }

}

注意:同一个类下的同一泛型参数对象是一样的
在这里插入图片描述

TypeVariable方法

  1. Type[] getBounds();返回此类型的上界
  2. D getGenericDeclaration();返回声明这个泛型声明类型变量的对象
  3. String getName();返回泛型的名称
  4. AnnotatedType[] getAnnotatedBounds();

泛型的使用方式

  1. 泛型类:public class User<T>
  2. 泛型方法:public <T> metho(T t);
  3. 泛型接口:public interface InterfaceTest <T>
  4. <?>无界泛型 <T extends Object>上界泛型为Object,只能是Object或其子类 <? super User> 下界泛型为User,只能是User或User父类型

ParameterizedType

反射获取获取泛型类型的实际参数信息。

public class ParameterizedTypeExample {

    public static void main(String[] args) {
        // 获取所有的公共方法,包括继承的
        Method[] methods = ParameterizedTypeExample.class.getMethods();
        for (Method method : methods) {
            if (method.getName().equals("parameterizedType")){
                Parameter[] parameters = method.getParameters();
                for (Parameter parameter : parameters) {
                    if (parameter.getAnnotation(RequestBody.class) != null){
                        Class<?> type = parameter.getType();
                        System.out.println("parameter type: " + type.getName());

                        // 参数泛型类型判断
                        if (parameter.getParameterizedType() instanceof ParameterizedType){
                            System.out.println("is ParameterizedType");
                        }
                        if (parameter.getParameterizedType() instanceof TypeVariable){
                            System.out.println("is TypeVariable");
                        }
                    }
                }
            }
        }
    }

    public void parameterizedType(@RequestBody List<String> list){

    }
}

在这里插入图片描述

ParameterizedType

  1. Type[] getActualTypeArguments();泛型的实参
  2. Type getRawType();
  3. Type getOwnerType();
  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值