|
1 | | -/** |
2 | | - * Copyright 2015 yanglb.com |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | -package com.yanglb.utilitys.codegen.converter; |
17 | | - |
18 | | -import java.lang.reflect.Field; |
19 | | -import java.util.Map; |
20 | | - |
21 | | -import com.yanglb.utilitys.codegen.exceptions.CodeGenException; |
22 | | -import com.yanglb.utilitys.codegen.utility.MsgUtility; |
23 | | -import com.yanglb.utilitys.codegen.utility.ObjectUtility; |
24 | | -import com.yanglb.utilitys.codegen.utility.StringUtility; |
25 | | - |
26 | | -public class BeanMapConverter<T> { |
27 | | - |
28 | | - /** |
29 | | - * 将Map转换为 cls类的对象 |
30 | | - * @param cls 将要转换为的类 |
31 | | - * @param map 保存数据的Map |
32 | | - * @return 转换结果 |
33 | | - * @throws CodeGenException |
34 | | - */ |
35 | | - public T convert(Class<T> cls, Map<String, String> map) throws CodeGenException { |
36 | | - T result = null; |
37 | | - try { |
38 | | - result = cls.newInstance(); |
39 | | - for(String key:map.keySet()) { |
40 | | - Field field = ObjectUtility.getDeepField(result.getClass(), key); |
41 | | - field.setAccessible(true); |
42 | | - |
43 | | - String mapValue = map.get(key); |
44 | | - Object value = null; |
45 | | - |
46 | | - // 类型转换 |
47 | | - Class<?> type = field.getType(); |
48 | | - if(boolean.class.equals(type)) { |
49 | | - value = false; |
50 | | - if(!StringUtility.isNullOrEmpty(mapValue)) { |
51 | | - value = this.toBoolean(mapValue); |
52 | | - } |
53 | | - } else if (Boolean.class.equals(type)) { |
54 | | - value = null; |
55 | | - if(!StringUtility.isNullOrEmpty(mapValue)) { |
56 | | - value = this.toBoolean(mapValue); |
57 | | - } |
58 | | - } else if (int.class.equals(type)) { |
59 | | - // 直接转换,出错时向外抛异常 |
60 | | - try{ |
61 | | - value = Integer.parseInt(mapValue); |
62 | | - }catch(NumberFormatException e) { |
63 | | - throw new CodeGenException(String.format(MsgUtility.getString("E_001"), key, e.getMessage())); |
64 | | - } |
65 | | - } else if ( Integer.class.equals(type)) { |
66 | | - // 可以为Null的整型 |
67 | | - if(StringUtility.isNullOrEmpty(mapValue)) { |
68 | | - value = null; |
69 | | - } else { |
70 | | - try{ |
71 | | - value = Integer.parseInt(mapValue); |
72 | | - }catch(NumberFormatException e) { |
73 | | - throw new CodeGenException(String.format(MsgUtility.getString("E_001"), key, e.getMessage())); |
74 | | - } |
75 | | - } |
76 | | - } else { |
77 | | - // TODO: 其它类型的转换需要添加 |
78 | | - value = mapValue; |
79 | | - } |
80 | | - field.set(result, value); |
81 | | - } |
82 | | - } catch (InstantiationException | IllegalAccessException e) { |
83 | | - throw new CodeGenException(e.getMessage()); |
84 | | - } catch (NoSuchFieldException e) { |
85 | | - throw new CodeGenException(String.format(MsgUtility.getString("E_002"), e.getMessage())); |
86 | | - } catch (SecurityException e) { |
87 | | - throw new CodeGenException(e.getMessage()); |
88 | | - } |
89 | | - return result; |
90 | | - } |
91 | | - |
92 | | - private boolean toBoolean(String v) { |
93 | | - boolean value = false; |
94 | | - v = v.toLowerCase(); |
95 | | - if("y".equals(v) |
96 | | - || "yes".equals(v) |
97 | | - || "true".equals(v)){ |
98 | | - value = true; |
99 | | - } |
100 | | - return value; |
101 | | - } |
102 | | -} |
| 1 | +/** |
| 2 | + * Copyright 2015 yanglb.com |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.yanglb.utilitys.codegen.converter; |
| 17 | + |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import com.yanglb.utilitys.codegen.exceptions.CodeGenException; |
| 22 | +import com.yanglb.utilitys.codegen.utility.MsgUtility; |
| 23 | +import com.yanglb.utilitys.codegen.utility.ObjectUtility; |
| 24 | +import com.yanglb.utilitys.codegen.utility.StringUtility; |
| 25 | + |
| 26 | +public class BeanMapConverter<T> { |
| 27 | + |
| 28 | + /** |
| 29 | + * 将Map转换为 cls类的对象 |
| 30 | + * @param cls 将要转换为的类 |
| 31 | + * @param map 保存数据的Map |
| 32 | + * @return 转换结果 |
| 33 | + * @throws CodeGenException |
| 34 | + */ |
| 35 | + public T convert(Class<T> cls, Map<String, String> map) throws CodeGenException { |
| 36 | + T result = null; |
| 37 | + try { |
| 38 | + result = cls.newInstance(); |
| 39 | + for(String key:map.keySet()) { |
| 40 | + Field field = ObjectUtility.getDeepField(result.getClass(), key); |
| 41 | + field.setAccessible(true); |
| 42 | + |
| 43 | + String mapValue = map.get(key); |
| 44 | + Object value = null; |
| 45 | + |
| 46 | + // 类型转换 |
| 47 | + Class<?> type = field.getType(); |
| 48 | + if(boolean.class.equals(type)) { |
| 49 | + value = false; |
| 50 | + if(!StringUtility.isNullOrEmpty(mapValue)) { |
| 51 | + value = this.toBoolean(mapValue); |
| 52 | + } |
| 53 | + } else if (Boolean.class.equals(type)) { |
| 54 | + value = null; |
| 55 | + if(!StringUtility.isNullOrEmpty(mapValue)) { |
| 56 | + value = this.toBoolean(mapValue); |
| 57 | + } |
| 58 | + } else if (int.class.equals(type)) { |
| 59 | + // 直接转换,出错时向外抛异常 |
| 60 | + try{ |
| 61 | + value = Integer.parseInt(mapValue); |
| 62 | + }catch(NumberFormatException e) { |
| 63 | + throw new CodeGenException(String.format(MsgUtility.getString("E_001"), key, e.getMessage())); |
| 64 | + } |
| 65 | + } else if ( Integer.class.equals(type)) { |
| 66 | + // 可以为Null的整型 |
| 67 | + if(StringUtility.isNullOrEmpty(mapValue)) { |
| 68 | + value = null; |
| 69 | + } else { |
| 70 | + try{ |
| 71 | + value = Integer.parseInt(mapValue); |
| 72 | + }catch(NumberFormatException e) { |
| 73 | + throw new CodeGenException(String.format(MsgUtility.getString("E_001"), key, e.getMessage())); |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + // TODO: 其它类型的转换需要添加 |
| 78 | + value = mapValue; |
| 79 | + } |
| 80 | + field.set(result, value); |
| 81 | + } |
| 82 | + } catch (InstantiationException | IllegalAccessException e) { |
| 83 | + throw new CodeGenException(e.getMessage()); |
| 84 | + } catch (NoSuchFieldException e) { |
| 85 | + throw new CodeGenException(String.format(MsgUtility.getString("E_002"), e.getMessage())); |
| 86 | + } catch (SecurityException e) { |
| 87 | + throw new CodeGenException(e.getMessage()); |
| 88 | + } |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + private boolean toBoolean(String v) { |
| 93 | + boolean value = false; |
| 94 | + v = v.toLowerCase(); |
| 95 | + if("y".equals(v) |
| 96 | + || "√".equals(v) |
| 97 | + || "yes".equals(v) |
| 98 | + || "true".equals(v)){ |
| 99 | + value = true; |
| 100 | + } |
| 101 | + return value; |
| 102 | + } |
| 103 | +} |
0 commit comments