實例如下:
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; /** * 獲取實體類型的屬性名和類型 * @param model 為實體類 * @author kou 為傳入參數 */ public class GetModelNameAndType { public static void testReflect(Object model) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { // 獲取實體類的所有屬性,返回Field數組 Field[] field = model.getClass().getDeclaredFields(); // 獲取屬性的名字 String[] modelName = new String[field.length]; String[] modelType = new String[field.length]; for ( int i = 0 ; i < field.length; i++) { // 獲取屬性的名字 String name = field[i].getName(); modelName[i] = name; // 獲取屬性類型 String type = field[i].getGenericType().toString(); modelType[i] = type; //關鍵。。??稍L問私有變量 field[i].setAccessible( true ); //給屬性設置 field[i].set(model, field[i].getType().getConstructor(field[i].getType()).newInstance( "kou" )); // 將屬性的首字母大寫 name = name.replaceFirst(name.substring( 0 , 1 ), name.substring( 0 , 1 ) .toUpperCase()); if (type.equals( "class java.lang.String" )) { // 如果type是類類型,則前面包含"class ",后面跟類名 Method m = model.getClass().getMethod( "get" + name); // 調用getter方法獲取屬性值 String value = (String) m.invoke(model); if (value != null ) { System.out.println( "attribute value:" + value); } } if (type.equals( "class java.lang.Integer" )) { Method m = model.getClass().getMethod( "get" + name); Integer value = (Integer) m.invoke(model); if (value != null ) { System.out.println( "attribute value:" + value); } } if (type.equals( "class java.lang.Short" )) { Method m = model.getClass().getMethod( "get" + name); Short value = (Short) m.invoke(model); if (value != null ) { System.out.println( "attribute value:" + value); } } if (type.equals( "class java.lang.Double" )) { Method m = model.getClass().getMethod( "get" + name); Double value = (Double) m.invoke(model); if (value != null ) { System.out.println( "attribute value:" + value); } } if (type.equals( "class java.lang.Boolean" )) { Method m = model.getClass().getMethod( "get" + name); Boolean value = (Boolean) m.invoke(model); if (value != null ) { System.out.println( "attribute value:" + value); } } if (type.equals( "class java.util.Date" )) { Method m = model.getClass().getMethod( "get" + name); Date value = (Date) m.invoke(model); if (value != null ) { System.out.println( "attribute value:" + value.toLocaleString()); } } } } public static void main(String[] args) { try { testReflect( new VO()); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } } |
以上這篇java反射遍歷實體類屬性和類型,并賦值和獲取值的簡單方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。