model转map

package xyz.jangle.oa.utils;

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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * model工具类
 * @author huhj
 *
 */
public class ModelUtils {

	/**
	 * 将model转化为map
	 * @param model
	 * @return
	 */
	public static Map modelToMap(Object model) {
		if (model == null) {
			return new HashMap();
		}
		Map map = new HashMap();
		Method method;
		Field[] fields1 = model.getClass().getSuperclass().getDeclaredFields(); // 超类属性
		Field[] fields2 = model.getClass().getDeclaredFields(); // 本类属性
		List list = new ArrayList(Arrays.asList(fields1));
		List list2 = Arrays.asList(fields2);
		list.addAll(list2);
		for (Field field : list) {
			boolean isStatic = Modifier.isStatic(field.getModifiers());
			if(isStatic) {
				continue;	//去除静态成员
			}
			String getMethodName = getMethodName(field.getName());
			try {
				method = model.getClass().getMethod(getMethodName);
				map.put(field.getName(), method.invoke(model));
			} catch (NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	private static String getMethodName(String name) {
		if (name != null && name.length() > 2) {
			return "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
		}
		return name;
	}

}

 

你可能感兴趣的:(Java相关)