Introspector.getBeanInfo()方法使用

本文介绍了一种将Java对象转换为Map的高效方法,通过使用Java反射API实现对象属性到Map键值对的映射。适用于快速获取对象属性的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static Map convertBean(Object bean) throws IntrospectionException,
  IllegalAccessException, InvocationTargetException {


  Map returnMap = new HashMap();

 

  Class type = bean.getClass(); // 获得对象的名字

 

  BeanInfo beanInfo = Introspector.getBeanInfo(type);
  // 在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件

 

 

  PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors();
  // 获得 beans PropertyDescriptor

 

  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[0]);
    if (result != null) {
     returnMap.put(propertyName, result);
    } else {
     returnMap.put(propertyName, "");
    }
   }
  }
  return returnMap;
 }

}

package com.bjsasc.finance.common.utils; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class JavaBeanUtil { /** * * @param source 被复制的实体类对象 * @param to 复制完后的实体类对象 * @throws Exception */ public static void Copy(Object source, Object to) throws Exception { // 获取属性 BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),java.lang.Object.class); PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors(); BeanInfo destBean = Introspector.getBeanInfo(to.getClass(),java.lang.Object.class); PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors(); try { for (int i = 0; i < sourceProperty.length; i++) { for (int j = 0; j < destProperty.length; j++) { if (sourceProperty[i].getName().equals(destProperty[j].getName())) { // 调用source的getter方法和dest的setter方法 destProperty[j].getWriteMethod().invoke(to,sourceProperty[i].getReadMethod().invoke(source)); break; } } } } catch (Exception e) { throw new Exception("属性复制失败:" + e.getMessage()); } } /** * Pojo -> Map<String, Object> * @param obj * @return * @throws Exception */ public static Map<String,Object> object2Map(Object obj)throws Exception{ Map<String,Object> map =new HashMap<>(); Field[] fields = obj.getClass().getDeclaredFields(); for(Field field:fields){ field.setAccessible(true); map.put(field.getName(), field.get(obj)); } return map; } } 帮我优化下这个代码,考虑对象中有对象和对象集合的情况
04-19
public class HtmlEscapeUtil { // 增加上下文适配的编码方法(保持不变) public static String escapeForHtml(String input) { return Encode.forHtml(input); } public static String escapeForHtmlAttribute(String input) { return Encode.forHtmlAttribute(input); } public static String escapeForJavaScript(String input) { return Encode.forJavaScript(input); } // 改进后的通用处理方法使用Bean属性访问) public static <T> T escapeHtmlFields(T obj) { if (obj == null) { return null; } try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : propertyDescriptors) { String propertyName = pd.getName(); if ("class".equals(propertyName)) { continue; // 跳过class属性 } // 获取当前属性值 Object value = pd.getReadMethod().invoke(obj); if (value instanceof String) { // 根据字段类型选择编码方式(示例为HTML上下文) String escapedValue = escapeForHtml((String) value); // 设置转义后的属性值 pd.getWriteMethod().invoke(obj, escapedValue); } else if (!isPrimitiveOrString(value.getClass())) { // 递归处理非基本类型属性 escapeHtmlFields(value); } } } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return obj; } // 辅助方法:判断是否为基本类型或String(保持不变) private static boolean isPrimitiveOrString(Class<?> type) { return type.isPrimitive() || type.equals(String.class); } }这段代码报了奇信安的代码注入:HTTP响应截断:Cookies,应该怎么改
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值