Commit 82966b3beb0793e42b9188e46e4b6ee9ab9000de
1 parent
0bedf9bb
Exists in
master
optimized UtilObject
Showing
2 changed files
with
103 additions
and
3 deletions
Show diff stats
src/main/java/com/taover/util/UtilObject.java
@@ -2,7 +2,9 @@ package com.taover.util; | @@ -2,7 +2,9 @@ package com.taover.util; | ||
2 | 2 | ||
3 | import java.lang.reflect.Field; | 3 | import java.lang.reflect.Field; |
4 | import java.util.ArrayList; | 4 | import java.util.ArrayList; |
5 | +import java.util.Iterator; | ||
5 | import java.util.List; | 6 | import java.util.List; |
7 | +import java.util.Map; | ||
6 | 8 | ||
7 | public class UtilObject { | 9 | public class UtilObject { |
8 | /** | 10 | /** |
@@ -96,7 +98,81 @@ public class UtilObject { | @@ -96,7 +98,81 @@ public class UtilObject { | ||
96 | } | 98 | } |
97 | 99 | ||
98 | return newDest; | 100 | return newDest; |
99 | - } | 101 | + } |
102 | + | ||
103 | + /** | ||
104 | + * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; | ||
105 | + * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer | ||
106 | + * @param source | ||
107 | + * @param destClass | ||
108 | + * @param allowNull | ||
109 | + * @param exceptFieldNameArray | ||
110 | + * @return 如果source==null或者destClass==null,则返回false | ||
111 | + */ | ||
112 | + public static <E> E fieldCopy(Object source, Class<E> destClass, boolean allowNull, String exceptFieldNameArray[]) throws Exception{ | ||
113 | + //引用检查 | ||
114 | + if(source==null || destClass==null){ | ||
115 | + return null; | ||
116 | + } | ||
117 | + | ||
118 | + //获取Field数组 | ||
119 | + Field[] sourceField = source.getClass().getDeclaredFields(); | ||
120 | + Field[] destField = destClass.getDeclaredFields(); | ||
121 | + | ||
122 | + //获取目标对象 | ||
123 | + String[] destFieldNameArray = new String[destField.length]; | ||
124 | + String[] destFieldTypeArray = new String[destField.length]; | ||
125 | + for(int i=0; i<destField.length; ++i){ | ||
126 | + destFieldNameArray[i] = destField[i].getName(); | ||
127 | + destFieldTypeArray[i] = destField[i].getType().getName(); | ||
128 | + } | ||
129 | + | ||
130 | + E newDest = destClass.newInstance(); | ||
131 | + | ||
132 | + //遍历源field数组,处理源field数组 | ||
133 | + for(int i=0; i<sourceField.length; ++i){ | ||
134 | + //获取源field相关信息 | ||
135 | + Object sourceFieldValue = null; | ||
136 | + String sourceFieldName = sourceField[i].getName(); | ||
137 | + String sourceFieldType = sourceField[i].getType().getName(); | ||
138 | + | ||
139 | + //获取sourceFieldValue值 | ||
140 | + sourceField[i].setAccessible(true); | ||
141 | + try { | ||
142 | + sourceFieldValue = sourceField[i].get(source); | ||
143 | + } catch (Exception e1) { | ||
144 | + e1.printStackTrace(); | ||
145 | + } | ||
146 | + | ||
147 | + //检查是否在exceptFieldName中 | ||
148 | + if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){ | ||
149 | + continue; | ||
150 | + } | ||
151 | + | ||
152 | + //检验sourceFieldValue是否为NULL | ||
153 | + if(!allowNull && sourceFieldValue == null){ | ||
154 | + continue; | ||
155 | + } | ||
156 | + | ||
157 | + //检查源field字段名是否在目标field组中存在 | ||
158 | + int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray); | ||
159 | + if(nameIndex == -1){ | ||
160 | + continue; | ||
161 | + }else{ | ||
162 | + //如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象 | ||
163 | + if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){ | ||
164 | + destField[nameIndex].setAccessible(true); | ||
165 | + try { | ||
166 | + destField[nameIndex].set(newDest, sourceFieldValue); | ||
167 | + } catch (Exception e) { | ||
168 | + e.printStackTrace(); | ||
169 | + } | ||
170 | + } | ||
171 | + } | ||
172 | + } | ||
173 | + | ||
174 | + return newDest; | ||
175 | + } | ||
100 | 176 | ||
101 | /** | 177 | /** |
102 | * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性 | 178 | * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性 |
@@ -194,6 +270,32 @@ public class UtilObject { | @@ -194,6 +270,32 @@ public class UtilObject { | ||
194 | } | 270 | } |
195 | 271 | ||
196 | return result; | 272 | return result; |
273 | + } | ||
274 | + | ||
275 | + public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass, boolean isCopyNull, String exceptFieldNameArray[]) throws Exception{ | ||
276 | + if(beanClass == null){ | ||
277 | + return null; | ||
278 | + } | ||
279 | + E result = beanClass.newInstance(); | ||
280 | + Iterator<String> keyIter = dataMap.keySet().iterator(); | ||
281 | + while(keyIter.hasNext()){ | ||
282 | + String keyName = keyIter.next(); | ||
283 | + String camelName = UtilString.camelName(keyName); | ||
284 | + Object keyValue = dataMap.get(keyName); | ||
285 | + | ||
286 | + if(keyValue == null){ | ||
287 | + continue; | ||
288 | + } | ||
289 | + | ||
290 | + Field keyField = beanClass.getDeclaredField(camelName); | ||
291 | + String keyTypeName = keyField.getType().getSimpleName(); | ||
292 | + if(keyTypeName.equals("String")){ | ||
293 | + keyField.set(result, keyValue.toString()); | ||
294 | + }else if(keyTypeName.equals("Integer")){ | ||
295 | + keyField.set(result, Integer.valueOf(keyValue.toString())); | ||
296 | + } | ||
297 | + } | ||
298 | + return result; | ||
197 | } | 299 | } |
198 | 300 | ||
199 | public static void main(String args[]){ | 301 | public static void main(String args[]){ |
src/main/java/com/taover/util/bean/ResultInfoException.java
@@ -7,8 +7,6 @@ public class ResultInfoException extends Exception { | @@ -7,8 +7,6 @@ public class ResultInfoException extends Exception { | ||
7 | private String error; | 7 | private String error; |
8 | private Object data; | 8 | private Object data; |
9 | 9 | ||
10 | - private ResultInfoException(){} | ||
11 | - | ||
12 | protected ResultInfoException(String code, String error, Object data){ | 10 | protected ResultInfoException(String code, String error, Object data){ |
13 | this.code = code; | 11 | this.code = code; |
14 | this.error = error; | 12 | this.error = error; |