本文作者:佚名

反射获取list属性里的类名

佚名 2019-05-14 ???
摘要:List fieldList=new ArrayList();for (Class superClass = object.getClass(); superClass != Object.class; superClass = super


List fieldList=new ArrayList();
        for (Class superClass = object.getClass(); superClass != Object.class; 
				superClass = superClass.getSuperclass()) {
        	
				Field[] fields  = superClass.getDeclaredFields();
				for (Field field : fields) {
					Class fc = field.getType();
					if(fc.isPrimitive()){
						System.out.println("基本数据类型: " + field.getName() + "  " + fc.getName());
					}else{
						if(fc.isAssignableFrom(List.class)){ //判断是否为List
							System.out.println("List类型:" + field.getName());
							java.lang.reflect.Type gt = field.getGenericType();	//得到泛型类型
							ParameterizedType pt = (ParameterizedType)gt;
							Class listClazz = (Class)pt.getActualTypeArguments()[0];
							System.out.println("\t\t" + listClazz.getName());
						}
					}
					fieldList.add(field);
				}
		}

反射获取list属性里的类名

未经允许不得转载:

作者:佚名,标题:反射获取list属性里的类名,原文地址:https://www.vfjianzhan.com/python/201905/14055.html发布于2019-05-14
转载或复制请以超链接形式并注明出处唯福建站

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏