List<Field> fieldList=new ArrayList<Field>(); 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(" " + listClazz.getName()); } } fieldList.add(field); } }
反射获取list属性里的类名