1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| public static void copyDataObjectToBean(DataObject src, DataObject dest) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { Map map = src.toMap(); Set set = map.keySet(); Iterator<String> it = set.iterator(); PropertyDescriptor[] propertiesStr = PropertyUtils.getPropertyDescriptors(dest); System.out.println("Map:"+map); for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(dest.getClass()).getPropertyDescriptors()){ Method method = propertyDescriptor.getReadMethod(); String methodStr = propertyDescriptor.getReadMethod().getName().substring(3); String beanKey = propertyDescriptor.getReadMethod().getName().substring(3).toUpperCase(); String name = underscoreName(propertyDescriptor.getName()); if(!map.containsKey(name)) { continue; } String value = MapUtils.getString(map, name);
BeanUtils.setProperty(dest, propertyDescriptor.getName(), value); } }
public static <T extends DataObject> T copyDataObjectToBean(DataObject src, Class<T> clazz) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { Map map = src.toMap(); Set set = map.keySet(); Iterator<String> it = set.iterator(); DataObject dest = clazz.newInstance(); PropertyDescriptor[] propertiesStr = PropertyUtils.getPropertyDescriptors(dest); System.out.println("Map:"+map); for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(dest.getClass()).getPropertyDescriptors()){ Method method = propertyDescriptor.getReadMethod(); String methodStr = propertyDescriptor.getReadMethod().getName().substring(3); String beanKey = propertyDescriptor.getReadMethod().getName().substring(3).toUpperCase(); String name = underscoreName(propertyDescriptor.getName()); if(!map.containsKey(name)) { continue; } String value = MapUtils.getString(map, name);
BeanUtils.setProperty(dest, propertyDescriptor.getName(), value); } return (T) dest; }
public static String underscoreName(String name) { StringBuilder result = new StringBuilder(); if (name != null && name.length() > 0) { result.append(name.substring(0, 1).toUpperCase()); for (int i = 1; i < name.length(); i++) { String s = name.substring(i, i + 1); if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) { result.append("_"); } result.append(s.toUpperCase()); } } return result.toString(); }
public static String camelName(String name) { StringBuilder result = new StringBuilder(); if (name == null || name.isEmpty()) { return ""; } else if (!name.contains("_")) { return name.substring(0, 1).toLowerCase() + name.substring(1); } String camels[] = name.split("_"); for (String camel : camels) { if (camel.isEmpty()) { continue; } if (result.length() == 0) { result.append(camel.toLowerCase()); } else { result.append(camel.substring(0, 1).toUpperCase()); result.append(camel.substring(1).toLowerCase()); } } return result.toString(); }
|