java獲取Object屬性型別、屬性名稱、屬性值
阿新 • • 發佈:2019-02-14
轉載自
點選開啟連結因為專案需要用到,於是簡單封裝了一些常用的操作:
[java] view plain copy print?- /**
- * 根據屬性名獲取屬性值
- * */
- private Object getFieldValueByName(String fieldName, Object o) {
- try {
- String firstLetter = fieldName.substring(0, 1).toUpperCase();
- String getter = "get" + firstLetter + fieldName.substring(
- Method method = o.getClass().getMethod(getter, new Class[] {});
- Object value = method.invoke(o, new Object[] {});
- return value;
- } catch (Exception e) {
- log.error(e.getMessage(),e);
- returnnull;
- }
- }
- /**
- * 獲取屬性名陣列
- * */
- private String[] getFiledName(Object o){
- Field[] fields=o.getClass().getDeclaredFields();
- String[] fieldNames=new String[fields.length];
- for(int i=0;i<fields.length;i++){
- System.out.println(fields[i].getType());
- fieldNames[i]=fields[i].getName();
- }
- return fieldNames;
- }
- /**
- * 獲取屬性型別(type),屬性名(name),屬性值(value)的map組成的list
- * */
- private List getFiledsInfo(Object o){
- Field[] fields=o.getClass().getDeclaredFields();
- String[] fieldNames=new String[fields.length];
- List list = new ArrayList();
- Map infoMap=null;
- for(int i=0;i<fields.length;i++){
- infoMap = new HashMap();
- infoMap.put("type", fields[i].getType().toString());
- infoMap.put("name", fields[i].getName());
- infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
- list.add(infoMap);
- }
- return list;
- }
- /**
- * 獲取物件的所有屬性值,返回一個物件陣列
- * */
- public Object[] getFiledValues(Object o){
- String[] fieldNames=this.getFiledName(o);
- Object[] value=new Object[fieldNames.length];
- for(int i=0;i<fieldNames.length;i++){
- value[i]=this.getFieldValueByName(fieldNames[i], o);
- }
- return value;
- }
/**
* 根據屬性名獲取屬性值
* */
private Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e) {
log.error(e.getMessage(),e);
return null;
}
}
/**
* 獲取屬性名陣列
* */
private String[] getFiledName(Object o){
Field[] fields=o.getClass().getDeclaredFields();
String[] fieldNames=new String[fields.length];
for(int i=0;i<fields.length;i++){
System.out.println(fields[i].getType());
fieldNames[i]=fields[i].getName();
}
return fieldNames;
}
/**
* 獲取屬性型別(type),屬性名(name),屬性值(value)的map組成的list
* */
private List getFiledsInfo(Object o){
Field[] fields=o.getClass().getDeclaredFields();
String[] fieldNames=new String[fields.length];
List list = new ArrayList();
Map infoMap=null;
for(int i=0;i<fields.length;i++){
infoMap = new HashMap();
infoMap.put("type", fields[i].getType().toString());
infoMap.put("name", fields[i].getName());
infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
list.add(infoMap);
}
return list;
}
/**
* 獲取物件的所有屬性值,返回一個物件陣列
* */
public Object[] getFiledValues(Object o){
String[] fieldNames=this.getFiledName(o);
Object[] value=new Object[fieldNames.length];
for(int i=0;i<fieldNames.length;i++){
value[i]=this.getFieldValueByName(fieldNames[i], o);
}
return value;
}