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

biz.paluch.logging.gelf.standalone.BeanPropertyExtraction Maven / Gradle / Ivy

There is a newer version: 1.15.1
Show newest version
package biz.paluch.logging.gelf.standalone;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Mark Paluch
 * @since 31.07.14 08:56
 */
class BeanPropertyExtraction {
    public final static String IS_REGEX = "is([^ ]+)";
    public final static String GET_REGEX = "get([^ ]+)";

    public final static Pattern IS_PATTERN = Pattern.compile(IS_REGEX);
    public final static Pattern GET_PATTERN = Pattern.compile(GET_REGEX);

    static Map extractProperties(Object object) {

        Map methodMap = createPropertyToMethodMap(object);

        Map fields = retrieveValues(object, methodMap);
        return fields;
    }

    private static Map createPropertyToMethodMap(Object object) {
        Class beanClass = object.getClass();
        Map methodMap = new HashMap();

        Method[] methods = beanClass.getMethods();
        for (Method method : methods) {

            if (method.getParameterTypes().length != 0) {
                continue;
            }

            if (method.getReturnType().equals(Void.class) || method.getReturnType().equals(Void.TYPE)) {
                continue;
            }

            if (method.getDeclaringClass().equals(Object.class)) {
                continue;
            }

            String name = method.getName();
            String propertyName = getPropertyName(name, GET_PATTERN);
            if (propertyName == null) {
                propertyName = getPropertyName(name, IS_PATTERN);
            }

            if (propertyName == null) {
                continue;
            }

            propertyName = decapitalizePropertyName(propertyName);

            methodMap.put(propertyName, method);
        }
        return methodMap;
    }

    private static String decapitalizePropertyName(String propertyName) {
        String first = propertyName.substring(0, 1);
        String last = propertyName.substring(1);

        return first.toLowerCase() + last;

    }

    private static Map retrieveValues(Object object, Map methodMap) {
        Map fields = new HashMap();

        for (Map.Entry entry : methodMap.entrySet()) {
            try {
                Object value = entry.getValue().invoke(object);

                if (value != null) {
                    fields.put(entry.getKey(), value);
                }

            } catch (IllegalAccessException e) {
                // ignore
            } catch (InvocationTargetException e) {
                // ignore
            }

        }
        return fields;
    }

    private static String getPropertyName(String name, Pattern pattern) {
        Matcher matcher = pattern.matcher(name);
        if (matcher.matches()) {
            return matcher.toMatchResult().group(1);
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy