|
1 | 1 | /** |
2 | 2 | * Copyright 2015-2023 yanglb.com |
3 | | - * |
| 3 | + * <p> |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
| 7 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
19 | 19 | import com.yanglb.codegen.utils.ObjectUtil; |
20 | 20 | import com.yanglb.codegen.utils.Resources; |
21 | 21 | import com.yanglb.codegen.utils.StringUtil; |
| 22 | + |
22 | 23 | import java.lang.reflect.Field; |
23 | 24 | import java.util.Map; |
24 | 25 |
|
25 | 26 |
|
26 | 27 | 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 = ObjectUtil.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(!StringUtil.isNullOrEmpty(mapValue)) { |
51 | | - value = this.toBoolean(mapValue); |
52 | | - } |
53 | | - } else if (Boolean.class.equals(type)) { |
54 | | - value = null; |
55 | | - if(!StringUtil.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(Resources.getString("E_001"), key, e.getMessage())); |
64 | | - } |
65 | | - } else if ( Integer.class.equals(type)) { |
66 | | - // 可以为Null的整型 |
67 | | - if(StringUtil.isNullOrEmpty(mapValue)) { |
68 | | - value = null; |
69 | | - } else { |
70 | | - try{ |
71 | | - value = Integer.parseInt(mapValue); |
72 | | - }catch(NumberFormatException e) { |
73 | | - throw new CodeGenException(String.format(Resources.getString("E_001"), key, e.getMessage())); |
74 | | - } |
75 | | - } |
76 | | - } else { |
77 | | - value = mapValue; |
78 | | - } |
79 | | - field.set(result, value); |
80 | | - } |
81 | | - } catch (NoSuchFieldException e) { |
82 | | - throw new CodeGenException(String.format(Resources.getString("E_002"), e.getMessage())); |
83 | | - } catch (SecurityException e) { |
84 | | - throw new CodeGenException(e.getMessage()); |
85 | | - } catch (Exception e) { |
86 | | - throw new CodeGenException(e.getMessage()); |
87 | | - } |
88 | | - return result; |
89 | | - } |
90 | | - |
91 | | - private boolean toBoolean(String v) { |
92 | | - boolean value = false; |
93 | | - v = v.toLowerCase(); |
94 | | - if("y".equals(v) |
95 | | - || "√".equals(v) |
96 | | - || "yes".equals(v) |
97 | | - || "true".equals(v)){ |
98 | | - value = true; |
99 | | - } |
100 | | - return value; |
101 | | - } |
| 28 | + |
| 29 | + /** |
| 30 | + * 将Map转换为 cls类的对象 |
| 31 | + * @param cls 将要转换为的类 |
| 32 | + * @param map 保存数据的Map |
| 33 | + * @return 转换结果 |
| 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 = ObjectUtil.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 (!StringUtil.isNullOrEmpty(mapValue)) { |
| 51 | + value = this.toBoolean(mapValue); |
| 52 | + } |
| 53 | + } else if (Boolean.class.equals(type)) { |
| 54 | + value = null; |
| 55 | + if (!StringUtil.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(Resources.getString("E_001"), key, e.getMessage())); |
| 64 | + } |
| 65 | + } else if (Integer.class.equals(type)) { |
| 66 | + // 可以为Null的整型 |
| 67 | + if (StringUtil.isNullOrEmpty(mapValue)) { |
| 68 | + value = null; |
| 69 | + } else { |
| 70 | + try { |
| 71 | + value = Integer.parseInt(mapValue); |
| 72 | + } catch (NumberFormatException e) { |
| 73 | + throw new CodeGenException(String.format(Resources.getString("E_001"), key, e.getMessage())); |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + value = mapValue; |
| 78 | + } |
| 79 | + field.set(result, value); |
| 80 | + } |
| 81 | + } catch (NoSuchFieldException e) { |
| 82 | + throw new CodeGenException(String.format(Resources.getString("E_002"), e.getMessage())); |
| 83 | + } catch (SecurityException e) { |
| 84 | + throw new CodeGenException(e.getMessage()); |
| 85 | + } catch (Exception e) { |
| 86 | + throw new CodeGenException(e.getMessage()); |
| 87 | + } |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + private boolean toBoolean(String v) { |
| 92 | + boolean value = false; |
| 93 | + v = v.toLowerCase(); |
| 94 | + if ("y".equals(v) |
| 95 | + || "√".equals(v) |
| 96 | + || "yes".equals(v) |
| 97 | + || "true".equals(v)) { |
| 98 | + value = true; |
| 99 | + } |
| 100 | + return value; |
| 101 | + } |
102 | 102 | } |
0 commit comments