import java.lang.reflect.Field;
import java.util. edList;
import java.util.List;

/**
 * @Auther: liyue
 * @Date: 2018/10/24 14:44
 * @De ion:
 */
public class ReflexUtil {


    /**
     * 获取对象的所有属性值
     *
     * @param clazzT
     * @param <T>
     * @return
     */
    public static <T> List<String> get AttributeNames(Class<T> clazzT) {
        List<String> list = new  edList<>();
        try {
            //根据类名获得其对应的Class对象 写上你想要的类名就是了 注意是全名 如果有包的话要加上 比如java.Lang.String
            Class clazz = Class.forName(clazzT.getName());
            //根据Class对象获得属性 私有的也可以获得
            Field[] fields = clazz.getDeclaredFields();
            for (Field f : fields) {
                list.add(f.getName());
                // 类型
                // f.getType();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 把对象String类型为空的值设置为\"\"
     *
     * @param  
     */
    public static void setNotNull(   ) {
        try {
            for (Field f :  .getClass().getDeclaredFields()) {
                f.setAccessible(true);
                //判断字段是否为空,并且对象属性中的基本都会转为对象类型来判断
                if (f.get( ) == null) {
                    try {
                        f.set( , \"\");
                    } catch (Exception e) {
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

 

END。

收藏 打印