package com.srs.common.utils.bean;

import com.srs.common.annotation.Trim;

import java.lang.reflect.Field;

/**
 * 给有@Trim注解的字段去掉前后空格
 */
public class BeanTrimmer {
 
    public static void trimFields(Object entity)   {
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Trim.class)) {
                try{
                field.setAccessible(true);
                Object value = field.get(entity);
                if (value instanceof String) {

                        String trimmedValue = ((String) value).trim();
                        field.set(entity, trimmedValue);


                }
                }catch (IllegalAccessException e){
                    continue;
                }
            }
        }
    }
}