• 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)
            {
            }
        }
    
    }
  • 相关阅读:
    python-opencv 分离图片(视频)中的某一颜色物体
    python-opencv遍历图片像素,并对像素进行操作
    7月5日实习日志
    7月4日实习日志
    对《软件工程》这门课总结
    作业四: 结对编程项目---四则运算
    PSP记录个人项目耗时情况
    代码复审
    是否需要有代码规范
    四则运算程序生成
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11164242.html
Copyright © 2020-2023  润新知