package com.huatek.busi.common.util;

import java.lang.reflect.Field;

import org.springframework.util.ReflectionUtils;

/**
 * 工具类
 *
 * @author anker cheng
 *
 * @Date 2019年7月2日
 */
public final class FieldUtil {

    private FieldUtil() {}

    public static Object getFieldValue(Object target, String fieldName) {
        Field field = ReflectionUtils.findField(target.getClass(), fieldName);
        if (field == null) {
            return null;
        }
        field.setAccessible(true);
        return ReflectionUtils.getField(field, target);
    }

    public static Object getFieldValue(Object target, Field field) {
        if (field == null) {
            return null;
        }
        field.setAccessible(true);
        return ReflectionUtils.getField(field, target);
    }

    public static void setFieldValue(Object target, String fieldName, Object value) {
        if (target == null) {
            return;
        }
        Field field = ReflectionUtils.findField(target.getClass(), fieldName);
        if (field == null) {
            return;
        }
        field.setAccessible(true);
        ReflectionUtils.setField(field, target, value);
    }
}
