Java 遍历对象属性,并将属性值不为空的取出.
public void getValue(SelectIdListDto selectIdListDto){
Field[] field = selectIdListDto.getClass().getDeclaredFields();
for(int j=0 ; j<field.length ; j++){
String name = field[j].getName();
name = name.substring(0,1).toUpperCase()+name.substring(1);
String type = field[j].getGenericType().toString();
if(type.equals("class java.lang.String")){
Method m;
String value;
try {
m = selectIdListDto.getClass().getMethod("get"+name);
value = (String) m.invoke(selectIdListDto);
if(value != null && !"".equals(value)){
System.out.println(name);
System.out.println(value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
}