package com.yuzhouwan.common.util;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
 * Copyright @ 2016 yuzhouwan.com
 * All right reserved.
 * Function：Bean Utils
 *
 * @author Benedict Jin
 * @since 2016/12/1
 */
public class BeanUtils {

    private static final Logger _log = LoggerFactory.getLogger(BeanUtils.class);

    /**
     * Swap values into object's similar filed.
     *
     * @param o       object
     * @param key     filed
     * @param value   value
     * @param ignores character to be ignored
     */
    public static void swapper(Object o, String key, Object value, String... ignores) {
        Field[] fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (StrUtils.isLike(field.getName(), key, ignores)) {
                field.setAccessible(true);
                try {
                    field.set(o, value);
                } catch (IllegalAccessException e) {
                    _log.error(ExceptionUtils.errorInfo(e));
                }
                return;
            }
        }
        _log.debug("[Warn] Cannot be swapped, object: {}, key: {}, value: {}, ignores: {}.",
                o, key, value, JSON.toJSONString(ignores));
    }
}

@@ -152,16 +152,63 @@
 t.Field;
+%0Aimport java.util.concurrent.ConcurrentHashMap;
 %0A%0A/**%0A *
@@ -444,16 +444,119 @@
 lass);%0A%0A
+    private static final ConcurrentHashMap%3CString, Field%5B%5D%3E FIELDS_CACHE = new ConcurrentHashMap%3C%3E();%0A%0A
     /**%0A
@@ -849,57 +849,559 @@
     
-Field%5B%5D fields = o.getClass().getDeclaredFields()
+if (o == null %7C%7C StrUtils.isEmpty(key)) return;%0A        Class%3C?%3E clazz;%0A        if ((clazz = o.getClass()) == null) return;%0A        String className = clazz.getName();%0A        if (StrUtils.isEmpty(className)) return;%0A        Field%5B%5D fields;%0A        if (FIELDS_CACHE.size() == 0 %7C%7C !FIELDS_CACHE.containsKey(className)) %7B%0A            fields = clazz.getDeclaredFields();%0A            FIELDS_CACHE.put(className, fields);%0A        %7D else %7B%0A            fields = FIELDS_CACHE.get(className);%0A        %7D%0A        if (fields == null %7C%7C fields.length == 0) return
 ;%0A  

add cache into BeanUtils for impove the speed of reflection