The JavaBean and Map transformations encapsulate the methods of the class
- 2020-05-10 18:14:43
- OfStack
Examples are as follows:
package com.ljq.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Map Utility class
*
* @author jqlin
*/
public class MapUtils {
/**
* from map Gets the property value in the collection
*
* @param <E>
* @param map
* map A collection of
* @param key
* Key to
* @param defaultValue
* The default value
* @return
* @author jiqinlin
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public final static <E> E get(Map map, Object key, E defaultValue) {
Object o = map.get(key);
if (o == null)
return defaultValue;
return (E) o;
}
/**
* Map The collection object is converted to JavaBean A collection of objects
*
* @param javaBean JavaBean Instance objects
* @param mapList Map Data set object
* @return
* @author jqlin
*/
@SuppressWarnings({ "rawtypes" })
public static <T> List<T> map2Java(T javaBean, List<Map> mapList) {
if(mapList == null || mapList.isEmpty()){
return null;
}
List<T> objectList = new ArrayList<T>();
T object = null;
for(Map map : mapList){
if(map != null){
object = map2Java(javaBean, map);
objectList.add(object);
}
}
return objectList;
}
/**
* Map Object conversion to JavaBean object
*
* @param javaBean JavaBean Instance objects
* @param map Map object
* @return
* @author jqlin
*/
@SuppressWarnings({ "rawtypes","unchecked", "hiding" })
public static <T> T map2Java(T javaBean, Map map) {
try {
// To obtain javaBean attribute
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
// create JavaBean object
Object obj = javaBean.getClass().newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean The property name
Object propertyValue = null; // javaBean Attribute values
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (map.containsKey(propertyName)) {
propertyValue = map.get(propertyName);
pd.getWriteMethod().invoke(obj, new Object[] { propertyValue });
}
}
return (T) obj;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* JavaBean Object conversion to Map object
*
* @param javaBean
* @return
* @author jqlin
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map java2Map(Object javaBean) {
Map map = new HashMap();
try {
// To obtain javaBean attribute
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean The property name
Object propertyValue = null; // javaBean Attribute values
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (!propertyName.equals("class")) {
Method readMethod = pd.getReadMethod();
propertyValue = readMethod.invoke(javaBean, new Object[0]);
map.put(propertyName, propertyValue);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}