Commit d4db6732bc6aa318716ba8cb270d16ffa249127d
1 parent
1a86ce60a3
Exists in
master
and in
1 other branch
添加了两个工具类:数组合并和bean的copy
Showing 2 changed files with 205 additions and 0 deletions
platform-common/src/main/java/com/lyms/platform/common/utils/ArrayUtils.java
View file @
d4db673
1 | +package com.lyms.platform.common.utils; | |
2 | + | |
3 | +import java.lang.reflect.Array; | |
4 | + | |
5 | +/** | |
6 | + * @auther HuJiaqi | |
7 | + * @createTime 2016年11月21日 10时14分 | |
8 | + * @discription | |
9 | + */ | |
10 | +public class ArrayUtils { | |
11 | + | |
12 | + /** | |
13 | + * @auther HuJiaqi | |
14 | + * @createTime 2016年04月08日 13时10分 | |
15 | + * @discription 数组合并,仅支持相同类型的数组合并 | |
16 | + */ | |
17 | + public static <T> T[] merge(T[] array1, T[] array2) { | |
18 | + if ((array1 == null) && (array2 == null)) | |
19 | + return null; | |
20 | + if ((array2 == null) || (array2.length == 0)) | |
21 | + return array1; | |
22 | + if ((array1 == null) || (array1.length == 0)) | |
23 | + return array2; | |
24 | + Class c = array1.getClass().getComponentType(); | |
25 | + Object[] array = (Object[]) Array.newInstance(c, array1.length + array2.length); | |
26 | + System.arraycopy(array1, 0, array, 0, array1.length); | |
27 | + System.arraycopy(array2, 0, array, array1.length, array2.length); | |
28 | + T[] result = (T[]) array; | |
29 | + return result; | |
30 | + } | |
31 | + | |
32 | +} |
platform-common/src/main/java/com/lyms/platform/common/utils/BeanUtils.java
View file @
d4db673
1 | +package com.lyms.platform.common.utils; | |
2 | + | |
3 | +import java.beans.BeanInfo; | |
4 | +import java.beans.Introspector; | |
5 | +import java.beans.PropertyDescriptor; | |
6 | +import java.io.ByteArrayInputStream; | |
7 | +import java.io.ByteArrayOutputStream; | |
8 | +import java.io.ObjectInputStream; | |
9 | +import java.io.ObjectOutputStream; | |
10 | +import java.lang.annotation.*; | |
11 | +import java.lang.reflect.Field; | |
12 | +import java.util.HashMap; | |
13 | +import java.util.Map; | |
14 | + | |
15 | +/** | |
16 | + * @auther HuJiaqi | |
17 | + * @createTime 2016年11月21日 10时13分 | |
18 | + * @discription | |
19 | + */ | |
20 | +public class BeanUtils { | |
21 | + | |
22 | + /** | |
23 | + * @auther HuJiaqi | |
24 | + * @createTime 2016年04月07日 14时12分 | |
25 | + * @discription byte数组转object,建议处理异常 | |
26 | + */ | |
27 | + public static Object bytesToObject(byte[] b) { | |
28 | + try { | |
29 | + ByteArrayInputStream bis = new ByteArrayInputStream(b); | |
30 | + ObjectInputStream ois = new ObjectInputStream(bis); | |
31 | + Object obj = ois.readObject(); | |
32 | + ois.close(); | |
33 | + bis.close(); | |
34 | + return obj; | |
35 | + } catch (Exception e) { | |
36 | + throw new RuntimeException("byte数组转object异常", e); | |
37 | + } | |
38 | + } | |
39 | + | |
40 | + /** | |
41 | + * @auther HuJiaqi | |
42 | + * @createTime 2016年04月07日 14时14分 | |
43 | + * @discription object转byte数组,建议处理异常 | |
44 | + */ | |
45 | + public static byte[] objectToBytes(Object obj) { | |
46 | + try { | |
47 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
48 | + ObjectOutputStream oos = new ObjectOutputStream(bos); | |
49 | + oos.writeObject(obj); | |
50 | + oos.flush(); | |
51 | + byte[] b = bos.toByteArray(); | |
52 | + oos.close(); | |
53 | + bos.close(); | |
54 | + return b; | |
55 | + } catch (Exception e) { | |
56 | + throw new RuntimeException("object转byte数组异常", e); | |
57 | + } | |
58 | + } | |
59 | + | |
60 | + /** | |
61 | + * @auther HuJiaqi | |
62 | + * @createTime 2016年04月07日 14时41分 | |
63 | + * @discription map转object,建议处理异常 | |
64 | + */ | |
65 | + public static Object mapToObject(Class c, Map map) { | |
66 | + try { | |
67 | + BeanInfo bi = Introspector.getBeanInfo(c); | |
68 | + Object obj = c.newInstance(); | |
69 | + PropertyDescriptor[] pds = bi.getPropertyDescriptors(); | |
70 | + for (PropertyDescriptor pd : pds) { | |
71 | + String name = pd.getName(); | |
72 | + if (map.containsKey(name)) { | |
73 | + pd.getWriteMethod().invoke(obj, map.get(name)); | |
74 | + } | |
75 | + } | |
76 | + return obj; | |
77 | + } catch (Exception e) { | |
78 | + throw new RuntimeException("map转object异常" + e); | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + /** | |
83 | + * @auther HuJiaqi | |
84 | + * @createTime 2016年04月07日 14时47分 | |
85 | + * @discription object转map,建议处理异常 | |
86 | + */ | |
87 | + public static Map<String, String> objectToMap(Object obj) { | |
88 | + try { | |
89 | + Class c = obj.getClass(); | |
90 | + Map<String, String> map = new HashMap<String, String>(); | |
91 | + BeanInfo bi = Introspector.getBeanInfo(c); | |
92 | + PropertyDescriptor[] pds = bi.getPropertyDescriptors(); | |
93 | + for (PropertyDescriptor pd : pds) { | |
94 | + String key = pd.getName(); | |
95 | + if (!key.equals("class")) { | |
96 | + Object result = pd.getReadMethod().invoke(obj); | |
97 | + if (result != null) { | |
98 | + map.put(key, result.toString()); | |
99 | + } else { | |
100 | + map.put(key, ""); | |
101 | + } | |
102 | + } | |
103 | + } | |
104 | + return map; | |
105 | + } catch (Exception e) { | |
106 | + throw new RuntimeException("object转map异常" + e); | |
107 | + } | |
108 | + } | |
109 | + | |
110 | + /** | |
111 | + * @auther HuJiaqi | |
112 | + * @createTime 2016年04月07日 14时24分 | |
113 | + * @discription 修饰filed的自定义注解,用于复制bean | |
114 | + */ | |
115 | + @Target(ElementType.FIELD) | |
116 | + @Retention(RetentionPolicy.RUNTIME) | |
117 | + @Documented | |
118 | + public @interface CopyName { | |
119 | + String value() default ""; | |
120 | + } | |
121 | + | |
122 | + /** | |
123 | + * @auther HuJiaqi | |
124 | + * @createTime 2016年04月07日 16时33分 | |
125 | + * @discription 这个方法默认子类不会有与父类相同的field,没有处理这种情况 | |
126 | + */ | |
127 | + public static void copy(Object from, Object to) { | |
128 | + if (from == null || to == null) { | |
129 | + return; | |
130 | + } | |
131 | + Class c = from.getClass(); | |
132 | + Field[] fields = c.getDeclaredFields(); | |
133 | + c = c.getSuperclass(); | |
134 | + while (c != null) { | |
135 | + Field[] temp = c.getDeclaredFields(); | |
136 | + fields = ArrayUtils.merge(fields, temp); | |
137 | + c = c.getSuperclass(); | |
138 | + } | |
139 | + if (fields != null && fields.length > 0) { | |
140 | + for (Field fromField : fields) { | |
141 | + fromField.setAccessible(true); | |
142 | + String fieldName; | |
143 | + if (fromField.isAnnotationPresent(CopyName.class)) { | |
144 | + CopyName adjuster = fromField.getAnnotation(CopyName.class); | |
145 | + fieldName = !"".equals(adjuster.value()) ? adjuster.value() : fromField.getName(); | |
146 | + } else { | |
147 | + fieldName = fromField.getName(); | |
148 | + } | |
149 | + try { | |
150 | + Object obj = fromField.get(from); | |
151 | + Field toField = null; | |
152 | + c = to.getClass(); | |
153 | + while (c != null) { | |
154 | + try { | |
155 | + toField = c.getDeclaredField(fieldName); | |
156 | + break; | |
157 | + } catch (Exception e) { | |
158 | + c = c.getSuperclass(); | |
159 | + // 如果异常了,接着找父类 | |
160 | + } | |
161 | + } | |
162 | + if (toField != null) { | |
163 | + toField.setAccessible(true); | |
164 | + toField.set(to, obj); | |
165 | + } | |
166 | + } catch (Exception e) { | |
167 | + // 什么都不干,异常就当跳过了 | |
168 | + } | |
169 | + } | |
170 | + } | |
171 | + } | |
172 | + | |
173 | +} |