现用版本:
Class<?> clazz = object.getClass();
for (; clazz != Object.class; clazz = clazz.getSuperclass()) {//向上循环 遍历父类
Field[] field = clazz.getDeclaredFields();
for (Field f : field) {
f.setAccessible(true);
try {
if (null != f) {
if(f.get(object) == null || "".equals(f.get(object))){
stringBufferOne.append(f.getName()+"$");
stringBufferTwo.append("$");
System.out.println("属性:"+f.getName()+" 值:"+f.get(object));
}else{
stringBufferOne.append(f.getName()+"$");
stringBufferTwo.append(f.get(object).toString()+"$");
System.out.println("属性:"+f.getName()+" 值:"+f.get(object).toString());
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
以下仅供参考:
Field[] fields = userDetails.getClass().getDeclaredFields();
if (null == fields) {
return;
}
for (Field s : fields) {
String name = s.getName();//获取属性名
String type = s.getGenericType().toString();//获取属性类型
if (type.equals("class java.lang.String")) {
try {
Method m = userDetails.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
String value = (String) m.invoke(userDetails);
if (null != value) {
Log.d("---属性名---",name);
Log.d("---属性值---",value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (type.equals("class java.lang.Integer")) {
try {
Method m = userDetails.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
int value = (String) m.invoke(userDetails);
if (null != value) {
Log.d("---属性名---",name);
Log.d("---属性值---",value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------