Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.lang.reflect.Field;
- import java.lang.reflect.Modifier;
- import java.util.Iterator;
- public class ObjectDumper {
- private static final String NULL = "NULL";
- private static final String EMPTY = "";
- private static final String OBJECT_SUFFIX = "}";
- private static final String OBJECT_PREFIX = "%s{";
- private static final String FIELD_OPERAND = "=";
- private static final String ERROR = "<ERROR>";
- private static final String CYCLE_FOUND = "<CYCLE FOUND>";
- private static final String TUPLE_SEPARATOR = ",";
- private static final String TUPLE_BRACKET_CLOSE = "]";
- private static final String TUPLE_BLACKET_OPEN = "[";
- private static final String DOUBLE_QUOTES = "\"";
- public static String dump(Object object) {
- return dump(object, 5);
- }
- private static String dump(Object object, int maxDepth) {
- return dump(object, 0, maxDepth);
- }
- private static String dump(Object object, int currentDepth, int maxDepth) {
- if (object == null) {
- return NULL;
- }
- Class<?> type = object.getClass();
- if (isWritable(type)) {
- return DOUBLE_QUOTES + String.valueOf(object) + DOUBLE_QUOTES;
- } else if (Iterable.class.isAssignableFrom(type)) {
- Iterator<?> iterator = ((Iterable<?>) object).iterator();
- StringBuilder subBuilder = new StringBuilder(TUPLE_BLACKET_OPEN);
- while (iterator.hasNext()) {
- subBuilder.append(subBuilder.length() > 1 ? TUPLE_SEPARATOR : EMPTY).append(dump(iterator.next(), currentDepth + 1, maxDepth));
- }
- return subBuilder.append(TUPLE_BRACKET_CLOSE).toString();
- } else if (type.isArray()) {
- StringBuilder subBuilder = new StringBuilder(TUPLE_BLACKET_OPEN);
- for (int i = 0; i < Array.getLength(object); i++) {
- subBuilder.append(subBuilder.length() > 1 ? TUPLE_SEPARATOR : EMPTY).append(dump(Array.get(object, i), currentDepth + 1, maxDepth));
- }
- return subBuilder.append(TUPLE_BRACKET_CLOSE).toString();
- } else {
- if (currentDepth == maxDepth) {
- return String.valueOf(object);
- } else {
- StringBuilder contents = new StringBuilder();
- contents = contents.append(String.format(OBJECT_PREFIX, type.getName()));
- Class<?> tempType = type;
- while (tempType != null && tempType != Object.class) {
- for (Field field : tempType.getDeclaredFields()) {
- boolean fieldAccessibility = field.isAccessible();
- try {
- if (!field.isAccessible()) {
- field.setAccessible(true);
- }
- if (!Modifier.isFinal(field.getModifiers())) {
- try {
- Object fieldValue = field.get(object);
- contents = contents.append(contents.indexOf(FIELD_OPERAND) > 1 ? TUPLE_SEPARATOR : EMPTY).append(field.getName())
- .append(FIELD_OPERAND);
- contents.append(System.identityHashCode(fieldValue) != System.identityHashCode(object) ? dump(fieldValue,
- currentDepth + 1, maxDepth) : CYCLE_FOUND);
- } catch (Exception e) {
- contents = contents.append(ERROR);
- }
- }
- } finally {
- field.setAccessible(fieldAccessibility);
- }
- }
- tempType = tempType.getSuperclass();
- }
- return contents.append(OBJECT_SUFFIX).toString();
- }
- }
- }
- private static boolean isWritable(Class<?> type) {
- if (type.isPrimitive() || type.isEnum() || String.class.isAssignableFrom(type)) {
- return true;
- }
- if (type == java.lang.Long.class || type == java.lang.Integer.class || type == java.lang.Boolean.class || type == java.lang.String.class
- || type == java.lang.Double.class || type == java.lang.Short.class || type == java.lang.Byte.class) {
- return true;
- }
- return false;
- }
- public static void main(String[] args) {
- System.out.println(ObjectDumper.dump(new Exception()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement