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

com.zhuang.data.util.EntityUtils Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.zhuang.data.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class EntityUtils {

	public static Map convertToMap(Object entity) {
		if (entity == null) {
			return null;
		} else if (entity instanceof Map) {
			return (Map) entity;
		}
		Map map = new HashMap();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(entity.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String propertyName = property.getName();
				if (!propertyName.equals("class")) {
					Method readMethod = property.getReadMethod();
					Object propertyValue = readMethod.invoke(entity);
					map.put(propertyName, propertyValue);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("实体转Map失败!", e);
		}
		return map;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy