Last active
December 10, 2015 05:39
-
-
Save jekamax/4389255 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jekamax.utils; | |
import java.lang.reflect.Field; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.TreeSet; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
*Print Contents of deep Hierarchies of objects | |
* To be printed object must implement ObjectPrinter.Printable | |
* (its just a marker, no real methods to implement) | |
* Printer is protected from recursion by tracing already printed objects | |
* @author jekamax | |
*/ | |
public class ObjectPrinter | |
{ | |
public static interface Printable | |
{ | |
} | |
public static String makeIndent(int indent) | |
{ | |
StringBuilder stBuilder = new StringBuilder(); | |
for (int i = 0; i < indent; ++i) | |
{ | |
stBuilder.append(" "); | |
} | |
return stBuilder.toString(); | |
} | |
public static String indentStr(int indent, String str) | |
{ | |
String sep = System.getProperty("line.separator"); | |
String indentStr = makeIndent(indent); | |
String[] split = str.split("\\r?\\n"); | |
StringBuilder stBuilder = new StringBuilder(); | |
for (String st : split) | |
{ | |
stBuilder.append(indentStr); | |
stBuilder.append(st); | |
stBuilder.append(sep); | |
} | |
return stBuilder.toString(); | |
} | |
public static String printObject(Object obj) | |
{ | |
return printObject(obj, 0, null, false); | |
} | |
public static String printObject(Object obj, int preIndent, Set<Object> AlreadyPrinted, boolean inline) | |
{ | |
StringBuilder stBuilder = new StringBuilder(); | |
String objStr="null"; | |
if(obj!=null) | |
{ | |
objStr=obj.toString(); | |
} | |
if (inline) | |
{ | |
stBuilder.append("["+objStr+"] "); | |
if(obj!=null) | |
{ | |
stBuilder.append(obj.getClass().getSimpleName()); | |
} | |
} else | |
{ | |
stBuilder.append(indentStr(preIndent, objStr)); | |
} | |
if (AlreadyPrinted == null) | |
{ | |
AlreadyPrinted = new HashSet<Object>(); | |
} | |
if (obj!=null && (obj instanceof ObjectPrinter.Printable || obj instanceof Collection<?>)) | |
{ | |
if (!AlreadyPrinted.contains(obj)) | |
{ | |
AlreadyPrinted.add(obj); | |
if(inline) | |
{ | |
stBuilder.append("\n"); | |
} | |
stBuilder.append(indentStr(preIndent, String.format("{"))); | |
int indent = preIndent + 1; | |
if (obj instanceof Collection<?>) | |
{ | |
Collection<?> coll = (Collection<?>) obj; | |
for (Object o : coll) | |
{ | |
stBuilder.append(printObject(o, indent , AlreadyPrinted, false)); | |
} | |
} else | |
{ | |
for (Field field : obj.getClass().getDeclaredFields()) | |
{ | |
try | |
{ | |
field.setAccessible(true); | |
String name = field.getName(); | |
java.lang.Object value = field.get(obj); | |
stBuilder.append(indentStr(indent, String.format("%s: %s", name, printObject(value, preIndent, AlreadyPrinted, true)))); | |
} | |
catch (IllegalArgumentException ex) | |
{ | |
Logger.getLogger(OBJ_Object.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
catch (IllegalAccessException ex) | |
{ | |
Logger.getLogger(OBJ_Object.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
stBuilder.append(indentStr(preIndent, String.format("}"))); | |
} else | |
{ | |
stBuilder.append(" (already printed)"); | |
} | |
} | |
return stBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment