All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.excel.util.BeanMapUtils Maven / Gradle / Ivy

package com.alibaba.excel.util;

import com.alibaba.excel.support.cglib.beans.BeanMap;
import com.alibaba.excel.support.cglib.core.DefaultNamingPolicy;

/**
 * bean utils
 *
 * @author Jiaju Zhuang
 */
public class BeanMapUtils {

    /**
     * Helper method to create a new BeanMap.  For finer
     * control over the generated instance, use a new instance of
     * BeanMap.Generator instead of this static method.
     *
     * Custom naming policy to prevent null pointer exceptions.
     * see: https://github.com/alibaba/easyexcel/issues/2064
     *
     * @param bean the JavaBean underlying the map
     * @return a new BeanMap instance
     */
    public static BeanMap create(Object bean) {
        BeanMap.Generator gen = new BeanMap.Generator();
        gen.setBean(bean);
        gen.setContextClass(bean.getClass());
        gen.setNamingPolicy(EasyExcelNamingPolicy.INSTANCE);
        return gen.create();
    }

    public static class EasyExcelNamingPolicy extends DefaultNamingPolicy {
        public static final EasyExcelNamingPolicy INSTANCE = new EasyExcelNamingPolicy();

        @Override
        protected String getTag() {
            return "ByEasyExcelCGLIB";
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy