程式整理
範例
假Bean(含有Map) to 真Bean
- 靜態(static)方法回傳泛型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
public static void copyDataObjectToBean(DataObject src, DataObject dest) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Map map = src.toMap();
Set set = map.keySet();
Iterator<String> it = set.iterator();
PropertyDescriptor[] propertiesStr = PropertyUtils.getPropertyDescriptors(dest);
System.out.println("Map:"+map);
for(PropertyDescriptor propertyDescriptor :
Introspector.getBeanInfo(dest.getClass()).getPropertyDescriptors()){
// propertyEditor.getReadMethod() exposes the getter
// btw, this may be null if you have a write-only property
Method method = propertyDescriptor.getReadMethod();
String methodStr = propertyDescriptor.getReadMethod().getName().substring(3);
String beanKey = propertyDescriptor.getReadMethod().getName().substring(3).toUpperCase();
String name = underscoreName(propertyDescriptor.getName());
if(!map.containsKey(name)) {
continue;
}
String value = MapUtils.getString(map, name);
// System.out.println("beanKey:" + propertyDescriptor.getName());
BeanUtils.setProperty(dest, propertyDescriptor.getName(), value);
}
}
public static <T extends DataObject> T copyDataObjectToBean(DataObject src, Class<T> clazz) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
Map map = src.toMap();
Set set = map.keySet();
Iterator<String> it = set.iterator();
DataObject dest = clazz.newInstance();
PropertyDescriptor[] propertiesStr = PropertyUtils.getPropertyDescriptors(dest);
System.out.println("Map:"+map);
for(PropertyDescriptor propertyDescriptor :
Introspector.getBeanInfo(dest.getClass()).getPropertyDescriptors()){
// propertyEditor.getReadMethod() exposes the getter
// btw, this may be null if you have a write-only property
Method method = propertyDescriptor.getReadMethod();
String methodStr = propertyDescriptor.getReadMethod().getName().substring(3);
String beanKey = propertyDescriptor.getReadMethod().getName().substring(3).toUpperCase();
String name = underscoreName(propertyDescriptor.getName());
if(!map.containsKey(name)) {
continue;
}
String value = MapUtils.getString(map, name);
// System.out.println("beanKey:" + propertyDescriptor.getName());
BeanUtils.setProperty(dest, propertyDescriptor.getName(), value);
}
return (T) dest;
}
/**
* 將駝峰式命名的字串轉換為下劃線大寫方式。如果轉換前的駝峰式命名的字串為空,則返回空字串。</br>
* 例如:HelloWorld->HELLO_WORLD
* @param name 轉換前的駝峰式命名的字串
* @return 轉換後下劃線大寫方式命名的字串
*/
public static String underscoreName(String name) {
StringBuilder result = new StringBuilder();
if (name != null && name.length() > 0) {
// 將第一個字元處理成大寫
result.append(name.substring(0, 1).toUpperCase());
// 迴圈處理其餘字元
for (int i = 1; i < name.length(); i++) {
String s = name.substring(i, i + 1);
// 在大寫字母前新增下劃線
if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
result.append("_");
}
// 其他字元直接轉成大寫
result.append(s.toUpperCase());
}
}
return result.toString();
}
/**
* 將下劃線大寫方式命名的字串轉換為駝峰式。如果轉換前的下劃線大寫方式命名的字串為空,則返回空字串。</br>
* 例如:HELLO_WORLD->HelloWorld
* @param name 轉換前的下劃線大寫方式命名的字串
* @return 轉換後的駝峰式命名的字串
*/
public static String camelName(String name) {
StringBuilder result = new StringBuilder();
// 快速檢查
if (name == null || name.isEmpty()) {
// 沒必要轉換
return "";
} else if (!name.contains("_")) {
// 不含下劃線,僅將首字母小寫
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
// 用下劃線將原始字串分割
String camels[] = name.split("_");
for (String camel : camels) {
// 跳過原始字串中開頭、結尾的下換線或雙重下劃線
if (camel.isEmpty()) {
continue;
}
// 處理真正的駝峰片段
if (result.length() == 0) {
// 第一個駝峰片段,全部字母都小寫
result.append(camel.toLowerCase());
} else {
// 其他的駝峰片段,首字母大寫
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
}
return result.toString();
}
|
網路範例
1
2
3
4
5
6
7
|
for(PropertyDescriptor propertyDescriptor :
Introspector.getBeanInfo(yourClass).getPropertyDescriptors()){
// propertyEditor.getReadMethod() exposes the getter
// btw, this may be null if you have a write-only property
System.out.println(propertyDescriptor.getReadMethod());
}
|
[Solved] Java Reflection: How can I get the all getter methods of a java class and invoke them - Code Redirect
理論整理
Spring的BeanUtils實現忽略大小寫的copyProperties()方法! - 台部落
关于属性描述符PropertyDescriptor - 猫叔的博客 | MySelf
[详叙BeanWrapper和PropertyDescriptor - YourBatman - 博客园](https://www.cnblogs.com/yourbatman/p/11212258.html
小信豬的原始部落: Java 學習筆記 (10) - Reflection
菜鳥工程師 肉豬: Java 使用Class.forName().newinstance()產生物件實例。