biz.paluch.logging.gelf.standalone.BeanPropertyExtraction Maven / Gradle / Ivy
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 static final String IS_REGEX = "is([^ ]+)";
public static final String GET_REGEX = "get([^ ]+)";
public static final Pattern IS_PATTERN = Pattern.compile(IS_REGEX);
public static final Pattern GET_PATTERN = Pattern.compile(GET_REGEX);
private BeanPropertyExtraction() {
// no instance allowed
}
static Map extractProperties(Object object) {
Map methodMap = createPropertyToMethodMap(object);
return retrieveValues(object, methodMap);
}
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 | 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;
}
}