package com.phlx.anchorcollect.util;

import com.phlx.anchorcollect.entity.CattleResumeEntity;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class EntityUtils {

    /**
     * * 获取属性名数组
     *
     * @param
     * @return
     */
    public static String[] getFiledName(Object user) {

        Field[] fields = user.getClass().getDeclaredFields();
        String[] fieldNamStrings = new String[fields.length];
        for (int i = 0; i < fieldNamStrings.length; i++) {
            fieldNamStrings[i] = fields[i].getName();
        }

        return fieldNamStrings;
    }

    /**
     * @param name
     * @param user
     * @return
     * @throws IllegalAccessException
     * @Description 根据属性名 获取值（value）
     */
    public static Object getFieldValueByName(String name, Object user) throws IllegalAccessException {

        String firstletter = name.substring(0, 1).toUpperCase();

        String getter = "get" + firstletter + name.substring(1);

        Method method;
        Object value;
        try {
            method = user.getClass().getMethod(getter, new Class[]{});

            value = method.invoke(user, new Object[]{});

            return value;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

}
