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

com.tukeof.common.util.bean.BeanMapUtil Maven / Gradle / Ivy

The newest version!
package com.tukeof.common.util.bean;

import com.tukeof.common.util.ReflectUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by tuke on 2019-01-23
 */
public class BeanMapUtil {
    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====
    // bean to map
    public static List toList(Object bean) {
        return new ArrayList<>(toProp(bean).values());
    }

    public static Map toProp(Object bean) {
        Map map = toMap(bean);
        return map2prop(map);
    }

    public static Map toProp(
            Object bean, Class aliasAnnotation) {
        Map map = toMap(bean, aliasAnnotation);
        return map2prop(map);
    }

    public static Map toProp(
            Object bean, Class aliasAnnotation,
            Class excludeAnnotation) {
        Map map = toMap(bean, aliasAnnotation, excludeAnnotation);
        return map2prop(map);
    }

    public static Map toProp(
            Object bean, Class aliasAnnotation,
            Class excludeAnnotation, int extraExcludeModifiers) {
        Map map = toMap(bean, aliasAnnotation, excludeAnnotation, extraExcludeModifiers);
        return map2prop(map);
    }

    public static Map toMap(
            Object bean) {
        return toMap(bean, null);
    }

    public static Map toMap(
            Object bean, Class aliasAnnotation) {
        return toMap(bean, aliasAnnotation, null);
    }

    public static Map toMap(
            Object bean, Class aliasAnnotation,
            Class excludeAnnotation) {
        return toMap(bean, aliasAnnotation, excludeAnnotation,
                Modifier.TRANSIENT | Modifier.VOLATILE);
    }

    // reflect field
    public static Map toMap(
            Object bean, Class aliasAnnotation,
            Class excludeAnnotation, int extraExcludeModifiers) {
        Class clazz = bean.getClass();
        Map map = new HashMap<>();

        Map fieldMap = new HashMap<>();
        ReflectUtil.retrieveFields(clazz, fieldMap, aliasAnnotation);

        fieldMap.entrySet().stream()
                .filter(entry -> {
                    Field field = entry.getValue();
                    boolean exclude = ReflectUtil.excludeStaticAndExtra(field, extraExcludeModifiers, excludeAnnotation);
                    return !exclude;
                })
                .forEach(entry -> {
                    String name = entry.getKey();
                    Field field = entry.getValue();
                    field.setAccessible(true);
                    Object value = null;
                    try {
                        value = field.get(bean);
                    } catch (IllegalAccessException ignored) {
                    }
                    if (value != null) {
                        map.put(name, value);
                    }
                });
        return map;
    }

    // reflect method
    public static Map toMapByGetter(
            Object bean, Class aliasAnnotation) {
        Class clazz = bean.getClass();

        Map map = new HashMap<>();
        List methodList = new ArrayList<>();
        ReflectUtil.retrieveMethods(clazz, methodList);
        List fieldNames = new ArrayList<>();
        ReflectUtil.retrieveFieldNames(clazz, fieldNames, aliasAnnotation);

        for (Method method : methodList) {
            Class returnType = method.getReturnType();
            int modifiers = method.getModifiers();
            boolean isNotPublish = (modifiers & Modifier.PUBLIC) == 0;
            boolean isStatic = (modifiers & Modifier.STATIC) != 0;
            if (isNotPublish || isStatic || returnType.equals(Void.class)) continue;

            if (returnType.equals(Boolean.class) || returnType.equals(boolean.class)) {
                putByPrefix(map, method, bean, fieldNames, "is");
            } else {
                putByPrefix(map, method, bean, fieldNames, "get");
            }
        }
        return map;
    }

    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====

    private static void putByPrefix(
            Map map, Method method,
            Object bean, List fieldNames, String prefix) {
        String methodName = method.getName();
        if (!methodName.startsWith(prefix)) return;

        String fieldName = methodName.substring(methodName.indexOf(prefix) + prefix.length());
        fieldName = fieldName.toLowerCase().charAt(0) + fieldName.substring(1);
        if (!fieldNames.contains(fieldName)) return;

        try {
            map.put(methodName, method.invoke(bean));
        } catch (IllegalAccessException | InvocationTargetException ignored) {
        }

    }

    private static Map map2prop(Map map) {
        Map prop = new HashMap<>();
        for (String key : map.keySet()) {
            Object value = map.get(key);
            if (value == null) {
                continue;
            }
            prop.put(key, value.toString());
        }
        return prop;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy