package cn.lihongjie.coal.common;

import java.lang.reflect.Field;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ReflectionUtils;

@UtilityClass
public class ReflectUtils {

    public static Object getFieldValue(Object object, String fieldName) {

        if (object == null) {
            return null;
        }

        if (StringUtils.isEmpty(fieldName)) {
            return null;
        }

        Field field = ReflectionUtils.findField(object.getClass(), fieldName);
        if (field != null) {
            field.setAccessible(true);
            return ReflectionUtils.getField(field, object);
        }
        return null;
    }
}
