
com.ruijc.util.MapUtils Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ruijc.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
/**
* 映射工具类
*
* @author Storezhang
*/
public class MapUtils {
public static Map toMap(Object obj) {
Map map = null;
if (obj == null) {
return map;
}
map = new HashMap();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!key.equals("class")) {
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
map = null;
}
return map;
}
public static Map toStringMap(Object obj) {
Map map = null;
if (obj == null) {
return map;
}
map = new HashMap();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!key.equals("class")) {
Method getter = property.getReadMethod();
map.put(key, getter.invoke(obj).toString());
}
}
} catch (Exception e) {
map = null;
}
return map;
}
public static int getInt(Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy