Back to project page VideoExtand.
The source code is released under:
Apache License
If you think the Android project VideoExtand listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ??????BeanRefUtils.java//w w w .j ava2 s . c o m * ?????<????> * ?????<????> * ????xiaoying * ?????2013-8-30 * ????xiaoying * ?????2013-8-30 * ???v1.0 */ package com.yuninfo.videoextand.utils; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; /** * ??? * * @author xiaoying * */ public class BeanRefUtil { /** * Get all fields' value and put them to a map. * * @param bean * @return Map */ public static Map<String, Object> getFieldValueMap(Object bean) { Class<?> cls = bean.getClass(); Map<String, Object> valueMap = new HashMap<String, Object>(); // Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); Object value = field.get(bean); // if(value == null) { // valueMap.put(field.getName(), ""); // continue; // } valueMap.put(field.getName(), value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return valueMap; } /** * Set the fields' value. * * @param bean * @param valMap */ public static void setFieldValues(Object bean, Map<String, Object> valMap) { Class<?> cls = bean.getClass(); //Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { if(valMap.containsKey(field.getName())) { field.setAccessible(true); try { field.set(bean, valMap.get(field.getName())); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } }