java 泛型 反射

 一家店不要说去一一回答,就算试着去读一读所有的攻击,我想这家店还不如关门去做其他生意。我尽我所能也尽我所知道的最好的办法去做,这样,即使别人再花费十倍的力气来说我做得不够好或是做错了,也是毫无用处的。——林肯

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 泛型反射
 * 
 * @author ningdalong
 *
 */
public class GenericReflect extends ArrayList<Long> {

    private static final long serialVersionUID = 1L;
    public List<String> a;

    public List<Integer> b(List<BigDecimal> a, Integer b) throws A {
        // 参数反射
        Type parameterType = a.getClass().getGenericSuperclass();
        sys((ParameterizedType) parameterType);

        // 本地变量反射
        List<Integer> ints = new ArrayList<Integer>();
        Type localType = ints.getClass().getGenericSuperclass();
        sys((ParameterizedType) localType);
        return ints;
    }

    public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
        // 一下为声明解析都可以调用
        extend(GenericReflect.class);
        methodException(GenericReflect.class, "b", List.class, Integer.class);
        methodReturn(GenericReflect.class, "b", List.class, Integer.class);
        methodParameter(GenericReflect.class, "b", List.class, Integer.class);
        field(GenericReflect.class, "a");

        // 一下是运行时无法解析到
        new GenericReflect().b(new ArrayList<BigDecimal>(), 1);
    }

    /**
     * 类的父类泛型
     * 
     * @param clazz
     */
    public static void extend(Class<?> clazz) {
        sys((ParameterizedType) clazz.getGenericSuperclass());
    }

    /**
     * 异常的泛型 类的接口泛型
     * 
     * @param clazz
     * @param methodName
     * @param parameterTypes
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static void methodException(Class<?> clazz, String methodName, Class<?>... parameterTypes)
            throws NoSuchMethodException, SecurityException {
        Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
        Type type = method.getGenericExceptionTypes()[0];
        // 样例中的第一个异常
        Class<?> cl = (Class<?>) type;
        // 实现的第一个接口
        sys((ParameterizedType) cl.getGenericInterfaces()[0]);
    }

    /**
     * 方法返回值的泛型
     * 
     * @param clazz
     * @param methodName
     * @param parameterTypes
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static void methodReturn(Class<?> clazz, String methodName, Class<?>... parameterTypes)
            throws NoSuchMethodException, SecurityException {
        Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
        sys((ParameterizedType) method.getGenericReturnType());
    }

    /**
     * 方法参数的泛型
     * 
     * @param clazz
     * @param methodName
     * @param parameterTypes
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static void methodParameter(Class<?> clazz, String methodName, Class<?>... parameterTypes)
            throws NoSuchMethodException, SecurityException {
        Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
        // 样例中的第一个参数
        sys((ParameterizedType) method.getGenericParameterTypes()[0]);
    }

    /**
     * 字段的泛型
     * 
     * @param clazz
     * @param fieldName
     * @throws NoSuchFieldException
     * @throws SecurityException
     */
    public static void field(Class<?> clazz, String fieldName) throws NoSuchFieldException, SecurityException {
        Field field = clazz.getDeclaredField(fieldName);
        sys((ParameterizedType) field.getGenericType());
    }

    public static void sys(ParameterizedType type) {
        Type[] actualTypeArguments = type.getActualTypeArguments();
        for (Type t : actualTypeArguments) {
            if (t instanceof TypeVariable) {
                System.out.println(((TypeVariable<?>) t).getName());
            } else if (t instanceof Class) {
                System.out.println(((Class<?>) t).getName());
            } else {
                System.out.println("没有匹配");
            }

        }
    }

}

class A extends RuntimeException implements Iterable<GenericReflect> {
    private static final long serialVersionUID = 1L;

    @Override
    public Iterator<GenericReflect> iterator() {
        return null;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值