java8范型测试代码


import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collection;
import java.util.List;

public class View
{
    public Collection<String> c;

    public T<Integer> tt;

    public static void main(String[] args) throws NoSuchFieldException, SecurityException
    {
        T<Integer> t = new T();
        TypeVariable<?>[] cs = t.getClass().getTypeParameters();
        for (TypeVariable<?> c : cs)
        {
            // c instanceof ParameterizedType
            // 必须为false,class上不存在实际类型,只存在声明的类型,所以是TypeVariable
            System.out.println(c + " " + (c instanceof ParameterizedType) + "  " + c.getGenericDeclaration());
        }
        Type type = View.class.getDeclaredField("tt").getGenericType();
        printType(type);
        printType(T2.class.getGenericSuperclass());
        type = T.class.getDeclaredField("o").getGenericType();
        printType(type);
        Type types [] = T.class.getMethods()[0].getGenericParameterTypes();
        for(Type ty:types)
            printType(ty);
    }

    static void printType(Type type)
    {
        System.out.println("-----------------------------------------------");
        if (ParameterizedType.class.isAssignableFrom(type.getClass()))
        {
            ParameterizedType pt = (ParameterizedType) type;
            System.out.println("is ParameterizedType...........");
            System.out.println("typeName:" + pt.getTypeName() + "\nownerType:" + pt.getOwnerType() + "\nrawType:"
                    + pt.getRawType());
            Type[] typs = pt.getActualTypeArguments();
            for (Type tttt : typs)
                System.out.println(" getActualTypeArguments "+tttt.getTypeName());
        }
        else if (TypeVariable.class.isAssignableFrom(type.getClass()))
        {
            System.out.println("not ParameterizedType  that is TypeVariable");
            TypeVariable t = (TypeVariable) type;
            System.out.println("getGenericDeclaration " + t.getGenericDeclaration());
            System.out.println("name: " + t.getName());
            System.out.println("bounds:" + t.getBounds()[0].getTypeName());
        }
        else if(GenericArrayType.class.isAssignableFrom(type.getClass())) 
        {
            System.out.println("GenericArrayType...");
            GenericArrayType genericArrayType = (GenericArrayType)type;
            printType(genericArrayType.getGenericComponentType());
        }
        else 
        {
            System.out.println("unknow....."+type.getTypeName());
        }
    }

    public static class T2<O> extends T<O>
    {
    }

    public static class T<O>
    {
        O o;

        public void show(List<String>[] pTypeArray, O[] vTypeArray)
        {
        }
    }

}

优质内容筛选与推荐>>
1、byte为什么要与上0xff?
2、about china wholesale market guangzhou
3、单例模式的七种写法
4、Concurrency and the impact on reusable libraries
5、Java安全


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号