zoukankan      html  css  js  c++  java
  • 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() + "
    ownerType:" + pt.getOwnerType() + "
    rawType:"
                        + 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)
            {
            }
        }
    
    }
  • 相关阅读:
    Leetcode 1489找到最小生成树李关键边和伪关键边
    Leetcode 113 路径总和 II
    hdu 1223 还是畅通工程
    hdu 1087 Super Jumping! Jumping! Jumping!
    hdu 1008 Elevator
    hdu 1037 Keep on Truckin'
    湖工oj 1241 畅通工程
    湖工oj 1162 大武汉局域网
    hdu 2057 A + B Again
    poj 2236 Wireless Network
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11164242.html
Copyright © 2011-2022 走看看