如何將常量類中的所有屬性和值,傳出來
阿新 • • 發佈:2019-02-05
以下方法能將常量類中的屬性和值轉換出來MAP
public
static
Map<String, Object>
ConstsProperty2Map(Class obj) {
Map<String, Object> map =
new
Hashtable<String, Object>();
Field[] field1 = obj.getDeclaredFields();
for
(
int
i =
0
; i < field1.length; i++) {
field1[i].setAccessible(
true
);
try
{
map.put(field1[i].getName(), field1[i].get(obj));
}
catch
(IllegalArgumentException e1) {
throw
new
STBSystemException(e1);
}
catch
(IllegalAccessException e2) {
throw
new
STBSystemException(e2);
}
}
return
map;
}