Skip to content

Instantly share code, notes, and snippets.

@Hackforid
Created October 18, 2014 12:27
Show Gist options
  • Save Hackforid/46f79a804d8892b3d6ca to your computer and use it in GitHub Desktop.
Save Hackforid/46f79a804d8892b3d6ca to your computer and use it in GitHub Desktop.
Java
public static String dump(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
StringBuilder sb = new StringBuilder();
sb.append(object.getClass().getSimpleName()).append('{');
boolean firstRound = true;
for (Field field : fields) {
if (!firstRound) {
sb.append(", ");
}
firstRound = false;
field.setAccessible(true);
try {
final Object fieldObj = field.get(object);
final String value;
if (null == fieldObj) {
value = "null";
} else {
value = fieldObj.toString();
}
sb.append(field.getName()).append('=').append('\'')
.append(value).append('\'');
} catch (IllegalAccessException ignore) {
//this should never happen
}
}
sb.append('}');
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment