Commit 15acd796058ab8348996f6fda863ae37de5255c1
1 parent
b4ec3db9
Exists in
master
objectutil upgrade
Showing
2 changed files
with
256 additions
and
187 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilObject.java
| ... | ... | @@ -3,6 +3,8 @@ package com.taover.util; |
| 3 | 3 | import java.lang.reflect.Field; |
| 4 | 4 | import java.lang.reflect.Modifier; |
| 5 | 5 | import java.util.ArrayList; |
| 6 | +import java.util.Arrays; | |
| 7 | +import java.util.HashMap; | |
| 6 | 8 | import java.util.Iterator; |
| 7 | 9 | import java.util.List; |
| 8 | 10 | import java.util.Map; |
| ... | ... | @@ -11,376 +13,443 @@ public class UtilObject { |
| 11 | 13 | /** |
| 12 | 14 | * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; |
| 13 | 15 | * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer |
| 16 | + * | |
| 14 | 17 | * @param source |
| 15 | 18 | * @param dest |
| 16 | 19 | * @param allowNull |
| 17 | 20 | * @param exceptFieldNameArray |
| 18 | 21 | * @return 如果source==dest或者source==null或者dest==null,则返回false |
| 19 | 22 | */ |
| 20 | - public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], boolean isNewInstance){ | |
| 21 | - //引用检查 | |
| 22 | - if(source==null || dest==null || source==dest){ | |
| 23 | + public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], | |
| 24 | + boolean isNewInstance) { | |
| 25 | + // 引用检查 | |
| 26 | + if (source == null || dest == null || source == dest) { | |
| 23 | 27 | return null; |
| 24 | 28 | } |
| 25 | - | |
| 26 | - //获取Field数组 | |
| 27 | - Field[] sourceField = source.getClass().getDeclaredFields(); | |
| 28 | - Field[] destField = dest.getClass().getDeclaredFields(); | |
| 29 | - | |
| 30 | - //获取目标对象 | |
| 29 | + | |
| 30 | + // 获取Field数组 | |
| 31 | + Field[] sourceField = getAndCachedFields(source.getClass()); | |
| 32 | + Field[] destField = getAndCachedFields(dest.getClass()); | |
| 33 | + | |
| 34 | + // 获取目标对象 | |
| 31 | 35 | String[] destFieldNameArray = new String[destField.length]; |
| 32 | 36 | String[] destFieldTypeArray = new String[destField.length]; |
| 33 | - for(int i=0; i<destField.length; ++i){ | |
| 37 | + for (int i = 0; i < destField.length; ++i) { | |
| 34 | 38 | destFieldNameArray[i] = destField[i].getName(); |
| 35 | 39 | destFieldTypeArray[i] = destField[i].getType().getName(); |
| 36 | 40 | } |
| 37 | - | |
| 41 | + | |
| 38 | 42 | Object newDest = null; |
| 39 | - //注入属性 | |
| 40 | - if(isNewInstance){ | |
| 43 | + // 注入属性 | |
| 44 | + if (isNewInstance) { | |
| 41 | 45 | try { |
| 42 | 46 | newDest = dest.getClass().newInstance(); |
| 43 | - for(int i=0; i<destField.length; ++i){ | |
| 47 | + for (int i = 0; i < destField.length; ++i) { | |
| 44 | 48 | destField[i].setAccessible(true); |
| 45 | 49 | Object tempValue = destField[i].get(dest); |
| 46 | 50 | destField[i].set(newDest, tempValue); |
| 47 | 51 | } |
| 48 | - } catch (Exception e2) { } | |
| 52 | + } catch (Exception e2) { | |
| 53 | + } | |
| 49 | 54 | } |
| 50 | - | |
| 51 | - //遍历源field数组,处理源field数组 | |
| 52 | - for(int i=0; i<sourceField.length; ++i){ | |
| 53 | - //获取源field相关信息 | |
| 55 | + | |
| 56 | + // 遍历源field数组,处理源field数组 | |
| 57 | + for (int i = 0; i < sourceField.length; ++i) { | |
| 58 | + // 获取源field相关信息 | |
| 54 | 59 | Object sourceFieldValue = null; |
| 55 | 60 | String sourceFieldName = sourceField[i].getName(); |
| 56 | 61 | String sourceFieldType = sourceField[i].getType().getName(); |
| 57 | - | |
| 58 | - //获取sourceFieldValue值 | |
| 62 | + | |
| 63 | + // 获取sourceFieldValue值 | |
| 59 | 64 | sourceField[i].setAccessible(true); |
| 60 | 65 | try { |
| 61 | 66 | sourceFieldValue = sourceField[i].get(source); |
| 62 | - } catch (Exception e1) { } | |
| 63 | - | |
| 64 | - //检查是否在exceptFieldName中 | |
| 65 | - if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | |
| 67 | + } catch (Exception e1) { | |
| 68 | + } | |
| 69 | + | |
| 70 | + // 检查是否在exceptFieldName中 | |
| 71 | + if (-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)) { | |
| 66 | 72 | continue; |
| 67 | 73 | } |
| 68 | - | |
| 69 | - //检验sourceFieldValue是否为NULL | |
| 70 | - if(!allowNull && sourceFieldValue == null){ | |
| 74 | + | |
| 75 | + // 检验sourceFieldValue是否为NULL | |
| 76 | + if (!allowNull && sourceFieldValue == null) { | |
| 71 | 77 | continue; |
| 72 | 78 | } |
| 73 | - | |
| 74 | - //检查源field字段名是否在目标field组中存在 | |
| 79 | + | |
| 80 | + // 检查源field字段名是否在目标field组中存在 | |
| 75 | 81 | int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray); |
| 76 | - if(nameIndex == -1){ | |
| 82 | + if (nameIndex == -1) { | |
| 77 | 83 | continue; |
| 78 | - }else{ | |
| 79 | - //如果是final关键词修饰,则返回 | |
| 80 | - if(Modifier.isFinal(destField[nameIndex].getModifiers())){ | |
| 84 | + } else { | |
| 85 | + // 如果是final关键词修饰,则返回 | |
| 86 | + if (Modifier.isFinal(destField[nameIndex].getModifiers())) { | |
| 81 | 87 | continue; |
| 82 | 88 | } |
| 83 | - | |
| 84 | - //如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象 | |
| 85 | - if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){ | |
| 89 | + | |
| 90 | + // 如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象 | |
| 91 | + if (sourceFieldType.equals(destFieldTypeArray[nameIndex])) { | |
| 86 | 92 | destField[nameIndex].setAccessible(true); |
| 87 | 93 | try { |
| 88 | 94 | destField[nameIndex].set(dest, sourceFieldValue); |
| 89 | - if(isNewInstance){ | |
| 90 | - destField[nameIndex].set(newDest, sourceFieldValue); | |
| 95 | + if (isNewInstance) { | |
| 96 | + destField[nameIndex].set(newDest, sourceFieldValue); | |
| 91 | 97 | } |
| 92 | - } catch (Exception e) { } | |
| 98 | + } catch (Exception e) { | |
| 99 | + } | |
| 93 | 100 | } |
| 94 | 101 | } |
| 95 | 102 | } |
| 96 | - | |
| 103 | + | |
| 97 | 104 | return newDest; |
| 98 | 105 | } |
| 99 | - | |
| 106 | + | |
| 107 | + private static Map<String, Field[]> cachedField = new HashMap<String, Field[]>(); | |
| 108 | + | |
| 109 | + private static Field[] getAndCachedFields(Class<? extends Object> infoClass) { | |
| 110 | + String infoClassName = infoClass.getName(); | |
| 111 | + if (cachedField.containsKey(infoClassName)) { | |
| 112 | + return cachedField.get(infoClassName); | |
| 113 | + } else { | |
| 114 | + List<Field> recurseField = new ArrayList<Field>(); | |
| 115 | + loadClassFieldRecurse(infoClass, recurseField); | |
| 116 | + Field[] data = new Field[recurseField.size()]; | |
| 117 | + recurseField.toArray(data); | |
| 118 | + cachedField.put(infoClassName, data); | |
| 119 | + return data; | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + private static void loadClassFieldRecurse(Class<? extends Object> infoClass, List<Field> recurseField) { | |
| 124 | + // Object对象不处理 | |
| 125 | + if ("java.lang.Object".equals(infoClass.getName())) { | |
| 126 | + return; | |
| 127 | + } | |
| 128 | + | |
| 129 | + // 父类处理 | |
| 130 | + Class parentClass = infoClass.getSuperclass(); | |
| 131 | + if (parentClass != null || !"java.lang.Object".equals(parentClass.getName())) { | |
| 132 | + loadClassFieldRecurse(parentClass, recurseField); | |
| 133 | + } | |
| 134 | + | |
| 135 | + recurseField.addAll(Arrays.asList(infoClass.getDeclaredFields())); | |
| 136 | + } | |
| 137 | + | |
| 100 | 138 | /** |
| 101 | 139 | * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; |
| 102 | 140 | * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer |
| 141 | + * | |
| 103 | 142 | * @param source |
| 104 | 143 | * @param destClass |
| 105 | 144 | * @param allowNull |
| 106 | 145 | * @param exceptFieldNameArray |
| 107 | 146 | * @return 如果source==null或者destClass==null,则返回false |
| 108 | 147 | */ |
| 109 | - public static <E> E fieldCopy(Object source, Class<E> destClass, boolean allowNull, String exceptFieldNameArray[]) throws Exception{ | |
| 110 | - //引用检查 | |
| 111 | - if(source==null || destClass==null){ | |
| 148 | + public static <E> E fieldCopy(Object source, Class<E> destClass, boolean allowNull, String exceptFieldNameArray[]) | |
| 149 | + throws Exception { | |
| 150 | + // 引用检查 | |
| 151 | + if (source == null || destClass == null) { | |
| 112 | 152 | return null; |
| 113 | 153 | } |
| 114 | - | |
| 115 | - //获取Field数组 | |
| 116 | - Field[] sourceField = source.getClass().getDeclaredFields(); | |
| 117 | - Field[] destField = destClass.getDeclaredFields(); | |
| 118 | - | |
| 119 | - //获取目标对象 | |
| 154 | + | |
| 155 | + // 获取Field数组 | |
| 156 | + Field[] sourceField = getAndCachedFields(source.getClass()); | |
| 157 | + Field[] destField = getAndCachedFields(destClass); | |
| 158 | + | |
| 159 | + // 获取目标对象 | |
| 120 | 160 | String[] destFieldNameArray = new String[destField.length]; |
| 121 | 161 | String[] destFieldTypeArray = new String[destField.length]; |
| 122 | - for(int i=0; i<destField.length; ++i){ | |
| 162 | + for (int i = 0; i < destField.length; ++i) { | |
| 123 | 163 | destFieldNameArray[i] = destField[i].getName(); |
| 124 | 164 | destFieldTypeArray[i] = destField[i].getType().getName(); |
| 125 | 165 | } |
| 126 | - | |
| 166 | + | |
| 127 | 167 | E newDest = destClass.newInstance(); |
| 128 | - | |
| 129 | - //遍历源field数组,处理源field数组 | |
| 130 | - for(int i=0; i<sourceField.length; ++i){ | |
| 131 | - //获取源field相关信息 | |
| 168 | + | |
| 169 | + // 遍历源field数组,处理源field数组 | |
| 170 | + for (int i = 0; i < sourceField.length; ++i) { | |
| 171 | + // 获取源field相关信息 | |
| 132 | 172 | Object sourceFieldValue = null; |
| 133 | 173 | String sourceFieldName = sourceField[i].getName(); |
| 134 | 174 | String sourceFieldType = sourceField[i].getType().getName(); |
| 135 | - | |
| 136 | - //获取sourceFieldValue值 | |
| 175 | + | |
| 176 | + // 获取sourceFieldValue值 | |
| 137 | 177 | sourceField[i].setAccessible(true); |
| 138 | 178 | try { |
| 139 | 179 | sourceFieldValue = sourceField[i].get(source); |
| 140 | - } catch (Exception e1) { } | |
| 141 | - | |
| 142 | - //检查是否在exceptFieldName中 | |
| 143 | - if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | |
| 180 | + } catch (Exception e1) { | |
| 181 | + } | |
| 182 | + | |
| 183 | + // 检查是否在exceptFieldName中 | |
| 184 | + if (-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)) { | |
| 144 | 185 | continue; |
| 145 | 186 | } |
| 146 | - | |
| 147 | - //检验sourceFieldValue是否为NULL | |
| 148 | - if(!allowNull && sourceFieldValue == null){ | |
| 187 | + | |
| 188 | + // 检验sourceFieldValue是否为NULL | |
| 189 | + if (!allowNull && sourceFieldValue == null) { | |
| 149 | 190 | continue; |
| 150 | 191 | } |
| 151 | - | |
| 152 | - //检查源field字段名是否在目标field组中存在 | |
| 192 | + | |
| 193 | + // 检查源field字段名是否在目标field组中存在 | |
| 153 | 194 | int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray); |
| 154 | - if(nameIndex == -1){ | |
| 195 | + if (nameIndex == -1) { | |
| 155 | 196 | continue; |
| 156 | - }else{ | |
| 157 | - //如果是final关键词修饰,则返回 | |
| 158 | - if(Modifier.isFinal(destField[nameIndex].getModifiers())){ | |
| 197 | + } else { | |
| 198 | + // 如果是final关键词修饰,则返回 | |
| 199 | + if (Modifier.isFinal(destField[nameIndex].getModifiers())) { | |
| 159 | 200 | continue; |
| 160 | 201 | } |
| 161 | - | |
| 162 | - //如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象 | |
| 163 | - if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){ | |
| 202 | + | |
| 203 | + // 如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象 | |
| 204 | + if (sourceFieldType.equals(destFieldTypeArray[nameIndex])) { | |
| 164 | 205 | destField[nameIndex].setAccessible(true); |
| 165 | 206 | try { |
| 166 | 207 | destField[nameIndex].set(newDest, sourceFieldValue); |
| 167 | - } catch (Exception e) { } | |
| 208 | + } catch (Exception e) { | |
| 209 | + } | |
| 168 | 210 | } |
| 169 | 211 | } |
| 170 | 212 | } |
| 171 | - | |
| 213 | + | |
| 172 | 214 | return newDest; |
| 173 | 215 | } |
| 174 | - | |
| 216 | + | |
| 175 | 217 | /** |
| 176 | 218 | * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性 |
| 177 | - * 在字符串驼峰命名转化成下划线形式 | |
| 178 | - * 如 helloWord -> hello_word | |
| 219 | + * 在字符串驼峰命名转化成下划线形式 如 helloWord -> hello_word | |
| 220 | + * | |
| 179 | 221 | * @param source |
| 180 | 222 | * @param isCopyNull |
| 181 | 223 | * @param exceptFieldNameArray |
| 182 | 224 | * @return |
| 183 | 225 | */ |
| 184 | - public static List<Object[]> objecFieldToListToLowerCase(Object source, boolean isCopyNull, String exceptFieldNameArray[]){ | |
| 226 | + public static List<Object[]> objecFieldToListToLowerCase(Object source, boolean isCopyNull, | |
| 227 | + String exceptFieldNameArray[]) { | |
| 185 | 228 | List<Object[]> result = new ArrayList<Object[]>(); |
| 186 | - //参数检验 | |
| 187 | - if(source == null){ | |
| 229 | + // 参数检验 | |
| 230 | + if (source == null) { | |
| 188 | 231 | return result; |
| 189 | 232 | } |
| 190 | - | |
| 191 | - //获取Field数组 | |
| 192 | - Field[] sourceField = source.getClass().getDeclaredFields(); | |
| 193 | - //遍历源field数组,处理源field数组 | |
| 194 | - for(int i=0; i<sourceField.length; ++i){ | |
| 195 | - //获取源field相关信息 | |
| 233 | + | |
| 234 | + // 获取Field数组 | |
| 235 | + Field[] sourceField = getAndCachedFields(source.getClass()); | |
| 236 | + // 遍历源field数组,处理源field数组 | |
| 237 | + for (int i = 0; i < sourceField.length; ++i) { | |
| 238 | + // 获取源field相关信息 | |
| 196 | 239 | Object sourceFieldValue = null; |
| 197 | 240 | String sourceFieldName = sourceField[i].getName(); |
| 198 | - | |
| 199 | - //获取sourceFieldValue值 | |
| 241 | + | |
| 242 | + // 获取sourceFieldValue值 | |
| 200 | 243 | sourceField[i].setAccessible(true); |
| 201 | 244 | try { |
| 202 | 245 | sourceFieldValue = sourceField[i].get(source); |
| 203 | - } catch (Exception e1) { } | |
| 204 | - | |
| 205 | - //检查是否在exceptFieldName中 | |
| 206 | - if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | |
| 246 | + } catch (Exception e1) { | |
| 247 | + } | |
| 248 | + | |
| 249 | + // 检查是否在exceptFieldName中 | |
| 250 | + if (-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)) { | |
| 207 | 251 | continue; |
| 208 | 252 | } |
| 209 | - | |
| 210 | - //检验sourceFieldValue是否为NULL | |
| 211 | - if(!isCopyNull && sourceFieldValue == null){ | |
| 253 | + | |
| 254 | + // 检验sourceFieldValue是否为NULL | |
| 255 | + if (!isCopyNull && sourceFieldValue == null) { | |
| 212 | 256 | continue; |
| 213 | 257 | } |
| 214 | 258 | sourceFieldName = UtilString.underscoreName(sourceFieldName).toLowerCase(); |
| 215 | - //添加属性 | |
| 216 | - result.add(new Object[]{sourceFieldName, sourceFieldValue}); | |
| 259 | + // 添加属性 | |
| 260 | + result.add(new Object[] { sourceFieldName, sourceFieldValue }); | |
| 217 | 261 | } |
| 218 | - | |
| 262 | + | |
| 219 | 263 | return result; |
| 220 | 264 | } |
| 221 | - | |
| 265 | + | |
| 222 | 266 | /** |
| 223 | 267 | * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性 |
| 268 | + * | |
| 224 | 269 | * @param source |
| 225 | 270 | * @param allowNull |
| 226 | 271 | * @param exceptFieldNameArray |
| 227 | 272 | * @return |
| 228 | 273 | */ |
| 229 | - public static List<Object[]> fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]){ | |
| 274 | + public static List<Object[]> fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]) { | |
| 230 | 275 | List<Object[]> result = new ArrayList<Object[]>(); |
| 231 | - //参数检验 | |
| 232 | - if(source == null){ | |
| 276 | + // 参数检验 | |
| 277 | + if (source == null) { | |
| 233 | 278 | return result; |
| 234 | 279 | } |
| 235 | - | |
| 236 | - //获取Field数组 | |
| 237 | - Field[] sourceField = source.getClass().getDeclaredFields(); | |
| 238 | - //遍历源field数组,处理源field数组 | |
| 239 | - for(int i=0; i<sourceField.length; ++i){ | |
| 240 | - //获取源field相关信息 | |
| 280 | + | |
| 281 | + // 获取Field数组 | |
| 282 | + Field[] sourceField = getAndCachedFields(source.getClass()); | |
| 283 | + // 遍历源field数组,处理源field数组 | |
| 284 | + for (int i = 0; i < sourceField.length; ++i) { | |
| 285 | + // 获取源field相关信息 | |
| 241 | 286 | Object sourceFieldValue = null; |
| 242 | 287 | String sourceFieldName = sourceField[i].getName(); |
| 243 | - | |
| 244 | - //获取sourceFieldValue值 | |
| 288 | + | |
| 289 | + // 获取sourceFieldValue值 | |
| 245 | 290 | sourceField[i].setAccessible(true); |
| 246 | 291 | try { |
| 247 | 292 | sourceFieldValue = sourceField[i].get(source); |
| 248 | - } catch (Exception e1) { } | |
| 249 | - | |
| 250 | - //检查是否在exceptFieldName中 | |
| 251 | - if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | |
| 293 | + } catch (Exception e1) { | |
| 294 | + } | |
| 295 | + | |
| 296 | + // 检查是否在exceptFieldName中 | |
| 297 | + if (-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)) { | |
| 252 | 298 | continue; |
| 253 | 299 | } |
| 254 | - | |
| 255 | - //检验sourceFieldValue是否为NULL | |
| 256 | - if(!allowNull && sourceFieldValue == null){ | |
| 300 | + | |
| 301 | + // 检验sourceFieldValue是否为NULL | |
| 302 | + if (!allowNull && sourceFieldValue == null) { | |
| 257 | 303 | continue; |
| 258 | 304 | } |
| 259 | - | |
| 260 | - //添加属性 | |
| 261 | - result.add(new Object[]{sourceFieldName, sourceFieldValue}); | |
| 305 | + | |
| 306 | + // 添加属性 | |
| 307 | + result.add(new Object[] { sourceFieldName, sourceFieldValue }); | |
| 262 | 308 | } |
| 263 | - | |
| 309 | + | |
| 264 | 310 | return result; |
| 265 | - } | |
| 266 | - | |
| 267 | - public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass, boolean isCopyNull, String exceptFieldNameArray[]) throws Exception{ | |
| 268 | - if(beanClass == null){ | |
| 311 | + } | |
| 312 | + | |
| 313 | + public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass, boolean isCopyNull, | |
| 314 | + String exceptFieldNameArray[]) throws Exception { | |
| 315 | + if (beanClass == null) { | |
| 269 | 316 | return null; |
| 270 | 317 | } |
| 271 | 318 | E result = beanClass.newInstance(); |
| 272 | 319 | Iterator<String> keyIter = dataMap.keySet().iterator(); |
| 273 | - while(keyIter.hasNext()){ | |
| 320 | + while (keyIter.hasNext()) { | |
| 274 | 321 | String keyName = keyIter.next(); |
| 275 | 322 | String camelName = UtilString.camelName(keyName); |
| 276 | 323 | Object keyValue = dataMap.get(keyName); |
| 277 | - | |
| 278 | - if(keyValue == null){ | |
| 324 | + | |
| 325 | + if (keyValue == null) { | |
| 326 | + continue; | |
| 327 | + } | |
| 328 | + | |
| 329 | + Field keyField = findDeclaredField(beanClass, camelName); | |
| 330 | + if (keyField == null) { | |
| 279 | 331 | continue; |
| 280 | 332 | } |
| 281 | - | |
| 282 | - Field keyField = beanClass.getDeclaredField(camelName); | |
| 283 | 333 | String keyTypeName = keyField.getType().getSimpleName(); |
| 284 | - | |
| 285 | - //如果是final关键词修饰,则返回 | |
| 286 | - if(Modifier.isFinal(keyField.getModifiers())){ | |
| 334 | + | |
| 335 | + // 如果是final关键词修饰,则返回 | |
| 336 | + if (Modifier.isFinal(keyField.getModifiers())) { | |
| 287 | 337 | continue; |
| 288 | 338 | } |
| 289 | - | |
| 290 | - if(keyTypeName.equals("String")){ | |
| 339 | + | |
| 340 | + if (keyTypeName.equals("String")) { | |
| 291 | 341 | keyField.set(result, keyValue.toString()); |
| 292 | - }else if(keyTypeName.equals("Integer")){ | |
| 342 | + } else if (keyTypeName.equals("Integer")) { | |
| 293 | 343 | keyField.set(result, Integer.valueOf(keyValue.toString())); |
| 294 | 344 | } |
| 295 | 345 | } |
| 296 | 346 | return result; |
| 297 | 347 | } |
| 298 | - | |
| 299 | - public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass) throws Exception{ | |
| 300 | - if(beanClass == null){ | |
| 348 | + | |
| 349 | + public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass) throws Exception { | |
| 350 | + if (beanClass == null) { | |
| 301 | 351 | return null; |
| 302 | 352 | } |
| 303 | 353 | E result = beanClass.newInstance(); |
| 304 | 354 | Iterator<String> keyIter = dataMap.keySet().iterator(); |
| 305 | - while(keyIter.hasNext()){ | |
| 355 | + while (keyIter.hasNext()) { | |
| 306 | 356 | String keyName = keyIter.next(); |
| 307 | 357 | String camelName = UtilString.camelName(keyName); |
| 308 | 358 | Object keyValue = dataMap.get(keyName); |
| 309 | - | |
| 310 | - if(keyValue == null){ | |
| 359 | + | |
| 360 | + if (keyValue == null) { | |
| 311 | 361 | continue; |
| 312 | 362 | } |
| 313 | - | |
| 363 | + | |
| 314 | 364 | try { |
| 315 | - Field keyField = beanClass.getDeclaredField(camelName); | |
| 316 | - | |
| 317 | - //如果是final关键词修饰,则返回 | |
| 318 | - if(Modifier.isFinal(keyField.getModifiers())){ | |
| 365 | + Field keyField = findDeclaredField(beanClass, camelName); | |
| 366 | + if (keyField == null) { | |
| 367 | + continue; | |
| 368 | + } | |
| 369 | + | |
| 370 | + // 如果是final关键词修饰,则返回 | |
| 371 | + if (Modifier.isFinal(keyField.getModifiers())) { | |
| 319 | 372 | continue; |
| 320 | 373 | } |
| 321 | - | |
| 374 | + | |
| 322 | 375 | keyField.setAccessible(true); |
| 323 | 376 | keyField.set(result, keyValue); |
| 324 | - } catch (Exception e) { } | |
| 377 | + } catch (Exception e) { | |
| 378 | + } | |
| 325 | 379 | } |
| 326 | 380 | return result; |
| 327 | 381 | } |
| 328 | - | |
| 329 | - public static void main(String args[]){ | |
| 330 | - | |
| 331 | - class Temp2{ | |
| 382 | + | |
| 383 | + private static Field findDeclaredField(Class beanClass, String camelName) { | |
| 384 | + Field[] dataArr = getAndCachedFields(beanClass); | |
| 385 | + if (dataArr == null || dataArr.length == 0) { | |
| 386 | + return null; | |
| 387 | + } | |
| 388 | + for (Field item : dataArr) { | |
| 389 | + if (item.getName().equals(camelName)) { | |
| 390 | + return item; | |
| 391 | + } | |
| 392 | + } | |
| 393 | + return null; | |
| 394 | + } | |
| 395 | + | |
| 396 | + public static void main(String args[]) { | |
| 397 | + | |
| 398 | + class Temp2 { | |
| 332 | 399 | final Integer a; |
| 333 | 400 | Integer c; |
| 334 | - public Temp2(Integer a, Integer c){ | |
| 401 | + | |
| 402 | + public Temp2(Integer a, Integer c) { | |
| 335 | 403 | this.a = a; |
| 336 | 404 | this.c = c; |
| 337 | 405 | } |
| 406 | + | |
| 338 | 407 | @Override |
| 339 | - public String toString(){ | |
| 340 | - return "a="+a+":c="+c; | |
| 408 | + public String toString() { | |
| 409 | + return "a=" + a + ":c=" + c; | |
| 341 | 410 | } |
| 342 | 411 | } |
| 343 | 412 | |
| 344 | - class Temp{ | |
| 413 | + class Temp extends Temp2 { | |
| 345 | 414 | Integer a; |
| 346 | 415 | Integer b; |
| 347 | - public Temp(Integer a, Integer b){ | |
| 416 | + | |
| 417 | + public Temp(Integer a, Integer b) { | |
| 418 | + super(a, b); | |
| 348 | 419 | this.a = a; |
| 349 | 420 | this.b = b; |
| 350 | 421 | } |
| 422 | + | |
| 351 | 423 | @Override |
| 352 | - public String toString(){ | |
| 353 | - return "a="+a+":b="+b; | |
| 424 | + public String toString() { | |
| 425 | + return "a=" + a + ":b=" + b; | |
| 354 | 426 | } |
| 355 | 427 | } |
| 356 | - | |
| 357 | - Temp temp1 = new Temp(null,2); | |
| 358 | - Temp temp2 = new Temp(3,4); | |
| 359 | - Temp temp3 = new Temp(5,6); | |
| 360 | - Temp2 temp4 = new Temp2(7,8); | |
| 361 | - | |
| 428 | + | |
| 429 | + Temp temp1 = new Temp(null, 2); | |
| 430 | + Temp temp2 = new Temp(3, 4); | |
| 431 | + Temp temp3 = new Temp(5, 6); | |
| 432 | + Temp2 temp4 = new Temp2(7, 8); | |
| 433 | + | |
| 362 | 434 | try { |
| 363 | - Field a= temp4.getClass().getDeclaredField("a"); | |
| 364 | - System.out.print(Modifier.isFinal(a.getModifiers())); | |
| 365 | - } catch (NoSuchFieldException e) { | |
| 366 | - // TODO Auto-generated catch block | |
| 367 | - e.printStackTrace(); | |
| 435 | +//// Field a= temp4.getClass().getDeclaredField("a"); | |
| 436 | +//// System.out.print(Modifier.isFinal(a.getModifiers())); | |
| 437 | +// getAndCachedFields(Temp.class); | |
| 368 | 438 | } catch (SecurityException e) { |
| 369 | 439 | // TODO Auto-generated catch block |
| 370 | 440 | e.printStackTrace(); |
| 371 | 441 | } |
| 442 | + | |
| 372 | 443 | |
| 373 | - /*fieldCopy(temp1, temp2, false, null,false); | |
| 374 | - System.out.println(temp1.toString()); | |
| 375 | - System.out.println(temp2.toString()); | |
| 376 | - | |
| 377 | - fieldCopy(temp3, temp4, false, null,false); | |
| 378 | - System.out.println(temp3.toString()); | |
| 379 | - System.out.println(temp4.toString()); | |
| 380 | - | |
| 381 | - List<Object[]> temp = UtilObject.fieldToArray(temp1, false, new String[]{"b"}); | |
| 382 | - for(int i=0; i<temp.size(); ++i){ | |
| 383 | - System.out.println(temp.get(i)[0]+":"+temp.get(i)[1]); | |
| 384 | - } */ | |
| 444 | + fieldCopy(temp1, temp2, false, null,false); | |
| 445 | + System.out.println(temp1.toString()); System.out.println(temp2.toString()); | |
| 446 | + | |
| 447 | + fieldCopy(temp3, temp4, false, null,false); | |
| 448 | + System.out.println(temp3.toString()); System.out.println(temp4.toString()); | |
| 449 | + | |
| 450 | + List<Object[]> temp = UtilObject.fieldToArray(temp1, false, new | |
| 451 | + String[]{"b"}); for(int i=0; i<temp.size(); ++i){ | |
| 452 | + System.out.println(temp.get(i)[0]+":"+temp.get(i)[1]); } | |
| 453 | + | |
| 385 | 454 | } |
| 386 | 455 | } |
| 387 | 456 | \ No newline at end of file | ... | ... |