作者:faithKOBE | 来源:互联网 | 2023-09-11 10:08
1、2、当isAccessible()的结果是false时不允许通过反射访问private变量。packagecom.yung.ppapi.util;importjava.be
1、
2、当isAccessible()的结果是false时不允许通过反射访问private变量。
package com.yung.ppapi.util;import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.BeanUtils;import com.yung.ppapi.integration.dto.YCPFExpressDTO;/*** * @author lisheng4**/
public class BeanUtil {public static T clone(Object source, Class type) {try {if (source == null) {return null;}T target = type.newInstance();BeanUtils.copyProperties(source, target);return target;} catch (Exception e) {e.printStackTrace();return null;} } public static T clone(Object source, Class type, String... ignoreProperties) {try {if (source == null) {return null;}T target = type.newInstance();BeanUtils.copyProperties(source, target, ignoreProperties);return target;} catch (Exception e) {e.printStackTrace();return null;} } /*** 利用反射实现* @param map* @param beanClass* @return* @throws Exception*/public static Object mapToObject(Map map, Class> beanClass) throws Exception {if (map == null) {return null;}Object obj = beanClass.newInstance();Field[] fields = obj.getClass().getDeclaredFields();for (Field field : fields) {int mod = field.getModifiers();if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {continue;}boolean accessFlag = field.isAccessible();field.setAccessible(true);// 允许通过反射访问该字段field.set(obj, map.get(field.getName()));field.setAccessible(accessFlag);}return obj;}/*** 利用反射实现* 空属性不转换* 超过10万条数据不建议使用* @param obj* @return* @throws Exception*/public static Map objectToMap(Object obj) throws Exception {if (obj == null) {return null;}Map map = new HashMap();Field[] fields = obj.getClass().getDeclaredFields();for (int i = 0, len = fields.length; i map, Class> beanClass) throws Exception { if (map == null)return null; Object obj = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { Method setter = property.getWriteMethod(); if (setter != null) { setter.invoke(obj, map.get(property.getName())); } } return obj; } /*** 利用java.beans.Introspector实现* @param obj* @return* @throws Exception*/public static Map objectToMap2(Object obj) throws Exception { if(obj == null) return null; Map map = new HashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (key.compareToIgnoreCase("class") == 0) { continue; } Method getter = property.getReadMethod(); Object value = getter!=null ? getter.invoke(obj) : null; map.put(key, value); } return map; } /*** 利用org.apache.commons.beanutils.BeanUtils实现* @param map* @param beanClass* @return* @throws Exception*/public static Object mapToObject3(Map map, Class> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map); return obj; } /*** 利用org.apache.commons.beanutils.BeanMap实现* @param obj* @return*/public static Map,?> objectToMap3(Object obj) { if(obj == null) return null; return new BeanMap(obj); } // public static void main(String[] args) throws Exception {
// List
参考文章:https://www.cnblogs.com/XuYiHe/p/6871799.html
参考文章:https://blog.csdn.net/o_nianchenzi_o/article/details/78022416
楼主这么辛苦,请使用楼主的推荐码领取支付宝红包吧,记得一定要消费掉哦。双赢^_^。
1、打开支付宝首页搜索“8282987” 立即领红包。