豆到地图
// from n o w j a v a . c o m - 时 代 Java //package com.nowjava; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class Main { public static <T> Map<?, ?> beanToMap(T bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException { Class<?> type = bean.getClass(); Map<String, Object> returnMap = new HashMap<String, Object>(); BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors(); /*来自 时代Java公众号 - N o w J a v a . c o m*/ for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); Object result = readMethod.invoke(bean, new Object[] {});