Java 类动态添加属性字段

说明:做项目中遇到一种场景,需要根据查询时间段, 获取时间段中中每个月份对应的金额(费用统计)。如截图中的两列

因为列是动态的, 首先想到的就是后天拼接JSON格式字符串, 然后返回到前台, 组装表头及内容。 但是当前系统中easyUI版本为1.2,并不支持 data属性(官方从1.3.2开始支持)。所以只能返回list<T> 格式。

网上一搜相关代码很多, 看客可以自己搜索一下。 我这里记录一下我当时使用场景及用法,已备以后使用。

 

1.需要引用cglib jar包, 我用的版本是2.2

2.建一个实体对象 DynamicBean.java 。主要用来处理对象。

  1. public class DynamicBean {
  2. private Object object = null; // 动态生成的类
  3. private BeanMap beanMap = null; // 存放属性名称以及属性的类型
  4. public DynamicBean() {
  5. super();
  6. }
  7. public DynamicBean(Map propertyMap) {
  8. this.object = generateBean(propertyMap);
  9. this.beanMap = BeanMap.create(this.object);
  10. }
  11. /**
  12. * @param propertyMap
  13. * @return
  14. */
  15. private Object generateBean(Map propertyMap) {
  16. BeanGenerator generator = new BeanGenerator();
  17. Set keySet = propertyMap.keySet();
  18. for (Iterator<String> i = keySet.iterator(); i.hasNext();) {
  19. String key = (String) i.next();
  20. generator.addProperty(key, (Class) propertyMap.get(key));
  21. }
  22. return generator.create();
  23. }
  24. /**
  25. * ��bean���Ը�ֵ
  26. * @param property ������
  27. * @param value ֵ
  28. */
  29. public void setValue(Object property, Object value) {
  30. beanMap.put(property, value);
  31. }
  32. /**
  33. * ͨ���������õ�����ֵ
  34. * @param property ������
  35. * @return ֵ
  36. */
  37. public Object getValue(String property) {
  38. return beanMap.get(property);
  39. }
  40. /**
  41. * 返回新生成的对象
  42. * @return
  43. */
  44. public Object getObject() {
  45. return this.object;
  46. }
  47. }

3. 原来对象, 及需要拼接到对象中的属性字段集合处理方法。

  1. /**
  2. *参数说明:
  3. * object : 查询结果数组中对象。
  4. * moneyMap : 为对象对应所有月份数据集合
  5. * 解释:已经查询出一组账单对象集合List<Bill> , 而moneyMap为对象中的一个属性
  6. * Map<String,Bigdecimal>, 存放了月份及金额
  7. */
  8. private Object dynamicClass(Object object, Map<String, BigDecimal> moneyMap) throws Exception {
  9. // 字段 - 值 集合
  10. HashMap<String, Object> returnMap = new HashMap<String, Object>();
  11. // 字段 - 字段类型 集合
  12. HashMap<String, Object> typeMap = new HashMap<String, Object>();
  13. // 获取传入类
  14. Class<? extends Object> type = object.getClass();
  15. BeanInfo beanInfo = Introspector.getBeanInfo(type);
  16. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  17. // 获取对象中已存在的数据
  18. for (int i = 0; i < propertyDescriptors.length; i++) {
  19. PropertyDescriptor descriptor = propertyDescriptors[i];
  20. String propertyName = descriptor.getName();
  21. if (!propertyName.equals("class") && !propertyName.equals("monthMap")) {
  22. Method readMethod = descriptor.getReadMethod();
  23. Object result = readMethod.invoke(object, new Object[0]);
  24. if (result != null) {
  25. returnMap.put(propertyName, result);
  26. } else {
  27. String propertyType = descriptor.getPropertyType().toString();
  28. if (propertyType.contains("java.math.BigDecimal")) {
  29. returnMap.put(propertyName, new BigDecimal(0));
  30. } else {
  31. returnMap.put(propertyName, "");
  32. }
  33. }
  34. typeMap.put(propertyName, descriptor.getPropertyType());
  35. }
  36. }
  37. // 获取月份数据, 变为字段属性
  38. Set<String> monthKeys = moneyMap.keySet();
  39. for (Iterator<String> it = monthKeys.iterator(); it.hasNext();) {
  40. String key = (String) it.next();
  41. // 字段类型
  42. typeMap.put(key, Class.forName("java.math.BigDecimal"));
  43. // 字段对应值
  44. returnMap.put(key, moneyMap.get(key));
  45. }
  46. // map转换成实体对象
  47. DynamicBean bean = new DynamicBean(typeMap);
  48. // 赋值
  49. Set<String> keys = typeMap.keySet();
  50. for (Iterator<String> it = keys.iterator(); it.hasNext();) {
  51. String key = (String) it.next();
  52. bean.setValue(key, returnMap.get(key));
  53. }
  54. Object obj = bean.getObject();
  55. return obj;
  56. }

 

做笔记使用, 说不定以后还会用到。